在尝试找出问题后,我自己做了一些调试
我发现 @cols 和 @cols2 变量没有带来结果,我有 PRINT
PRINT('INSERT INTO [' + @Destination_Database_Name + '].[dbo].[' + @tablename + '] (' + @cols2 + ']' + ') SELECT [' + @cols2 + ']' + ' FROM [' + @Source_Database_Name + '].[dbo].[' + @tablename + ']');
而且该语句不会显示我得到的所有输出
(1 行受影响)
(1 行受影响)我在这里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
我可以根据要求发布完整代码