我的存储过程代码如下:if(@CaId=-111 and @DaId=-111)
begin
Declare @strSql3 nvarchar(1500)
set @strSql3='select '+ @cId+'=Count(CoId) from House'
+@st
exec (@strSql3)
end
可运行时老提示:
在将 varchar 值 'select ' 转换成数据类型 int 时失败
我该如何正确写?

解决方案 »

  1.   

    set @strSql3='select '+ CAST(@cId AS varchar(20) + '=Count(CoId) from House' + @st
      

  2.   

    if(@CaId=-111 and @DaId=-111)
    begin
    Declare @strSql3 nvarchar(1500)
    set @strSql3='select '+ convert(varchar(10),@cId)+'=Count(CoId) from House'
    +@st
    exec (@strSql3)
    end
      

  3.   

    if(@CaId=-111 and @DaId=-111)
    begin
    Declare @strSql3 nvarchar(1500)
    set @strSql3='select @cId=Count(CoId) from House'+@st
    exec sp_execute @strSql3, N'@cId int out', @cId output
    --exec (@strSql3)
    end
      

  4.   

    if(@CaId=-111 and @DaId=-111)
    begin
    Declare @strSql3 nvarchar(1500)
    set @strSql3='select @cId=Count(CoId) from House'+@st
    exec sp_executesql @strSql3, N'@cId int out', @cId output

    --exec (@strSql3)
    end
      

  5.   

    用4楼方法,却提示“过程需要类型为 'int' 的参数 '@handle'”,如何解决?ALTER PROCEDURE [dbo].[getCount]
    (@cId int output,
     @st nvarchar(500),
     @La nvarchar(50),
     @CaId int,
     @DaId int
    )

    ASif(@CaId=-111 and @DaId=-111)
    begin
    Declare @strSql3 varchar(1500)
    set @strSql3='select @cId=Count(CoId) from House'
    exec sp_execute @strSql3, N'@cId int out', @cId output
    end
      

  6.   

    对不起,3楼写错了 sp_execute, 4楼改正了 sp_executesql