【问题标题】:Msg 203, Level 16, State 2, is not a valid identifier消息 203,级别 16,状态 2,不是有效标识符
【发布时间】:2012-11-03 04:05:17
【问题描述】:

我收到以下错误:

消息 203,级别 16,状态 2,过程 getQuestion,第 18 行
名称 '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)' 不是有效标识符

来自以下存储过程:

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 sql-server sql-server-2008 tsql error-handling


    【解决方案1】:

    请试试这个,把执行@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
    

    【讨论】:

    • 为什么我们需要写exec(@query) 为什么exec @query 不起作用
    • 不确定,但我敢打赌它与 + 符号的连接有关!!
    • 你是救生员。我整个星期都在用头撞墙。
    【解决方案2】:

    问题是execute @query。我可以在测试后确认。 @techdo 是正确的。改成

    execute (@query)
    

    【讨论】:

      猜你喜欢
      • 2016-08-12
      • 2020-11-11
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多