Results 1 to 12 of 12

Thread: [RESOLVED] SPROC - Conditional Branching

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] SPROC - Conditional Branching

    Is there a better way of branching other that this:

    VB Code:
    1. DECLARE @Type INT
    2. SET @Type = 2
    3.  
    4. If @Type = 1
    5.     PRINT 'Number 1'
    6. If @Type = 2
    7.     PRINT 'Number 2'
    8. If @Type = 3
    9.     PRINT 'Number 3'
    10. If @Type = 4
    11.     PRINT 'Number 4'

    I need this to work on the following db: MS SQL 7.0, MS SQL 2000, MS SQL 2K5 and possibly MySQL 4.1.

    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SPROC - Conditional Branching

    You can try:

    Code:
    DECLARE @Type INT
    SET @Type = 2
    
    SELECT CASE @Type
    	WHEN 1 THEN 'Number 1'
    	WHEN 2 THEN 'Number 2'
    	WHEN 3 THEN 'Number 3'
    	WHEN 4 THEN 'Number 4'
    	WHEN 5 THEN 'Number 5'
    	ELSE 'Something Else'
    END

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SPROC - Conditional Branching

    @sevenhalo - SELECT CASE is a SELECT statement - not a branch statement.

    IF/blocks create logic path flow in the SPROC...

    CASE/WHEN statements can only be put into a SELECT statement for altering data return.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: SPROC - Conditional Branching

    Just curious, couldn't you place it into a cursor and print the value from there?

    ---Edit-----------
    Ohh, nvm... NOW I see what he's asking. Forget what I said.

  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SPROC - Conditional Branching

    So I guess I can only way for me to Conditional Branch is to use the example that I posted?

    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SPROC - Conditional Branching

    There are several flavors of IF/blocks in T-SQL

    VB Code:
    1. If @Type=1 Print 'Got a 1' -- This is a single line If - the default
    2.  
    3. If @Type=1
    4.         Print 'Got a 1'
    5. Else    Print 'Not a 1' -- This is a single line If with Else
    6.  
    7. -- When you need several lines in the IF you need to make your own "blocks"
    8.  
    9. If @Type=1
    10. Begin
    11.   Print 'Got a 1'
    12.   Print 'This is the second line of Got a 1'
    13. End
    14. Else
    15. Begin
    16.   Print 'Not a 1'
    17.   Print 'This is the second line of Not a 1'
    18. End
    Whenever I start needing anything more then the default single line If or If/Else I start using the Begin/End blocks. If can have many Else layers within the Begin/End blocks - it's just a little overwhelming for the eyes with all those Begin/End's...

    Here's an example of a SPROC that got pretty complex...

    VB Code:
    1. If @SD<@MemWelStart and @MemWelStart is not null
    2. Begin
    3.     Set @SD=@MemWelStart
    4.     Set @SRsn=@SRsn+'A'     -- Start prior to WELSTART (member)
    5. End
    6.  
    7. If @ED>@MemWelEnd and @MemWelEnd is not null
    8. Begin
    9.     Set @ED=@MemWelEnd
    10.     Set @ERsn=@ERsn+'B'     -- End after WELEND (member)
    11. End
    12.  
    13. If @MAffil<>'1'
    14. Begin
    15.     If @SD<@MasWelStart and @MasWelStart is not null
    16.     Begin
    17.         Set @SD=@MasWelStart
    18.         Set @SRsn=@SRsn+'C' -- Start prior to WELSTART (patient)
    19.     End
    20.  
    21.     If @ED>@MasWelEnd and @MasWelEnd is not null
    22.     Begin
    23.         Set @ED=@MasWelEnd
    24.         Set @ERsn=@ERsn+'D' -- End after WELEND (patient)
    25.     End
    26. End
    27.  
    28. If @MAffil='3' and @WelHandi<>'Y'
    29. Begin
    30.     Set @DepRsn=''
    31.     Set @ColEnd=null
    32.  
    33.     If @EOM19<@SD
    34.     Begin
    35.         Set @SRsn=@SRsn+'I'
    36.         Set @ColStart=(Select Min(CStart) From @WelCollege Where @SD<=CEnd)
    37.         If @ColStart is Null or @ColStart>@SD
    38.         Begin
    39.             Set @SD=@ColStart
    40.         End
    41.         Set @DepEnd=(Select Min(CEnd) From @WelCollege Where CStart=@SD)
    42.         Set @DepRsn=IsNull((Select Top 1 CRsn From @WelCollege Where CStart=@SD and CRsn<>''),'G')
    43.         If @DepEnd is Null
    44.         Begin
    45.             Set @DepEnd=(Select Min(CEnd) From @WelCollege Where CStart=@ColStart)
    46.             Set @DepRsn=IsNull((Select Top 1 CRsn From @WelCollege Where CStart=@ColStart and CRsn<>''),'G')
    47.         End
    48.     End
    49.     Else
    50.     Begin  
    51.         Set @DepRsn='E'     -- Reached Age 19 and not in school
    52.         Set @DepEnd=@EOM19  -- We are considering cutting off at age 19
    53.         Set @ColEnd=(Select Top 1 CEnd From @WelCollege Where DateAdd(dd,-1,CStart)<=@DepEnd and CEnd>@SD Order by CStart Desc)
    54.     End
    55.  
    56.     -- Let's see if we have a college start date that will give us a new cut off date
    57.     If @ColEnd is not null
    58.     Begin
    59.         If @ColEnd>@DepEnd
    60.         Begin
    61.             Set @DepEnd=@ColEnd
    62.             Set @DepRsn='G'     -- College end date reached
    63.         End
    64.     End
    65.  
    66.     If @ED>@DepEnd
    67.     Begin
    68.         Set @ED=@DepEnd
    69.         Set @ERsn=@ERsn+@DepRsn
    70.         If @ED>@EOM23
    71.         Begin
    72.             Set @ED=@EOM23
    73.             Set @ERsn=@ERsn+'J'
    74.         End
    75.         Else
    76.         Begin
    77.             Update @EligHrs Set EEnd=@ED Where EStart=@OrigSD
    78.             Insert into @EligHrs Select DateAdd(dd,1,@ED),@OrigED,@EElig,@EGrp,@EGrpOver,@ESRsn,@EERsn,@EEWho,''
    79.         End
    80.     End
    81. End

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] SPROC - Conditional Branching

    Steve,

    Thanks again. Since I introduced SPROCS into my app db access has been cut in half.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SPROC - Conditional Branching

    Quote Originally Posted by Mark Gambo
    Steve,

    Thanks again. Since I introduced SPROCS into my app db access has been cut in half.
    That is a really great piece of information - I wonder how many people, if they knew this, would finally abandon in-line SQL statements!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] SPROC - Conditional Branching

    Quote Originally Posted by szlamany
    That is a really great piece of information - I wonder how many people, if they knew this, would finally abandon in-line SQL statements!

    Too bad they don't work with MS Access DB , I have a few clients that would benefit greatly. I am trying to get them to upgrade to at least MySQL but they like the Access enviorment. Thats ok three of them have DB at about the size of 1.5 Gig. Oh boy I can't wait until I get the call:

    Quote Originally Posted by Mark's Client
    "Hey Mark, we can't access the data in our database, what could be the problem?"
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] SPROC - Conditional Branching

    Steve,
    Seriously, how can I design a SPROC in order to prevent a SQL Injection Attack? What I am currently doing now is analyzing the user's input prior to sending the SQL to DB (Searching for the keywords: INSERT, DELETE, Semi-Colons, 1=1, etc.). Is there a better way of doing this?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SPROC - Conditional Branching

    Quote Originally Posted by Mark Gambo
    Steve,
    Seriously, how can I design a SPROC in order to prevent a SQL Injection Attack? What I am currently doing now is analyzing the user's input prior to sending the SQL to DB (Searching for the keywords: INSERT, DELETE, Semi-Colons, 1=1, etc.). Is there a better way of doing this?
    Sounds like this might be better served as a new thread...

    Give me an example of a SPROC that you have with parameters that you think can be SQL-injected.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] SPROC - Conditional Branching

    Quote Originally Posted by szlamany
    Sounds like this might be better served as a new thread...

    Give me an example of a SPROC that you have with parameters that you think can be SQL-injected.

    Thanks, I'll do it a little later.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width