Re: IF statement in SQL?



"bakedmike via SQLMonster.com" <u15016@uwe> wrote in message
news:570f23fad2491@xxxxxx
> Can I write an if statement in T-SQL for MSSQL2K or do I have to do it in
> the
> WHERE clause?
>
> Would like to say:
>
> IF field1 = e
> insert into db2.accounts from db1.accounts where in db2.employee
> IF field2 = c
> insert into db2.accounts from db1.accounts where in db2.company
>
>
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-mseq/200511/1

Yes, there is an IF construct, but your pseudo code doesn't make much sense.
In what table does "field1" exist? How is SQL supposed to know the table if
you don't specify it? Which row or rows in that table is your IF statement
intended to refer to? Most likely you should be using a WHERE clause,
perhaps with an EXISTS subquery, but without a better spec that's just a
guess.

--
David Portas
SQL Server MVP
--


.



Relevant Pages

  • IF statement in SQL?
    ... Can I write an if statement in T-SQL for MSSQL2K or do I have to do it in the ... WHERE clause? ... IF field1 = e ...
    (microsoft.public.sqlserver.mseq)
  • Re: SELECT COUNT(*)
    ... It just says return this value no matter what. ... Once you add the WHERE clause, you are not saying 'no matter what', you are ... > Wayne Snyder, MCDBA, SQL Server MVP ...
    (microsoft.public.sqlserver.server)
  • Re: SELECT INTO AND ORDER BY?
    ... > the ORDER BY clause: ... > In fact you can't control the order in which IDENTITY values are assigned ... > David Portas ... > SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: SELECT COUNT(*)
    ... Countgives you the number of rows which satisfy the where clause... ... Wayne Snyder, MCDBA, SQL Server MVP ... "Mike Epprecht " wrote in message ...
    (microsoft.public.sqlserver.server)
  • Re: Very simple update query running super slow!
    ... RugMan SET RugMan.test = "GOOD" ... I have NO idea what ImageFile has to do with anything. ... Your WHERE clause ... implies that it's equal to the value of Field1, ...
    (microsoft.public.access.queries)