Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 If statement in Insert

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2005-08-03 : 10:46:35
How do I do this

I am doing an insert. One of the fields I pass in is a groupid. Depending on that ID, I need to update a one of 3 fields. if groupid = 1 then I need to update the a field, if groupid = 2 then I need to update the b field

kapilarya
Yak Posting Veteran

86 Posts

Posted - 2005-08-03 : 10:51:13
write a insert trigger in which you can check the value of group id and accordingly run the update statement

Kapil Arya
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-03 : 10:52:03
If groupid = 1
Update yourtable set a='somevalue
else if groupid = 2
Update yourtable set b='somevalue



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2005-08-03 : 11:30:23
Well the only problem is I am updating 40 fields


quote:
Originally posted by madhivanan

If groupid = 1
Update yourtable set a='somevalue
else if groupid = 2
Update yourtable set b='somevalue



Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-08-03 : 22:51:23
you said you were updating 3 fields

quote:

How do I do this

I am doing an insert. One of the fields I pass in is a groupid. Depending on that ID, I need to update a one of 3 fields. if groupid = 1 then I need to update the a field, if groupid = 2 then I need to update the b field



try the case statement

update table
set field=case when criteria1 then value1
when criteria2 then value2
...
end
where....

--edit

your subject is insert, so why not just do this checking before inserting the record so you won't have to do the update? it works the same way minus the update transaction

--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-04 : 01:05:27
>>update table
set field=case when criteria1 then value1
when criteria2 then value2

Jen, this will work if the field to be updated is same but assigning different values
He wants to update different field according to condition

helixpoint, can you explain more on what you are trying to do?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -