By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,742 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,742 IT Pros & Developers. It's quick & easy.

Conditional insert?

P: n/a
I need to do in SQL the following psuedo-code, and I'm stuck:

IF ((COUNT(*) FROM a WHERE column_a=1)>0)
THEN
INSERT INTO TABLE b
ENDIF
Aug 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
hoonew wrote:
I need to do in SQL the following psuedo-code, and I'm stuck:

IF ((COUNT(*) FROM a WHERE column_a=1)>0)
THEN
INSERT INTO TABLE b
ENDIF


create table a (column_a integer);
create table b (foo varchar(10));
insert into a (column_a) values (1), (1);

insert into b (foo)
select distinct 'foo' as foo
from a where column_a = 1;
....one record inserted, because of distinct.

insert into b (foo)
select distinct 'foo' as foo
from a where column_a = 2;
....zero records inserted, because no records in `a` match.

Bill
Aug 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.