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)
 Insert with Select SubQuery

Author  Topic 

Zee8
Starting Member

2 Posts

Posted - 2006-12-25 : 07:42:02

Hi all,

I need a 'lil help for an insert query having a select subquery. I'd created two tables such that...

create table table1 ( a int primary key, b varchar(100))
create table table2 ( c int primary key, d varchar(100), a int references table1(a) )
insert into table1 values(1, 'red')
insert into table1 values(2, 'yellow')


And I want to select the value of column "a" from table1 for some corresponding value of column "b". Then this value will be inserted into table2, and all of this work will be completed in one query, as...

insert into table2 values(1, 'abc', (select a from table1 where b=2))


What'll be the appropriate command?

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-12-25 : 07:56:03
[code]
insert into table2(c,d,a)
Select 1,'abc',a From table1 where b= 2
[/code]

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

Zee8
Starting Member

2 Posts

Posted - 2006-12-25 : 11:40:56
It works. Thanks a lot. :)
Go to Top of Page
   

- Advertisement -