Msg 203, Level 16, State 2, is not a valid identifier

I get the following error:

Msg 203, Level 16, State 2, GetQuestion Procedure, 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 
+4
source share
2 answers

Try this, change 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 
+16
source

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

 execute (@query) 
+1
source

Source: https://habr.com/ru/post/1443771/


All Articles