环境: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值?
感谢您的时间。
答案 0 :(得分:2)
给出消息
无法将值NULL插入列'NullBuster',表'MainDB.dbo.varUser';列不允许空值。 INSERT失败。
我认为表nullbuster
中的列varUser
设置为不允许空值,并且您尝试在其中插入一列。
答案 1 :(得分:0)
错误说明:表'MainDB.dbo.varUser';列不允许空值。您必须更改表结构以将空值插入所需的列。