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
 cursor multiple database

Author  Topic 

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2010-06-15 : 19:45:24
I'm using a cursor to create the same SP in multiple database however the cursor doesn't like the GO statement.



DECLARE @dbname VARCHAR(50), @query VARCHAR(8000)

DECLARE cur_objc CURSOR FOR
SELECT name FROM sys.databases
WHERE name IN ('kkk','xxx','abc')

OPEN cur_objc
FETCH NEXT FROM cur_objc INTO @dbname

WHILE @@FETCH_STATUS = 0
BEGIN

SET @query =

'
use ' + @dbname + '


if exists (select * from sys.procedures where name = ''TruncTbl'')
drop proc TruncTbl


create PROC [dbo].[TruncTbl]
@schema VARCHAR(30) = ''dbo'', @tbl VARCHAR(50)
WITH EXECUTE AS OWNER
AS
DECLARE @sql VARCHAR(100)
SET @sql = ''TRUNCATE TABLE ['' + @schema + ''].['' + @tbl + '']''
EXEC (@sql)
--print (@sql)

'
exec (@query)
print (@query)


FETCH NEXT FROM cur_objc INTO @dbname
END

CLOSE cur_objc
DEALLOCATE cur_objc



Anyway to work around this?

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2010-06-16 : 01:10:48
prefer this:

1. put script for your proc in a .sql file.
2. execute the .sql file using SQLCMD via .bat or powershell script. in the script, loop over the databases you want the proc in


elsasoft.org
Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2010-06-16 : 11:23:11
Thanks Jezemine I will give it a try in a little bit.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-16 : 11:29:06
I'm all for automation...but really...killing mult dbs in one shot????

make sure you have backups....and if your goal is to have "empty" db's...do it once and dump it


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2010-06-16 : 18:53:43
also the proc itself is questionable. (I didn't look at it when I first answered the question)

A proc to truncate a table where you pass the tablename? why not just issue the truncate statement directly?

It's generally frowned upon to be passing strings to procedures and then constructing dynamic sql from that input. Your code is vulnerable to sql injection.


elsasoft.org
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-16 : 19:44:17
ahhh...it's not probably being called...


Are you going to worry about RI????

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2010-06-17 : 00:16:57
jezemine,
i dont wwant to assign ddl_dbadmin server role. Thats why.

x002548,
no RI, this are reporting tables.
Go to Top of Page
   

- Advertisement -