Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure Incorrect syntax

Status
Not open for further replies.

fogal

Technical User
Aug 20, 2001
87
US
I am trying to run the following stored procedure. When I click check syntax, I get the error message Incorrect syntax near keyword begin, Incorrect syntax near keyword select

What am i doing wrong ???

declare @spid int, @sql varchar(1000)
select @spid = 10
while exists (select * from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,getdate())
begin
select @spid = min(spid) from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,getdate()
select @sql = 'kill ' + convert(varchar(20),@spid)
exec (@sql)
end
 
you're missing two closing brackets (in bold below)

declare @spid int, @sql varchar(1000)
select @spid = 10
while exists (select * from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,getdate()))
begin
select @spid = min(spid) from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,getdate())
select @sql = 'kill ' + convert(varchar(20),@spid)
exec (@sql)
end
 
replace your call to getdate() with a variable as shown below

declare @spid int, @sql varchar(1000)
select @spid = 10
declare @v_today datetime
set @v_today = getdate()
while exists (select * from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,@v_today))
begin
select @spid = min(spid) from master..sysprocesses where spid > @spid and login_time > dateadd(dd,-1,@v_today )
select @sql = 'kill ' + convert(varchar(20),@spid)
exec (@sql)


That should do the job.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top