SQL BEGIN AND END BLOCK ISSUES/SYNTAX
Posted on 2014-09-03
I have the following WHILE loop for my cursor... this is just the WHILE block...
Trouble I am having is with BEGIN and END blocks... need to nest them, or another solution if someone wants to reorg this snippet. I have lots of stored procs for various TSQL parsing, updating, etc., etc...
This is a one time only situation I am working on, I may incorporate into a stored proc at a later time, but not at the immediate.
Don't like nexting BEGIN END blocks like this, but I am half way there. I've gone back and forth with nesting a BEGIN/END TRANSACTION within a BEGIN/END block.. See in bold below.
WHILE CHARINDEX(':', @valueList, @pos+1)>0
set @len = CHARINDEX(':', @valueList, @pos+1) - @pos
set @value = SUBSTRING(@valueList, @pos, @len)
IF @counter > 0
set @counter = 0
" Right here when the counter is > 0, I want to place SQL update logic.
"i.e. Update tableA
" SET columnXYZ = @value
set @pos = CHARINDEX(':', @valueList, @pos+@len) +1
set @counter = @counter + 1
FETCH NEXT FROM tableA_cursor
INTO @supplier, @valueList