Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query all DBs on a database server

Author  Topic 

kdyer
Yak Posting Veteran

53 Posts

Posted - 2007-05-29 : 18:20:00
Trying to run the following and not getting good results. What am I missing? It returns whether it finds the column, however it does not return whether it does not either.


declare c1 cursor
for select name from dbo.sysdatabases (nolock) where name not in ('master','model','msdb','northwind','pubs','tempdb','setups') order by name
declare @v_run varchar(1000), @dbname varchar(250),@ct int
SET QUOTED_IDENTIFIER ON
open c1
fetch next from c1 into @dbname
while (@@fetch_status <> -1)
begin
IF LEFT(@dbname,8)<>'DB_0000_'
begin
print '**** '+@dbname+' ****'
print ' '
set @v_run = 'USE '+@dbname+';(SELECT COUNT(QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = ''BASE TABLE''
AND QUOTENAME(TABLE_NAME) = ''[scheduledPayment]'')'
--print @v_run
exec (@v_run)
select @v_run = @ct
SELECT CASE @ct
WHEN 1 THEN 'scheduledPayment is found'
WHEN 0 THEN 'scheduledPayment is not found'
end
From @v_run
--if @v_run <> '1'
-- begin
-- print 'scheduledPayment is found'
-- end
--else
-- begin
-- print 'scheduledPayment is not found'
-- end
--end
fetch next from c1 into @dbname
end
close c1
deallocate c1


Thanks,

Kent

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-05-29 : 19:49:38
you can't extract data from a dynamic sql statement that way. You have to use sp_executesql with output params. Here's an example from BOL:


DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @max_title varchar(30);

SET @IntVariable = 197;
SET @SQLString = N'SELECT @max_titleOUT = max(Title)
FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @level';
SET @ParmDefinition = N'@level tinyint, @max_titleOUT varchar(30) OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable, @max_titleOUT=@max_title OUTPUT;
SELECT @max_title;


from this page: http://msdn2.microsoft.com/en-us/library/ms188001.aspx


www.elsasoft.org
Go to Top of Page
   

- Advertisement -