| Author |
Topic |
|
type9
Starting Member
6 Posts |
Posted - 2004-02-26 : 10:41:41
|
| Hi,How do I check if a field in a table contains a certain value.(IF exists?) or EOFIf it does exist I want to perform an update query.thanks in adv.. |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-02-26 : 10:45:11
|
| [code]UPDATE <table>SET <column> = CASE WHEN <certain value> THEN <new value> ELSE <column> END[/code] |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-02-26 : 11:27:56
|
| Well that'll do the entire tableUPDATE TableSET Col = 'New ValuesWHERE Col = 'Old Value'Some specifics would help us figure what you're doing though....Brett8-) |
 |
|
|
type9
Starting Member
6 Posts |
Posted - 2004-02-26 : 13:41:15
|
| OK,I need to verify that say id=4 in table1then i'll insert a record into table2 with the rest of the values from table1eg:if select id from table1=4 then insert into table2(table2.id,table2.desc,table2.value,table2.name) values(table1.id,table1.desc,table2.value,"NAME")note: "NAME" is passed into the SP from VB. I dont know if a select into query will work here due to the extra field "NAME"thanks |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-02-26 : 13:45:27
|
| [code]IF EXISTS (SELECT 1 FROM Table1 WHERE ID = 4)INSERT INTO Table2 (ID,desc,value,name)SELECT t1.ID,t1.desc,t1.value,'NAME'FROM Table1 t1WHERE t1.ID =4[/code]EDIT: fixed typo |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-26 : 13:45:51
|
| INSERT INTO Table2 (...)SELECT t1.id, t1.desc, t1.value, @VariableNameFROM Table1 t1WHERE id = 4So if id = 4 doesn't exist, it won't insert any rows. But if it does exist, it will insert the rows.@VariableName is the name of the variable being passed into the stored procedure.Tara |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-26 : 13:47:32
|
| Jay, why bother with the IF statement? I don't think it is needed. Just run the INSERT.Tara |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-02-26 : 13:52:46
|
| Hi Tara,I agree it is not necessary and that your method is optimized. I included it for readability and to answer and demonstrate to the poster the use of IF EXISTS. |
 |
|
|
type9
Starting Member
6 Posts |
Posted - 2004-02-27 : 08:40:46
|
| Thanks all for the help! It did the job. |
 |
|
|
|