Re: IF statement in SQL?



Okay, here's the two statements I wanted to run using an if instead of
seperate statements. What you still don't see is that I have moved several
tables before this using a where clause. So here I want to compare to the
data that I moved so that I can leave out data referencing data I didn't move.


Run 1st:
INSERT INTO db2.dbo.Accounts (List all the fields in the table)
SELECT List of Fields in the Table FROM db1.dbo.Accounts WHERE db1.
dbo.Accounts.ACCT_ID
NOT IN (Limitors) AND (db1.dbo.ACCOUNTS.ACCT_ID IN
(SELECT db2.dbo.EMPLOYEE.EID FROM db2.dbo.EMPLOYEE));

Then:
INSERT INTO db2.dbo.Accounts (List of fields in table)
SELECT List of Fields in Table FROM db1.dbo.Accounts WHERE db1.dbo.
Accounts.ACCT_ID
NOT IN (Limitors) AND (db1.dbo..ACCOUNTS.ACCT_ID IN
(db2.dbo.COMPANY.CID FROM db2.dbo.COMPANY));

What I want to say is

If Acct_Type = E
INSERT INTO db2.dbo.Accounts (List all the fields in the table)
SELECT List of Fields in the Table FROM db1.dbo.Accounts WHERE db1.
dbo.Accounts.ACCT_ID
NOT IN (Limitors) AND (db1.dbo.ACCOUNTS.ACCT_ID IN
(SELECT db2.dbo.EMPLOYEE.EID FROM db2.dbo.EMPLOYEE))
If Acct_Type = C
INSERT INTO db2.dbo.Accounts (List of fields in table)
SELECT List of Fields in Table FROM db1.dbo.Accounts WHERE db1.dbo.
Accounts.ACCT_ID
NOT IN (Limitors) AND (db1.dbo..ACCOUNTS.ACCT_ID IN
(db2.dbo.COMPANY.CID FROM db2.dbo.COMPANY))


David Portas wrote:
>> Can I write an if statement in T-SQL for MSSQL2K or do I have to do it in
>> the
>[quoted text clipped - 6 lines]
>> IF field2 = c
>> insert into db2.accounts from db1.accounts where in db2.company
>
>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.
>

--
Message posted via http://www.sqlmonster.com
.