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
 General SQL Server Forums
 New to SQL Server Programming
 Inserting a value from another table

Author  Topic 

outspaced
Starting Member

21 Posts

Posted - 2006-11-17 : 10:11:23
Hi,

I am using MSSQL and I want to do something like this:

insert into
people
values
(newid(), *value from names table* )


but I can't work out how to select the value from one table and insert it into another table in a single query. Using a subquery or combining with a select has not worked.

Is this a job for a datacursor or is there a simpler way?

Thanks.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 10:15:59
insert into people (columns)
select columns
from table



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

outspaced
Starting Member

21 Posts

Posted - 2006-11-17 : 10:19:39
Thanks for the response.

How do I combine that with some static values?

eg

insert into
people
values
(newid(), *value from names table*, 'human', 'has 2 feet' )
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 10:31:57
insert into people (corresponding columns to insert into)
select newid(), columnsFromTable, 'human', 'has 2 feet',...
from table



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

outspaced
Starting Member

21 Posts

Posted - 2006-11-17 : 11:10:27
Thanks for that - stupidly using the word values.

Is there nay way I can sneak a distinct into there?

eg:
insert into people (corresponding columns to insert into)
select newid(), distinct name, 'human', 'has 2 feet',...
from names
where nameid in (1, 2 3)

Cheers!
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-11-17 : 11:18:25
insert into people (corresponding columns to insert into)
select newid(), name, 'human', 'has 2 feet',...
from
(select distinct name, other columns
from names
where nameid in (1, 2 3)
) t1




Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

outspaced
Starting Member

21 Posts

Posted - 2006-11-17 : 11:51:58
Excellent, thanks very much for your help!
Go to Top of Page
   

- Advertisement -