0

我正在尝试获取特定行的列的值,但出现以下错误。我不知道有什么问题。这是我的sql查询:

declare @Service_Table varchar(30),
        @Ser_Id tinyint,
        @sql  nvarchar(100), 
        @Service_State tinyint

SET @Service_Table='Third_Party_Assistance'
set @Ser_Id = 2
SET @sql = 'SELECT ' + @Service_State + '=  Service_State_Id FROM dbo.' + 
          quotename(@Service_Table) + ' WHERE Service_Id = ' + 
          convert(varchar,@Ser_Id) + 'AND Service_State_Id <> 4'
EXEC sp_executesql @sql
4

3 回答 3

1

您没有为 @Service_State 变量分配任何值!

于 2013-03-25T08:58:22.850 回答
1

使用带 OUTPUT 参数的sp_executesql系统存储过程

declare @Service_Table varchar(30),
        @Ser_Id tinyint,
        @sql  nvarchar(4000), 
        @Service_State tinyint

SET @Service_Table='Third_Party_Assistance'
SET @Ser_Id = 2
SET @sql = 'SELECT @Service_State =  Service_State_Id FROM dbo.' + 
            quotename(@Service_Table) + ' WHERE Service_Id = ' +
            '@Ser_Id AND Service_State_Id <> 4'
EXEC sp_executesql @sql, N'@Ser_Id tinyint, @Service_State tinyint OUTPUT', @Ser_Id, @Service_State OUTPUT
SELECT @Service_State
于 2013-03-25T09:22:36.557 回答
0

你不是已经问过这个问题了吗?声明在哪里?

尝试投射@Service_State:cast(@Service_State) as varchar(255))

declare @Service_Table varchar(30),
        @Ser_Id tinyint,
        @sql  nvarchar(30), 
        @Service_State tinyint

SET @Service_Table='Third_Party_Assistance'
set @Ser_Id = 2
SET @sql = 'SELECT ' + cast(@Service_State as varchar(255))+ '=  Service_State_Id FROM dbo.' + 
          quotename(@Service_Table) + ' WHERE Service_Id = ' + 
          convert(varchar,@Ser_Id) + 'AND Service_State_Id <> 4)'
于 2013-03-25T08:54:56.260 回答