Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I may be missing something, but why doesn't this bit work outside the cursor if you take out the @QuestionId from the where clause?:</p> <pre><code> --check if @isMulti is true or not if(@isMulti=1) begin update tblAnswers set col1 = 1, col2 = 1, col3 = (select count(*) from tblAnswers where ID=@ID) end else if(@isMulti=0) begin update tblAnswers set col1 = AnswerID, col2 = 1, col3 = (select LEN(count(*)) from tblAnswers where ID=@ID) end </code></pre> <p><strong>EDIT</strong></p> <p>Without knowing more about the metadata I'm not sure about how to process the multi element to the questions, but this should be a good way to the answer:</p> <pre><code>declare @question table (questionid int, multi int) declare @answer table (answerid int, col1 int, col2 int, col3 int) insert into @question (questionid, multi) values (1, 0) insert into @question (questionid, multi) values (2, 0) insert into @question (questionid, multi) values (3, 0) insert into @question (questionid, multi) values (4, 1) insert into @question (questionid, multi) values (5, 1) insert into @answer (answerid, col1, col2, col3) values (1, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (1, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (2, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (2, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (3, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (4, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (4, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (4, 0, 0, 0) insert into @answer (answerid, col1, col2, col3) values (5, 0, 0, 0) update @answer set col1 = 1, col2 = 1, col3 = (select count(*) from @answer a join @question q on a.answerid = q.questionid where q.multi = 0 and [@answer].answerid = a.answerid) select distinct * from @answer </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload