I'm pretty new to sql so any help will be much appreciated
I have a table containing a list of table names in a column of a table and I need to retrieve a column called [Last Refreshed] from all the tables listed. The tables all have different structures but they all have the [Last Refreshed] Column. I have managed to insert the tablenames into a sql variable but up to this point I am kind of stuck. I hope I managed to explain what I need but I have attached my code as well.
Declare @tables nvarchar(max)
Declare @sql nvarchar(max)
Declare @cnt int
DECLARE @Counter int
SET @Counter = 1
DECLARE @RowCount INT
SET @RowCount = (SELECT COUNT(*)
FROM (
SELECT * FROM TABLE_LIST1
UNION
SELECT * FROM TABLE_LIST2) data )
DROP TABLE #DB_DUMMY
CREATE TABLE #DB_DUMMY (
[TABLENAME] VARCHAR(512),
[LAST_REFRESHED] VARCHAR(533)
);
WHILE ( @Counter <= @RowCount)
BEGIN
SELECT @tables = FinalTable, @cnt = Row_num from (
SELECT FinalTable , ROW_NUMBER() OVER(ORDER BY FinalTable DESC) AS Row_num
FROM (
SELECT FinalTable FROM TABLE_LIST1
UNION
SELECT FinalTable FROM ABLE_LIST2) data
group by FinalTable) a
where Row_num = @Counter
--This part doesnt work
INSERT INTO #DB_DUMMY(TABLENAME,LAST_REFRESHED)
SELECT @tables , [Last Refreshed] from @tables
SET @Counter = @Counter + 1
END
exec(@sql)
I expect to see a list of tablenames as well as the last refresh in the temporary table #DB_DUMMY