簡體   English   中英

將數據從架構中的表復制到另一個架構中的另一個表

[英]Copy data from a table in a schema to another table in another schema

我必須將數據從架構中的表復制到另一個架構中的另一個表。 這兩個表具有相同的列。 我試過了

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]

如果第二個表存在

Set @script = 'insert into schema2.' + @tablename + ' select * from schema1.' + @tablename

如果第二個表不存在

Set @script = 'select * into schema2.' + @tablename + ' from schema1.' + @tablename

然后

Exec sp_executesql @script

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM