1

环境:SQL Server 2008 R2。

我有以下 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 值一起工作?

谢谢你的时间。

4

2 回答 2

2

鉴于消息

无法将值 NULL 插入到列“NullBuster”、表“MainDB.dbo.varUser”中;列不允许空值。插入失败。

我会想象表中的列nullbustervarUser设置为不允许空值,而您正试图在其中插入一个。

于 2013-10-22T15:46:24.123 回答
0

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

于 2013-10-22T15:46:39.933 回答