create procedure sp_killuser
       @dbname char(20)  = 'hqs'
as
declare  @spidlow int,
 @spidhigh int,
 @spid smallint
select  @spidlow =     0,
 @spidhigh = 32767
declare  c_s_spid cursor for
  select   spid
  from     master.dbo.sysprocesses
  where    spid >= @spidlow and spid <= @spidhigh and dbid<>0 and db_name(dbid) = dbname
open  c_s_spid
while(0=0) begin
    fetch next from c_s_spid into @spid
    if(@@fetch_status <> 0 ) break
    exec('kill '+ @spid)
end 
close c_s_spid
deallocate c_s_spid

解决方案 »

  1.   

    create procedure sp_killuser
           @dbname char(20)  = 'hqs'
    as
    declare  @spidlow int,
     @spidhigh int,
     @spid smallint
    select  @spidlow =     0,
     @spidhigh = 32767
    declare  c_s_spid cursor for
      select   spid
      from     master.dbo.sysprocesses
      where    spid >= @spidlow and spid <= @spidhigh and dbid<>0 and db_name(dbid) = dbname
    open  c_s_spid
    while(0=0) begin
        fetch next from c_s_spid into @spid
        if(@@fetch_status <> 0 ) break
        exec('kill '+ rtrim(@spid))  -- 最好将后面的空格去掉
    end 
    close c_s_spid
    deallocate c_s_spid
      

  2.   

    上面语句中有db_name(dbid) = dbname
    此处的db_name是自己定义的一个函数吗?
      

  3.   

    To 飞马, DB_NAME是SQL Server的函数