BEGIN-END Structure

  • I have an IF statement with "BEGIN" structures such as below. I need help on the FIRST "BEGIN/END" structure.

    IF exists (select * from #HelpProtect where hpObject = @SysObjectTest)

    BEGIN

    print 'the currect record is good, read next record'

    END

    ELSE

    BEGIN

    INSERT INTO MyTableName values (@SysObjectTableName)

    END

    FETCH NEXT FROM TableName_cursor INTO @SysObjectTableName

    If I comment out the "print 'the currect record is good, read next record' " statement I get syntax errors.

    In my IF statement I want to test for a POSITIVE condition.

    So, what can I put between the "BEGIN" & "END" that does essentially nothing, but will appease the syntax Gods?

  • I've used

    DECLARE @DUMMY int

    and that works.

    You could also use IF NOT EXISTS and skip the else completely...

  • Okay, why not just test for a negative? Or reverse the logic?

    If you really need a statement that does nothing at all, you could assign a variable to itself, or something of that sort. But what you're doing then is creating confusion for whoever has to maintain that procedure next. A year from now, will it make sense?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks Chad ! That worked !

  • Where do @SysObjectTest and @SysObjectTableName come from? If they both come from data in another table, then you may be able to do this using a join rather than using a cursor/RBAR...

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply