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.
| Author |
Topic |
|
John Newman
Starting Member
2 Posts |
Posted - 2002-09-09 : 16:30:13
|
| I need to automate a Kill statement to kill connections that areleft open overnight. Other processes need to run (restore db)that will not run if connections are open to the db. I have written code that will supply the necessary spid's. However, the KILL commanddoes not seem to accept a variable as an argument. The following code would work if the KILL statement would accept avariable. (The table #tb2_sysprocesses contains the spids that areconnected that need to be killed.)BEGIN TRANSACTIONDeclare spid_cursor1 cursor FOR SELECT cast(spid as smallint) from #tb2_sysprocesses where spid is not nullOPEN SPID_cursor1Declare @tempspid smallintFETCH NEXT FROM spid_cursor1 INTO @tempspidWHILE(@@FETCH_STATUS = 0) BEGIN Kill @tempspid FETCH NEXT FROM spid_cursor1 INTO @tempspid ENDCLOSE spid_cursor1DEALLOCATE spid_cursor1 COMMIT TRANSACTION Can you please tell me a way to resolve this problem regarding restrictions on the KILL command syntax?Johnjohn.newman@morcinc.org |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-09-09 : 16:35:06
|
| Dynamic SQL:http://www.sqlteam.com/item.asp?ItemID=4599http://www.sqlteam.com/item.asp?ItemID=4619You can build a dynamic SQL string that does not require a cursor at all, but will still include all of the SPIDs to be killed. It will be a lot easier to do it that way than to use dynamic SQL with a cursor. |
 |
|
|
|
|
|