I just wrote the following script. I'm wondering if it is possible to display the table name after each select statement so I can actually see from which table I have results.
Declare @tables table (table1 varchar(50));
declare @table varchar(50);
declare @tablename varchar(50);
INSERT INTO @tables select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where
COLUMN_NAME ='order_id'
While (Select Count(*) From @tables Where table1 IS NOT NULL) > 0
BEGIN
set @table = (select top 1 table1 from @tables)
EXEC ('IF EXISTS (SELECT order_id from [' + @table + '] where order_id =
13940001 )
SELECT * FROM [' + @table + '] where order_id = 13940001
'
)
--!! Remember to put replace those order_ids with your
order_ids on both these lines
delete from @tables where table1 = @table
END
I tried to do it like this:
Declare @tables table (table1 varchar(50));
declare @table varchar(50);
declare @tablename varchar(50);
INSERT INTO @tables select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where
COLUMN_NAME ='order_id'
While (Select Count(*) From @tables Where table1 IS NOT NULL) > 0
BEGIN
set @table = (select top 1 table1 from @tables)
EXEC ('IF EXISTS (SELECT order_id from [' + @table + '] where order_id =
13940001 )
SELECT * FROM [' + @table + '] where order_id = 13940001
SELECT @table'
)
--!! Remember to put replace those order_ids with your
order_ids on both these lines
delete from @tables where table1 = @table
END
But then I encounter this error:
Must declare the scalar variable "@TABLE".
And I don't know what to do about it.