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)
 Check if EOF...

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 EOF

If 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]
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-02-26 : 11:27:56
Well that'll do the entire table


UPDATE Table
SET Col = 'New Values
WHERE Col = 'Old Value'

Some specifics would help us figure what you're doing though....


Brett

8-)
Go to Top of Page

type9
Starting Member

6 Posts

Posted - 2004-02-26 : 13:41:15
OK,

I need to verify that say id=4 in table1

then i'll insert a record into table2 with the rest of the values from table1

eg:
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
Go to Top of Page

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 t1
WHERE t1.ID =4
[/code]EDIT: fixed typo
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-02-26 : 13:45:51
INSERT INTO Table2 (...)
SELECT t1.id, t1.desc, t1.value, @VariableName
FROM Table1 t1
WHERE id = 4

So 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
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

type9
Starting Member

6 Posts

Posted - 2004-02-27 : 08:40:46
Thanks all for the help! It did the job.

Go to Top of Page
   

- Advertisement -