tsql - How to assign variable if Cursor returns Bit of 0 -
i have written cursor search through table looking @ 1 bit value. if values 1, send email. if 1 value 0 in row, don't send email. issue having comes in if statement. in ssms, "@iscomplete = 0" breaking "incorrect syntax" error. not sure missing here. code below. thank you.
------------------------------------------------- -- start inner cursor -- ------------------------------------------------- declare @complete int declare @iscomplete bit = 1 declare innercur cursor select complete #aaeapvs open innercur fetch next innercur @complete while @@fetch_status = 0 begin if @complete = 0 begin @iscomplete = 0 end fetch next innercur @complete end close innercurs deallocate innercurs ------------------------------------------------- -- inner curser end -- -------------------------------------------------
the incorrect syntax need use set assign variable value, change
@iscomplete = 0
to
set @iscomplete = 0
and assuming want exit find not complete should change while condition to
while @@fetch_status = 0 , @iscomplete = 1
but important of all, don't need use cursor @ - should avoid cursors in sql if possible. can
declare @iscomplete bit = 1 if exists (select * #aaeapvs complete = 0) begin set @iscomplete = 0 end
and more need, can in single statement
declare @iscomplete bit = 1 select @iscomplete = 0 #aaeapvs complete = 0
Comments
Post a Comment