select distinct gln, soi.name as itemsTable, soia.name as attributesTable, soih.name as hierarchyTable from tbl_GS1_items i left outer join gs1..sysobjects soi on soi.name = 'tbl_GS1_Items_' + gln and soi.xtype = 'U' left outer join gs1..sysobjects soia on soia.name = 'tbl_GS1_ItemAttributes_' + gln and soia.xtype = 'U' left outer join gs1..sysobjects soih on soih.name = 'tbl_GS1_ItemHierarchy_' + gln and soih.xtype = 'U' where nullif( gln, '' ) is not null and gln <> '0000000000000' -- and gln = '0075192000008' order by gln select * from tbl_GS1_items where gln = 'NOMATCH' select * from tbl_GS1_itemAttributes where gln = 'NOMATCH' select * from tbl_GS1_itemHierarchy where gln = 'NOMATCH' delete from gs1..tbl_GS1_Items_#gln# delete from gs1..tbl_GS1_ItemAttributes_#gln# delete from gs1..tbl_GS1_ItemHierarchy_#gln# insert into gs1..tbl_GS1_Items_#gln#( #_itemsColumns# ) select #_itemsColumns# from tbl_GS1_items where gln = delete from tbl_GS1_items where gln = insert into gs1..tbl_GS1_itemAttributes_#gln# ( #_attributesColumns# ) select #_attributesColumns# from tbl_GS1_itemAttributes where gln = delete from tbl_GS1_itemAttributes where gln = insert into gs1..tbl_GS1_itemHierarchy_#gln# ( #_hierarchyColumns# ) select #_hierarchyColumns# from tbl_GS1_itemHierarchy where gln = delete from tbl_GS1_itemHierarchy where gln = select distinct gln, soi.name as itemsTable, soia.name as attributesTable, soih.name as hierarchyTable from tbl_GS1_items i left outer join gs1..sysobjects soi on soi.name = 'tbl_GS1_Items_' + gln and soi.xtype = 'U' left outer join gs1..sysobjects soia on soia.name = 'tbl_GS1_ItemAttributes_' + gln and soia.xtype = 'U' left outer join gs1..sysobjects soih on soih.name = 'tbl_GS1_ItemHierarchy_' + gln and soih.xtype = 'U' where nullif( gln, '' ) is not null and gln <> '0000000000000'