6

I'm getting the following error:

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

from the following stored procedure:

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

24

Please try this, changed execute @query to execute (@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
TechDo
  • 18,398
  • 3
  • 51
  • 64
2

The problem is execute @query. I can confirm that after testing it out. @techdo was correct. Change it to

execute (@query)
codingbiz
  • 26,179
  • 8
  • 59
  • 96