Home  |  About  | Last |  Submit  |  Contact
AllQuests.com



Previous Question:  OpenSSH and Apache Web Server Reset  General HelpNext Question:  [ath0 eth0]  Networking Wireless
Question MSSQL If/Then/Else Statements within a function ( CodeGuru Forums Database )
Updated: 2008-05-16 10:43:05 (3)
MSSQL If/Then/Else Statements within a function

I'm using MSSQL 2005 (and am kinda new with db programming) and I was wondering a couple of things about if/then/else statements within a function. Whats the syntax to nest and if/then/else statement inside an SQL function. also is it possible to embed if/then/else statements inside another if/then/else statement, if so what would the syntax look like?

Thanks,
vbHobbiest

Answers: MSSQL If/Then/Else Statements within a function ( CodeGuru Forums Database )
MSSQL If/Then/Else Statements within a function

Seeing as there is no "ELSE IF" statement you'll have to do it the "hard way".

Code:
IF <CONDITION> BEING IF <NEW CONDITION> BEGIN --STATEMENTS ELSE --STATEMENTS END ELSE IF <ANOTHER NEW CONDITION> BEGIN --STATEMENTS ELSE IF <ALSO A NEW CONDITION> BEGIN --STATEMENTS ELSE --STATEMENTS END END END



And so on.... if you use a proper indentation it should provide you with a measure of manageability.

However - if you start spanning multiple IF/ELSE conditions, then I would actually advice you to rethink the flow of your code/SQL and use multiple stored procedures/queries instead and thus be able to keep the logical IF/ELSE code within a data access tier for easier development and keep the database relevant code in the sql.

edit: Also - if you'll use the IF/ELSE to simply select different things out depending on input, or different order by, then there are alternatives as well which might help you out.

Alsvha

MSSQL If/Then/Else Statements within a function

Pretty much what Alsvha said, except his syntax is wrong.
IF will allow a single statement to be carried out conditionally, unless you use BEGIN/END around the whole set. However, if you have an ELSE condition, this must be seperated in a different BEGIN/END block with the ELSE in between.

Code:
IF <CONDITION> BEGIN --statements END ELSE BEGIN --statements END


otherwise, he's entirely correct.

KrisSimonis

MSSQL If/Then/Else Statements within a function

LOL - yeah, forgot a number of BEGIN/ENDs around the ELSE's

Alsvha

Previous Question:  OpenSSH and Apache Web Server Reset  Ubuntu Forums  General HelpNext Question:  [ath0 eth0]  Ubuntu Forums  Networking Wireless

- Source: MSSQL If/Then/Else Statements within a function CodeGuru Forums Database
- Previous Question: OpenSSH and Apache Web Server Reset Ubuntu Forums General Help
- Next Question: [ath0 eth0] Ubuntu Forums Networking Wireless