2013-10-22 98 views
1

环境:SQL Server 2008 R2。无法将NULL值插入'NullBuster'列

我有以下SP应该将表中的数据从MainDB复制到MiniDB到相同架构明智的表中。但是当我运行它,我得到这个错误:

(1 row(s) affected) 
Start Copying table: varUser at 2013-10-22 11:37:54 
Bulk copy error: Cannot insert the value NULL into column 'NullBuster', table 'MainDB.dbo.varUser'; column does not allow nulls. INSERT fails. 
The statement has been terminated. 

A .NET Framework error occurred during execution of user-defined routine or aggregate "usp_BulkCopy": 
System.NullReferenceException: Object reference not set to an instance of an object. 
System.NullReferenceException: 
    at StoredProcedures.usp_BulkCopy(String sourceServer, String sourceDatabase, String sourceSelectQuery, String destinationServer, String destinationDatabase, String destinationTable, Boolean FlagKeepIdentity, Boolean throwExceptionOnErrors, Boolean SourceTrusted, Boolean DestTrusted, String SourceUser, String SourcePass, String DestUser, String DestPass, String ColumnMappings) 
. 
A .NET Framework error occurred during execution of user-defined routine or aggregate "usp_BulkCopy": 
System.NullReferenceException: Object reference not set to an instance of an object. 
System.NullReferenceException: 
    at StoredProcedures.usp_BulkCopy(String sourceServer, String sourceDatabase, String sourceSelectQuery, String destinationServer, String destinationDatabase, String destinationTable, Boolean FlagKeepIdentity, Boolean throwExceptionOnErrors, Boolean SourceTrusted, Boolean DestTrusted, String SourceUser, String SourcePass, String DestUser, String DestPass, String ColumnMappings) 
. 

(1 row(s) affected) 

这里是我如何运行它:

exec dbo.sp_Copy_MYDB_Subset_Tables1 'Server1\Instance','MainDB',''Server1\Instance','Mini_DB' 

这里是SP。

USE [MYDB] 
GO 

/****** Object: StoredProcedure [dbo].[sp_Copy_MYDB_Subset_Tables1] Script Date: 10/22/2013 11:21:17 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE [dbo].[sp_Copy_MYDB_Subset_Tables1](
    @vSourceServer  varchar(255) 
    ,@vSourceDatabase varchar(255) = 'MYDB' 
    ,@vDestinationServer varchar(255) 
    ,@vDestinationDatabase varchar(255) = 'MYDB' 
    ,@vIsServerOnDomain  BIT = 1 -- 
    ,@TargetDBUserName varchar(255) = '' 
    ,@TargetDBPassword varchar(255) = '' 

    ) 
AS 
BEGIN 

    Declare 
    @vSourceTable varchar(255) 
    ,@vSourceSelectQuery varchar(255) 
    ,@vDestinationTable  varchar(255) 
    ,@vReturn    int 
    ,@vReturnMessage  varchar(max) 
    ,@vPeriodtoArchive  int 
    ,@ColumnMappings  varchar(4000) 


BEGIN TRY 


    if (@vSourceServer is null or @vSourceServer = '') 
     set @vSourceServer = @@servername 


    if object_id('tempdb..#TempTableCopyList') is not null 
     drop table #TempTableCopyList 

    Create Table #TempTableCopyList 
    (
     id [int] NOT NULL primary key clustered 
     ,TableName  varchar(100) 
     ,ColumnMappings varchar(4000) 
     ,DateCopied  datetime 
    ) 

    insert into #TempTableCopyList 
     Select id, TableName, ColumnMappings, DateCopied 
     from dbo.fn_Get_MYDB_Subset_TableList() 
     where TableName = 'varuser' -- just to test with one table. 

    declare c cursor for 
    Select TableName, ColumnMappings 
     from #TempTableCopyList 
      order by id desc 


    open c 

    fetch next from c into @vSourceTable, @ColumnMappings 

    While @@fetch_status =0 BEGIN 


       print 'Start Copying table: ' + @vSourceTable + ' at ' + convert(varchar(30),getdate(),120) 

       Set @vSourceSelectQuery = 'Select * from ' + @vSourceTable + ' with (nolock) ' 

     IF @vIsServerOnDomain = 0 
     BEGIN 
       exec master.dbo.usp_BulkCopy 
        @vSourceServer 
        ,@vSourceDatabase 
        ,@vSourceSelectQuery 
        ,@vDestinationServer 
        ,@vDestinationDatabase 
        ,@vSourceTable 
        ,1 
        ,1 
        ,true 
        ,false 
        ,'' 
        ,'' 
        ,@TargetDBUserName 
        ,@TargetDBPassword 
        ,@ColumnMappings 
     END 
     ELSE BEGIN 

       exec master.dbo.usp_BulkCopy 
        @vSourceServer 
        ,@vSourceDatabase 
        ,@vSourceSelectQuery 
        ,@vDestinationServer 
        ,@vDestinationDatabase 
        ,@vSourceTable 
        ,1 
        ,1 
        ,true 
        ,true 
        ,'' 
        ,'' 
        ,'' 
        ,'' 
        ,@ColumnMappings 
     END 
       UPDATE #TempTableCopyList 
        set DateCopied = GETDATE() 
       WHERE TableName = @vSourceTable 




     fetch next from c into @vSourceTable, @ColumnMappings 

    END 

    close c 
    deallocate c 

END TRY 
BEGIN CATCH 

    close c 
    deallocate c 
    DECLARE @ErrorMessage VARCHAR(MAX) 
    SET @ErrorMessage = error_message() 
    print @vSourceTable + '; '+ @vSourceServer+ '; '+ @vSourceDatabase+ '; '+ @vDestinationServer+ '; '+ @vDestinationDatabase+ '; '+ @vDestinationTable 
    Print @ErrorMessage 
    RAISERROR (@ErrorMessage, 0, 1) 

END CATCH 

    --INFORMATIONAL 
    SELECT * FROM #TempTableCopyList 

    drop table #TempTableCopyList 

return 

END 


GO 

是什么原因导致了这个错误?我认为这将是.net版本,它看起来不像。能够使用的“varuser”在某些列中具有空值。这可能是原因吗?如果这是原因,那么如何使这些工作在列中的NULL值?

谢谢你的时间。

回答

2

鉴于消息

无法插入NULL值插入列 'NullBuster',表 'MainDB.dbo.varUser';列不允许有空值。 INSERT失败。

我会想象在表varUsernullbuster设置为不允许为空,而你试图插入一个进去。

+0

我也这么认为。但是在表格中没有列作为黑名单。所以我认为这可能是一个神秘的SQL服务器说话的方式。 – user1666952

+0

这不是一个SQL Server消息。 'usp_BulkCopy'中有一些东西。我从名字谜团中猜测,它是专门设计来防止空值。 – podiluska

0

由于错误说:表'MainDB.dbo.varUser';列不允许为空值。您必须更改表结构以将空值插入到所需的列中。

相关问题