0

I have two Databases db1 and db2. In those two databases I have a table name "Asset_table" with same columns and same table structure. But In Some client's db2 does not have the "Asset_table".

First I need to check "Asset_table" is available in the client database and if it is available then insert data from db1 to that client DB table.

Here is my query.

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

Can any one sugest me an script for this?

jarlh
  • 42,561
  • 8
  • 45
  • 63

1 Answers1

0

Is this what you need?

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;