I need to print if particular table values for all database in a server. I tried this below error
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "@lookupname".
Can anybody advise me as to the mistake I made?
declare @name varchar(50);
declare Countofprovider cursor
for
SELECT name
FROM master.dbo.sysdatabases where name like '%CPM_%'
open Countofprovider
fetch next from Countofprovider into @name;
while @@FETCH_STATUS = 0
begin
--print 'Name : '+ @name+ ''
declare @sql varchar(MAX);
declare @lookupname varchar(100);
declare @lookupvalue varchar(100);
---------------------------
SET @sql = 'select @lookupname=lookupname,@lookupvalue=lookupvalue from [AUS-CPMLDGDB01].'+@name+'.cpm.ApplicationLookup where lookupname = ''IsPCPLeakageEnabled'' and lookupvalue = ''TRUE'''
print 'Name : '+ @name+ ', lookupname : '+ @lookupname+ ', Loolupvalue : '+@lookupvalue+''
exec (@SQL)
---------------------------
fetch next from Countofprovider into @name
end
CLOSE Countofprovider;
DEALLOCATE Countofprovider;
name like '%CPM_%'
, so@@FETCH_STATUS
never has the value0
and the code in thewhile
is never executed. – HoneyBadger