Re: IF statement in SQL?
- From: Hugo Kornelis <hugo@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 11 Nov 2005 23:44:09 +0100
On Fri, 11 Nov 2005 18:45:18 GMT, bakedmike via SQLMonster.com wrote:
>Acct_Type is a column.
>
>Is it possible to do an IF statement this way or do you have to write a
>WHERE clause?
>
>Does IFs only work with variables in T-SQL?
>
>Thanks for the response and the where clause.
Hi bakedmike,
Judging by the questions you ask, you are a beginner in the field of
SQL. Unfortunately, it's not really possible to really learn all the ins
and outs of SQL by trial, error and Usenet messages. I recommend that
you start off by reading some books.
A very basic entry-level SQL book (not specific for SQL Server, though):
* SQL Server for Mere Mortals (Hernandez / Viescas).
The following two books are medium-level books that ARE specific for SQL
Server. I haven't read them myself, but I've heard lots of positive
comments on them:
* Advanced Transact-SQL for SQL Server 2000 (Ben-Gan / Moreau)
* The Guru's Guide to Trnasact-SQL (Henderson)
For an IF, the only requirement is that the logical expression evaluates
to one single logical value: True, False, or Unknown. There are
virtually no limitations on how complex the expression is, and it might
involve several subqueries that process thousands of rows. As long as
the end result is no more or less than one single logical value.
If you need to evaluate some logic conditions for each row in the table,
and take action per row dependant on the outcome of the logic, CASE is
the expression to use. You _could_ set up a loop to get the rows one by
one, use an IF, then take approppriate action - but that would be a
terrible waste of resources, result in unwieldy code and limit your
scalability. SQL Server is optimized for set-based processing (i.e. use
one query that processes all rows "at once", instead of looping and
processing them one by one).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
.
- References:
- IF statement in SQL?
- From: bakedmike via SQLMonster.com
- Re: IF statement in SQL?
- From: David Portas
- Re: IF statement in SQL?
- From: bakedmike via SQLMonster.com
- Re: IF statement in SQL?
- From: Hugo Kornelis
- Re: IF statement in SQL?
- From: bakedmike via SQLMonster.com
- IF statement in SQL?
- Prev by Date: Re: IF statement in SQL?
- Next by Date: Re: Conditional Join
- Previous by thread: Re: IF statement in SQL?
- Next by thread: Re: Conditional Join
- Index(es):
Relevant Pages
|