2013-10-22 124 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';列不允許爲空值。您必須更改表結構以將空值插入到所需的列中。

相關問題