[英]How to copy a table from a linked server into the main database only on the condition that the table does not already exist?
我正在使用SQL Server 2012,並且具有以下T-SQL查詢:
USE MyDatabase
INSERT INTO [Table1]
SELECT *
FROM [xxx.xx.x.xx].[xxx].[dbo].[Table1]
我想修改此查詢,以便僅在MyDatabase
中不存在該表的情況下才將Table1
復制到MyDatabase
中。
我在這里查看了一下,但無法弄清楚如何將解決方案遷移到我的問題中: 檢查表是否在SQL Server中存在
我該如何實現?
應該這樣做:
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Table1'))
BEGIN
INSERT INTO [Table1]
SELECT *
FROM [xxx.xx.x.xx].[xxx].[dbo].[Table1]
END
您可以在此處閱讀更多詳細信息和方法: 檢查SQL Server中是否存在表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.