我将多个表从一个数据库复制到另一个数据库,我希望将declare/set
和source_db_name
destination_db_name
作为变量。我尝试使用以下内容但没有成功
DECLARE @DESTINATION_DB_NAME char(30)
SET @DESTINATION_DB_NAME = 'DDB'
DECLARE @SOURCE_DB_NAME char(30)
SET @SOURCE_DB_NAME = 'SDB'
然后使用:
CREATE TABLE [DESTINATION_DB_NAME].[dbo].[DESTINATION_TBL]
INSERT INTO [DESTINATION_DB_NAME].[dbo].[DESTINTION_TBL]
FROM [SOURCE_DB_NAME].[dbo].[SOURCE_TBL]
答案 0 :(得分:0)
您需要为此准备动态查询,如下所示:
DECLARE @DESTINATION_DB_NAME varchar(30)
SET @DESTINATION_DB_NAME = 'DDB'
DECLARE @SOURCE_DB_NAME varchar(30)
SET @SOURCE_DB_NAME = 'SDB'
DECLARE @SQL varchar(max)
SET @SQL = 'CREATE TABLE ['+@DESTINATION_DB_NAME + '].[dbo].[DESTINATION_TBL] INSERT INTO ['+ @DESTINATION_DB_NAME + '].[dbo].[DESTINTION_TBL] FROM [' + @SOURCE_DB_NAME + '].[dbo].[SOURCE_TBL]'
print @SQL
exec (@SQL)