Re: IF statement in SQL?



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)
.



Relevant Pages

  • Re: Follow up query from Fresher - Aaron Bertrand
    ... Start with Database Design for Mere Mortals, ... SAMS books, ... administration, then Inside SQL Server ...
    (microsoft.public.sqlserver.programming)
  • Re: Is it a complex sql query?
    ... >Is there any good book you can recommend me for sql 2005 (with SQL ... Personally, I'm going to wait for Inside SQL Server 2005, that Kalen ... "how do I operate it" kind of books. ...
    (comp.databases.ms-sqlserver)
  • Re: Books Online Updated
    ... First simply try re-installing the updated books on line.. ... If that doesn't work, go back to the SQL install, and do a custom install ... Wayne Snyder, MCDBA, SQL Server MVP ...
    (microsoft.public.sqlserver.server)
  • Re: Rowset solution is sought. Please help
    ... >Please, don't worry so ... There is actually only one SQL book I co-authored, ... The three sources that helped me advance my SQL Server skill most are: ... There are many other books (like Ken Henderson's Guru's guide, ...
    (microsoft.public.sqlserver.programming)
  • Re: Unable to Apply SP4 to SQL 2000 Cluster (new Node)
    ... Rebuild the node in the failover cluster. ... Scenario 1" in SQL Server 2000 Books Online. ... This setup process updates to SP4 only the binaries on the new ...
    (microsoft.public.sqlserver.clustering)