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 2005 Forums
 Transact-SQL (2005)
 Multiple values from different tables into 1 table

Author  Topic 

ramotswa
Starting Member

6 Posts

Posted - 2009-01-09 : 07:52:40
Hello all,

This may be an obvious one, but humour me.

I want to do an insert on one table. Basically:
Insert into table1 (fielda, fieldb, fieldc) VALUES
((Select field1 from table2 where xyz), value1, value2);

Does that make sense? Basically i want all the values that match the where clause in table2 to be put in with the static value1 and value2 's...

Any help would be nice.
Thanks
Ramo

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-09 : 07:54:47
some thing like this
try it
insert into table1 ( fielda,fieldb,fieldc)
select fielda,fieldb,fieldc
from table2
where xyz in ( value1,value2)
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-09 : 07:56:05
Insert into table1 (fielda, fieldb, fieldc) select
field1, @value1, @value2
from table2 where cond..
Go to Top of Page

ramotswa
Starting Member

6 Posts

Posted - 2009-01-09 : 08:01:28
Thanks for the quick reply.. but not quite what I was looking for.

The value1 and value2 are actually integers. (i.e. 4 and 19) and they aren't changing. These values dont exist in either table.

What does the @ symbol do?

Ramo
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-09 : 08:03:13
I think you need this

insert into table1 ( fielda,fieldb,fieldc)
select fielda,value1,value2
from table2 where xyz


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ramotswa
Starting Member

6 Posts

Posted - 2009-01-09 : 08:05:51
so this is basically what I wanted to do:

Insert into table1 (ContactID, periodID, trackID) values
(
SELECT contactid,
FROM contacts WHERE
( (contacts.ContactTypeID)<>4 And (contacts.ContactTypeID)<>11 )
AND
( (contacts.StatusTypeID)=6 or (contacts.StatusTypeID)=1) ),6, 59);

No?

Ramo
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-09 : 08:06:58
try this

Insert into table1 (ContactID, periodID, trackID)
SELECT contactid,6,59
FROM contacts WHERE
( (contacts.ContactTypeID)<>4 And (contacts.ContactTypeID)<>11 )
AND
( (contacts.StatusTypeID)=6 or (contacts.StatusTypeID)=1) )
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-09 : 08:11:29
Insert into table1 (ContactID, periodID, trackID)
SELECT contactid, 6, 59
FROM contacts WHERE
( (contacts.ContactTypeID)<>4 And (contacts.ContactTypeID)<>11 )
AND
( (contacts.StatusTypeID)=6 or (contacts.StatusTypeID)=1) ;


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-09 : 08:13:09
Insert into table1 (ContactID, periodID, trackID)
SELECT contactid, 6,59
FROM contacts WHERE
( ContactTypeID<>4 And ContactTypeID<>11 )
AND
( StatusTypeID=6 or StatusTypeID=1 )
Go to Top of Page

ramotswa
Starting Member

6 Posts

Posted - 2009-01-09 : 08:29:21
Thank you all..

you are all very clever!!
:)
Ramo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-09 : 08:36:00
quote:
Originally posted by ramotswa

Thanks for the quick reply.. but not quite what I was looking for.

The value1 and value2 are actually integers. (i.e. 4 and 19) and they aren't changing. These values dont exist in either table.

What does the @ symbol do?

Ramo


@ symbol stands for local variables or parameters in sql server
Go to Top of Page
   

- Advertisement -