ALTER PROCEDURE [dbo].[testProc]
AS
BEGIN
declare @test int;
     select count(*) as c into test from table2;
print @test

END就定义了一个test变量,第一次执行存储过程的时候好的,第二次以后就不行了,说test变量已存在。。为什么?换了其他的变量名同样
我直到零时表可以drop table,变量怎么办哪?

解决方案 »

  1.   

    ALTER PROCEDURE [dbo].[testProc]
    AS
    BEGIN
    if exists(select 1 from sysobjects where xtype='U' and name='test')
    drop table test
    declare @test int;
         select count(*) as c into test from table2;
    print @test

    END
      

  2.   

    语句写的也有问题
    select count(*) as c into test from table2
    这句是将结果存到test表中,但实际@test变量根本没接收到值
    可以改成
    ALTER PROCEDURE [dbo].[testProc]
    AS
    BEGIN
    declare @test int;
         select @test=count(*) from table2;
    print @test

    END