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
 General SQL Server Forums
 New to SQL Server Programming
 Wacky stored proc using a variable

Author  Topic 

craigwg
Posting Yak Master

154 Posts

Posted - 2009-07-23 : 11:02:40
Hello,

I am new to writing stored procedures and my dilemna is simple, I think. I have a table of server information which is static. I am trying to create a SP that will check (among many other things) the active databases on a server and compare that to the databases in my static table. But my SP will not work. Below is my code:


--The Stored Procedure
alter procedure countdb3
@servername sysname
as
/* declare @sql varchar(4000) */ --result of earlier attempt
execute sp_executesql
select (select serverid from usslcrioprod.admin.dbo.servers where servername='+@servername+'), *
from @servername.master..sysdatabases;

/* set @sql= --this goes with earlier attempt
'select (select serverid from usslcrioprod.admin.dbo.servers where servername='+@servername+'), *
from '+@servername+'.master.sys.sysdatabases s'
exec (@sql) */


I then execute the stored procedure by entering:
exec countdb3 'USSLCSQLT11'


I have squirrled around with many variations of ...['+@servername+']' and @servername.master.sys.sysdatabases. All kinds of things. My goal of course is to loop through 120+ servers which will build a massive table and truly be awesome. Later I want to add in things to check job status, backup status and more. All of the things I check in a typical server review. But I have to get this working before I can continue.

Help!

Craig Greenwood

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2009-07-23 : 19:23:45
Hi Craig,

To debug, have you tried a PRINT @sql in your proc to see what statement is produced? You can then cut&paste to a new query window and run it. I normally find this very handy when dealing with dynamic SQL.

HTH,

Tim
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-23 : 21:02:55
answered here i believe http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=129922
Go to Top of Page
   

- Advertisement -