在 Sql 服务器的特定数据库中检查 Table 可用

Check a Table Available in a Particular Database in Sql Server

我有两个数据库 db1 和 db2。在这两个数据库中,我有一个 table 名称“Asset_table”,具有相同的列和相同的 table 结构。但是在某些客户端的 db2 中没有 "Asset_table".

首先,我需要检查“Asset_table”在客户端数据库中是否可用,如果可用,则将数据从 db1 插入到该客户端数据库 table。

这是我的查询。

IF EXISTS (--here I need to check if the table is available in the db2---)
BEGIN 
INSERT INTO db2.Asset_table( asset_id, name,qty,description)
SELECT  asset_id, name,qty,description
FROM db1.Asset_table
END

谁能给我一个脚本吗?

这是你需要的吗?

IF NOT EXISTS(Select 1 from db2.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo' AND TABLE_NAME='Asset_table')
BEGIN
CREATE TABLE db2.dbo.Asset_table(asset_id int, name nvarchar(50),qty int,description nvarchar(150));
END

INSERT INTO db2.dbo.Asset_table( asset_id, name,qty,description)
SELECT  asset_id, name,qty,description
FROM db1.dbo.Asset_table;