在尝试自己做一些调试后找出问题
我发现@cols和@cols2变量没有带来结果,我有PRINT
PRINT('INSERT INTO [' + @Destination_Database_Name + '].[dbo].[' + @tablename + '] (' + @cols2 + ']' + ') SELECT [' + @cols2 + ']' + ' FROM [' + @Source_Database_Name + '].[dbo].[' + @tablename + ']');
声明不会显示我得到的输出
(1行受影响)
(1行(s)受影响) 我在这里2
c365online_script1 我在这里3 tCompany
这是我认为是问题的代码部分
Print 'I am here2'
SET IDENTITY_INSERT c365online_script1.dbo.tCompany ON
declare @cols2 varchar(max)
PRINT @cols2
select @cols2 = (Select Stuff((Select '],[' + C.COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS As C Where C.TABLE_SCHEMA = T.TABLE_SCHEMA And C.TABLE_NAME = T.TABLE_NAME Order By C.ORDINAL_POSITION For Xml Path('')), 1, 2, '') As Columns From INFORMATION_SCHEMA.TABLES As T WHERE T.TABLE_NAME = @tablename)
PRINT('INSERT INTO [' + @Destination_Database_Name + '].[dbo].[' + @tablename + '] (' + @cols2 + ']' + ') SELECT [' + @cols2 + ']' + ' FROM [' + @Source_Database_Name + '].[dbo].[' + @tablename + ']');
PRINT @Destination_Database_Name
Print 'I am here3'
Print @tablename
END
我可以根据要求发布完整的代码
答案 0 :(得分:0)
尝试
SET @cols2 = (Select....)
而不是
SELECT @cols2 = (Select....)
答案 1 :(得分:0)
这是一个NULL连接问题。尝试将变量初始化为空字符串,如下所示,并查看您对查询结果的了解:
declare @tablename varchar(128) = '',
@Destination_Database_Name varchar(128) = '',
@Source_Database_Name varchar(128) = '';