Sql 将数据从架构中的表复制到另一架构中的另一个表
我必须将数据从模式中的一个表复制到另一个模式中的另一个表。这两个表具有相同的列。我试过了Sql 将数据从架构中的表复制到另一架构中的另一个表,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我必须将数据从模式中的一个表复制到另一个模式中的另一个表。这两个表具有相同的列。我试过了 SET @Script = 'SELECT * FROM application_params.' + @tableName + ' INSERT INTO temp_application_params.' +@tableName Exec sp_executesql @Script 其中@tablename是一个包含表名的变量。表的名称是相同的。但是这些指令不会复制数据,第二个表仍然是空的。问题是
SET @Script = 'SELECT * FROM application_params.' + @tableName + ' INSERT INTO temp_application_params.' +@tableName
Exec sp_executesql @Script
其中@tablename是一个包含表名的变量。表的名称是相同的。但是这些指令不会复制数据,第二个表仍然是空的。问题是什么?如果目标表存在:
INSERT INTO targetTable
SELECT * FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
如果目标表不存在:
select * into targetTable from [sourceserver].[sourcedatabase].[dbo].[sourceTable]
如果目标表存在:
INSERT INTO targetTable
SELECT * FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
如果目标表不存在:
select * into targetTable from [sourceserver].[sourcedatabase].[dbo].[sourceTable]
如果第二个表存在
Set @script = 'insert into schema2.' + @tablename + ' select * from schema1.' + @tablename
如果第二个表不存在
Set @script = 'select * into schema2.' + @tablename + ' from schema1.' + @tablename
然后
如果第二个表存在
Set @script = 'insert into schema2.' + @tablename + ' select * from schema1.' + @tablename
如果第二个表不存在
Set @script = 'select * into schema2.' + @tablename + ' from schema1.' + @tablename
然后
这是如何运行的呢?您知道正确的语法应该是
INSERT-INTO-table-SELECT。。。从另一个表中
@Lamak:我也尝试过这种方法,但它不起作用。什么不起作用?
在这里的意思是什么?你发布的查询应该给你一个错误为什么要使用动态sql?一个简单的插入选择模式应该可以做到这一点,没有问题。我强烈建议您不要使用select*作为插入。您应该显式地命名列,以避免将来在表更改时出现问题。这是如何运行的?您知道正确的语法应该是INSERT-INTO-table-SELECT。。。从另一个表中
@Lamak:我也尝试过这种方法,但它不起作用。什么不起作用?
在这里的意思是什么?你发布的查询应该给你一个错误为什么要使用动态sql?一个简单的插入选择模式应该可以做到这一点,没有问题。我强烈建议您不要使用select*作为插入。您应该显式地命名列,以避免将来在表更改时出现问题。