sql - 消息 203,级别 16,状态 2,不是有效标识符

标签 sql sql-server sql-server-2008 tsql error-handling

我收到以下错误:

Msg 203, Level 16, State 2, Procedure getQuestion, Line 18
The name 'select top(1) * from tlb_Question inner join tlb_options on tlb_options.qID=tlb_Question.id and tlb_Question.qNumber=1 and tlb_Question.id not in (0,1)' is not a valid identifier

来自以下存储过程:

ALTER proc getQuestion
    @qNo bigint,
    @total bigint,
    @next nvarchar(max)
as
begin 
    declare @hisa bigint
    set @hisa=@total/3

    if(@qNo<=@total/3)
    begin
    declare @query nvarchar(max)
    set @query=('select top(1) * from tlb_Question 
        inner join tlb_options on tlb_options.qID=tlb_Question.id and tlb_Question.qNumber=1 and tlb_Question.id not in ('+cast(@next as varchar)+')')
    print @query
    execute @query
    end
end

最佳答案

请尝试一下,将执行@query更改为执行(@query):

ALTER proc getQuestion
    @qNo bigint,
    @total bigint,
    @next nvarchar(max)
as

begin 
    declare @hisa bigint
    set @hisa=@total/3

    if(@qNo<=@total/3)
    begin
      declare @query nvarchar(max)
      set @query=('select top(1) * from tlb_Question 
      inner join tlb_options on tlb_options.qID=tlb_Question.id and tlb_Question.qNumber=1 and tlb_Question.id not in ('+cast(@next as varchar)+')')
      --print @query
      execute (@query)
    end
end

关于sql - 消息 203,级别 16,状态 2,不是有效标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205827/

相关文章:

sql-server-2008 - insert exec 语句不能嵌套

sql - 将 CTE 与分层数据和 'cumulative' 值结合使用

sql-server - 将 Sql 中的行数转换为列格式

SQL SERVER 2014 代码片段丢失

sql - 选择 A 列相同但 B 列不同的行

sql - 在 Microsoft Visual Basic 6.0 中填充组合框

mysql - 在另一列上使用 LIKE

sql - 帮助将 SQL 嵌套子查询转换为 mysql 4.0 兼容性的连接查询

sql - 我安装了哪个版本的 tSQLt(数据库测试框架)?

sql-server - 如何获取 XML 节点中的 XML 属性列表