views:

34

answers:

3

Hi

I was playing around with a stored procedure and I think I made an endless while loop(I think I forgot to close the cursor)

now it is sending me emails and I don't know how to stop it. How can I kill it?

edit this is what I see when I do what remus told me to do

Sorry I don't know how to output the results nicer.

1,0,2010-07-06 17:00:45.637,background,RESOURCE MONITOR,NULL,NULL,NULL,NULL,0,1,NULL,0,NULL,0,SLEEP_TASK,,0,1,0,NULL,0,0,0,0,257,0x04848118,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
2,0,2010-07-06 17:00:45.637,background,LAZY WRITER,NULL,NULL,NULL,NULL,0,1,NULL,0,LAZYWRITER_SLEEP,904,LAZYWRITER_SLEEP,,0,1,0,NULL,0,0,0,0,1,0x0072C3E8,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
4,0,2010-07-06 17:00:45.637,background,LOCK MONITOR,NULL,NULL,NULL,NULL,0,1,NULL,0,REQUEST_FOR_DEADLOCK_SEARCH,4570,REQUEST_FOR_DEADLOCK_SEARCH,,0,1,0,NULL,0,0,0,0,1,0x0072C4D8,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
5,0,2010-07-06 17:00:45.637,background,SIGNAL HANDLER,NULL,NULL,NULL,NULL,1,1,NULL,0,KSOURCE_WAKEUP,12587720,KSOURCE_WAKEUP,,0,1,0,NULL,0,0,0,0,0,0x002582F8,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
6,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
7,0,2010-07-06 17:00:45.637,background,TRACE QUEUE TASK,NULL,NULL,NULL,NULL,1,1,NULL,0,SQLTRACE_BUFFER_FLUSH,3213,SQLTRACE_BUFFER_FLUSH,,0,1,0,NULL,0,0,0,0,1,0x0072C7A8,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
9,0,2010-07-06 17:00:45.637,background,BRKR TASK,NULL,NULL,NULL,NULL,1,1,NULL,0,BROKER_TRANSMITTER,12588032,BROKER_TRANSMITTER,,0,1,0,NULL,0,0,0,0,1,0x0072CA78,0,0,0,4096,us_english,mdy,7,1,0,1,0,1,1,1,1,2,-1,0,0,0,1,0,0
11,0,2010-07-06 17:00:45.637,background,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,ONDEMAND_TASK_QUEUE,12590216,ONDEMAND_TASK_QUEUE,,0,1,0,NULL,0,0,0,0,0,0x002583E8,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
12,0,2010-07-06 17:00:45.637,background,BRKR EVENT HNDLR,NULL,NULL,NULL,NULL,1,1,NULL,0,BROKER_EVENTHANDLER,337289,BROKER_EVENTHANDLER,,0,1,0,NULL,0,0,1170,0,0,0x002586B8,6,43,154502,4096,us_english,mdy,7,1,0,1,0,1,1,1,1,2,-1,0,0,0,1,0,0
13,0,2010-07-06 17:00:45.637,background,BRKR TASK,NULL,NULL,NULL,NULL,1,1,NULL,0,BROKER_TRANSMITTER,12588017,BROKER_TRANSMITTER,,0,1,0,NULL,0,0,0,0,0,0x002585C8,0,0,0,4096,us_english,mdy,7,1,0,1,0,1,1,1,1,2,-1,0,0,0,1,0,0
14,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,1413,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
15,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,101,11396,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
16,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,146,15318,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
17,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,235,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
18,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,476,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
19,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,711,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
20,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,506,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
21,0,2010-07-06 17:00:45.637,sleeping,TASK MANAGER,NULL,NULL,NULL,NULL,1,1,NULL,0,NULL,0,MISCELLANEOUS,,0,1,0,NULL,0,0,0,0,NULL,0x00000000,0,0,0,4096,us_english,mdy,7,0,0,0,0,0,0,0,0,2,-1,0,0,0,1,0,0
60,0,2010-07-06 20:30:33.360,running,SELECT,0x020000004D4F6005A3E8119F3DD3297095832ABE63E312F2,0,-1,0x060005004D4F6005B801000F000000000000000000000000,5,1,5758612B-D19E-43EB-B9A5-343B30DCA65C,0,NULL,0,MISCELLANEOUS,,0,1,2034480,0x,0,0,0,1,0,0x002595B8,0,0,99,2147483647,us_english,mdy,7,1,1,1,0,1,1,1,1,2,-1,0,19,0,0,0,0

Edit 2

1   0   2010-07-06 20:38:22.550 background  RESOURCE MONITOR    NULL    NULL    NULL    NULL    0   1   NULL    0   NULL    0   SLEEP_TASK      0   1   0   NULL    0   0   0   0   257 0x048A8118  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
2   0   2010-07-06 20:38:22.550 background  LAZY WRITER NULL    NULL    NULL    NULL    0   1   NULL    0   LAZYWRITER_SLEEP    561 LAZYWRITER_SLEEP        0   1   0   NULL    0   0   0   0   1   0x0070C3E8  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
4   0   2010-07-06 20:38:22.550 background  LOCK MONITOR    NULL    NULL    NULL    NULL    0   1   NULL    0   REQUEST_FOR_DEADLOCK_SEARCH 2792    REQUEST_FOR_DEADLOCK_SEARCH     0   1   0   NULL    0   0   0   0   1   0x0070C4D8  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
5   0   2010-07-06 20:38:22.550 background  SIGNAL HANDLER  NULL    NULL    NULL    NULL    1   1   NULL    0   KSOURCE_WAKEUP  282252  KSOURCE_WAKEUP      0   1   0   NULL    0   0   0   0   0   0x004F82F8  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
6   0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
7   0   2010-07-06 20:38:22.550 background  TRACE QUEUE TASK    NULL    NULL    NULL    NULL    1   1   NULL    0   SQLTRACE_BUFFER_FLUSH   3088    SQLTRACE_BUFFER_FLUSH       0   1   0   NULL    0   0   0   0   1   0x0070C7A8  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
9   0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  6   7   5360    4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
10  0   2010-07-06 20:38:22.550 background  TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   ONDEMAND_TASK_QUEUE 286683  ONDEMAND_TASK_QUEUE     0   1   0   NULL    0   0   0   0   0   0x004F83E8  0   0   0   4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
12  0   2010-07-06 20:38:22.550 background  BRKR EVENT HNDLR    NULL    NULL    NULL    NULL    1   1   NULL    0   BROKER_EVENTHANDLER 1326    BROKER_EVENTHANDLER     0   1   0   NULL    0   0   1045    0   0   0x004F86B8  6   277 53717   4096    us_english  mdy 7   1   0   1   0   1   1   1   1   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
13  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  4   0   2830    4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
14  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   0   276 4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
15  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   0   4716    4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
16  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   0   2346    4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
17  0   2010-07-06 20:38:22.550 background  BRKR TASK   NULL    NULL    NULL    NULL    1   1   NULL    0   BROKER_TRANSMITTER  282237  BROKER_TRANSMITTER      0   1   0   NULL    0   0   0   0   0   0x004F8898  0   0   0   4096    us_english  mdy 7   1   0   1   0   1   1   1   1   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
18  0   2010-07-06 20:38:22.550 background  BRKR TASK   NULL    NULL    NULL    NULL    1   1   NULL    0   BROKER_TRANSMITTER  282237  BROKER_TRANSMITTER      0   1   0   NULL    0   0   0   0   1   0x0070CC58  0   0   0   4096    us_english  mdy 7   1   0   1   0   1   1   1   1   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
19  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   3   3649    4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
20  0   2010-07-06 20:38:22.550 sleeping    TASK MANAGER    NULL    NULL    NULL    NULL    1   1   NULL    0   NULL    0   MISCELLANEOUS       0   1   0   NULL    0   0   0   0   NULL    0x00000000  0   0   804 4096    us_english  mdy 7   0   0   0   0   0   0   0   0   2   -1  0   0   0   1   0   0   NULL    NULL    NULL    NULL    NULL
55  0   2010-07-06 20:43:04.807 running SELECT  0x02000000C11D0A09F92E04D1EE8FD14B7EC06C01E9266CA8  0   -1  0x06000500C11D0A09B881F207000000000000000000000000  5   1   710FE4B3-5EB3-47FD-839C-81B9CD33E56A    0   NULL    0   MISCELLANEOUS       0   1   85928   0x  0   0   0   4   0   0x004F8988  6   0   66  2147483647  us_english  mdy 7   1   1   1   0   1   1   1   1   2   -1  0   18  0   0   0   0   NULL    NULL    NULL    0   

Edit 3

This was the stored procedure

I only had one record in the temp table

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



ALTER PROCEDURE [dbo].[csp_test]  
AS
BEGIN



declare @temp2 table (
    idx int identity(1,1),
    title varchar(max),
    body varchar(max))

insert into @temp2 (title, body)
select title, body
from temp

declare @title varchar(max)
declare @body varchar(max)

declare myCursor Cursor for select title, body from @temp2


open myCursor

fetch next from myCursor into @title, @body
while @@fetch_status = 0 begin

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SqlAlerts',
    @recipients = '[email protected]',
    @body = 'The stored procedure finished successfully.',
    @subject = 'Automated Success Message' ;


end


END
+2  A: 

KILL <spid>; where is the @@SPID of the spinning procedure. Use select * from sys.dm_exec_requests to identify the @@SPID (session_id) if you don't know it.

Update

Yeap, that is an infinite loop. You're missing a FETCH inside the WHILE loop:

fetch next from myCursor into @title, @body 
while @@fetch_status = 0 begin 
  EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'SqlAlerts', 
    @recipients = '[email protected]', 
    @body = 'The stored procedure finished successfully.', 
    @subject = 'Automated Success Message' ; 

   // Add this line:  
   fetch next from myCursor into @title, @body 
end 
Remus Rusanu
nice and simple +1
Michael Haren
Hmm I am not sure which one it is. It was called csp_test but I can't see it. I only see one running and it told me I can't kill my own process.
chobo2
Use this query: `select * from sys.dm_exec_requests r outer apply sys.dm_exec_sql_text(r.sql_handle) t`. This will also project the actual statement being executed. From what you posted there, it doesn't look like there is any looping procedure, btw.
Remus Rusanu
I will post you what that resulted. It looks almost the same. Well I don't know. I ran that stored procedure now every second my gmail gets like 10 new messages with the exact same msg I told it to send. So I don't know how to stop it.
chobo2
You don't have any looping procedure executing. There must be something else that is sending those mails. A trigger perhaps?
Remus Rusanu
Don't know I just have the database mail account that I set up. I posted my stored procedure. I only setup database mail profile and that's it. I did not setup any triggers.
chobo2
Try `KILL 55;`, but I suspect that is the SELECT from sys.dm_.. itself. You sure you execute it on the right server? Anyway, your procedure needs to FETCH inside the loop, see my update.
Remus Rusanu
Ya I think my things needs alot of things but first I need to stop this. Ya I tired that but it won't let me kill it. I just don't know where to look anymore it just keeps sending emails and I don't know why. I deleted the profile, I deleted the datbase table I tried to do sp_stop_dbmail but nothing works.
chobo2
The procedure has robably stopped. Righ now, all those mails send are in transit. Just wait untill it drains. How many could they be? 1M, 100M? Delete your inbox every now and then...
Remus Rusanu
Well I just made a filter to send them right to the trash can. I would like to contact google and tell them about this but I can't find an email address to send to them. It seems like a huge flaw that I can send email after email and it's like they don't care. They make it next to impossible to find how to contact them and tell them these problems.
chobo2
Hopefully it stops. From now on I am making a garbage account that I don't care. Knowing google they probably will try to ban me or something.
chobo2
@chobo2 - they may eventually throttle you or blacklist you.
Cade Roux
+1  A: 

Find your rogue connection with the activity monitor and kill it. Or if it's an emergency, just recycle the SQL Server service.

Michael Haren
or what @Remus said--that's much simpler
Michael Haren
A: 

I'd just cycle the SQL Server service, but this is what you need to do to fix your code before you test again:

open myCursor

fetch next from myCursor into @title, @body
while @@fetch_status = 0 begin

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'SqlAlerts',
        @recipients = '[email protected]',
        @body = 'The stored procedure finished successfully.',
        @subject = 'Automated Success Message' ;

    -- Need another fetch here - you are never attempting to seek to the next row
end

-- need CLOSE myCursor here
-- need DEALLOCATE myCursor here
Cade Roux
Ya I know I forgot to grab that from the tutorial. How do I cycle the server?
chobo2
@chobo2 Go to control panel - Administrative tools - Services - find the SQL Server Service - restart it.
Cade Roux
Ok I restarted "SQL Server (SQLSERVER2005) service" but the emails keep coming.
chobo2