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)
 Insert query

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-09-01 : 19:01:55
insert into table1 (col1, col2, col3, col4)
select col1, col2, col3, col4
from table2
where col2=34

but I want to insert col1=54 //different specific value

Please advice


SA

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2009-09-01 : 19:18:36
insert into table1 (col1, col2, col3, col4)
select 54, col2, col3, col4
from table2
where col2=34


=======================================
Men build too many walls and not enough bridges. -Isaac Newton, philosopher and mathematician (1642-1727)
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-09-01 : 19:47:39
This works perfectly well. Thank you. Can someone translate it using variables? I am new to sql.

Thanks!!

SA
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2009-09-01 : 19:50:07
insert into table1 (col1, col2, col3, col4)
select @Variable, col2, col3, col4
from table2
where col2=34


=======================================
Men build too many walls and not enough bridges. -Isaac Newton, philosopher and mathematician (1642-1727)
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-09-01 : 19:55:21
insert into table1 (col1, col2, col3, col4)
select @Variable1, col2, col3, col4
from table2
where col2=@Variable2

Can I write it as above and how would I assign the variables the 2 values.

SA
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-09-01 : 21:23:17
I would really appreciate some feedback on this!!

insert into table1 (col1, col2, col3, col4)
select @Variable1, col2, col3, col4
from table2
where col2=@Variable2

Can I write it as above and how would I assign each of the variables the 2 values.

Thanks!!

SA
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-09-01 : 22:31:52
That really depends on how you are running the SQL. If you're using SSMS and the variables are varchar(10) then it would be

declare @Variable1 as varchar(10)
declare @Variable2 as varchar(10)

select @Variable1 = 'something'
select @Variable2 = 'or_another'


insert into table1 (col1, col2, col3, col4)
select @Variable1, col2, col3, col4
from table2
where col2=@Variable2




An infinite universe is the ultimate cartesian product.
Go to Top of Page
   

- Advertisement -