是这样.在存储过程里有这样一个语句:select @ct=count(*) from tb where cID=@cID 这样可以得到@ct数,以便在下面的语句上用.
那么我想换成这样的形式(用单引号引住的)
set @ct='select count(*) from tb where cID'+@cID  //这句sql语句肯定是错的,只是想要这种形式的.怎么说呢.我就是想要执行一条sql语句,然后得到一个@ct的值.不晓得说得明不明白.

解决方案 »

  1.   

    set @ct=exec('select count(*) from tb where cID')+@cID 
      

  2.   

    declare @cID varchar(80)
    declare @ct varchar(80)
    set @ct =(select count(*) from tb where cID=@cID )
    select @ct
      

  3.   


    不行呢.说"exec附近有语法错误"PS:
    之类的写错了.少了个等号(=)
    set @ct='select count(*) from tb where cID='+@cID //这句sql语句肯定是错的,只是想要这种形式的.
      

  4.   


    直接引用@@rowcount 不行吗?
      

  5.   

    嗯.接近了.如果这个tb表,是一个前面定义好的tb nvarchar(10),表示一个表名.如果写呢?
    set @ct =(select count(*) from @tb where cID=@cID )
    这样?好像不行.
      

  6.   


    有可能我用的不是count(*),有可能是一个字段名比如select [name] from tb where cID.
      

  7.   

    declare @SQLString nvarchar(200)
    declare @where nvarchar(200)
    declare @column nvarchar(200)
    declare @table nvarchar(200)
    set @table ='t1'
    set @where ='where f.a=1'
    set @column ='a'
    declare @ParmDefinition nvarchar(200)
    SET @SQLString =
         N'SELECT @ResultNUM='+@column+' from '+@table+' f  '+@where;
    SET @ParmDefinition = N'@ResultNUM int output';
    DECLARE @ResultNUM INT;
    EXECUTE sp_executesql @SQLString, @ParmDefinition   ,@ResultNUM   output       
    SELECT @ResultNUM
      

  8.   

    你的疑惑请查看msdn 
    sp_executesql
      

  9.   


    我是想这样
    1,set @ct =(select cd from @tb where cID=@cID )//这个@tb表是前面定义的表名@tb nvarchar(10)
    2,然后用EXECUTE sp_executesql执行@ct
    3,得到一个@ct的值(这个值是cd字段里的)
    4,然后我要用@ct这个值来set @strSql='......where cd='+@ct
    5,最后就是 EXECUTE @strSql过程就这样.我现在想问的就是这个@ct在第1,2,3.这三步.谢谢你回复.
      

  10.   

    存储过程为什么这样
    select   @strSql   =   'select   cd  from  '+@tb+ ' where cID='''+@cID +''''
    exec(@strSql)
    会报:
    where cID='' 转换为数据类型为 int 的列时发生语法错误。
    的错误呢?
    哪里错了?请指正.