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
 INSERT SELECTed values and other..

Author  Topic 

kingroon
Starting Member

29 Posts

Posted - 2005-12-28 : 00:51:44
Hi,

Wondering if anyone can help with this..

I want to INSERT some data already SELECTED in a query and also some other variables.. Will the **dodgy code** make it easier to understand I wonder..

** DODGY CODE **

INSERT INTO tblTableOne(Column1, Column2, Column3, Column4)
VALUES( ( SELECT Column1, Column2 FROM tblTableTwo ), "Text for Column3", "And text for column4")

** END OF DODGY CODE **

I understand it may have something to do with TEMP TABLES perhaps??

Any help greatly appreciated..

KingRoon

Chaotician Man,
Slice the lines of virgin pathways.
Harmony Hero.

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-28 : 00:58:38
I think this is what you are looking for
INSERT INTO tblTableOne(Column1, Column2, Column3, Column4)
SELECT Column1, Column2 , '<Text for Column3>', '<And text for column4'>"
FROM tblTableTwo

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-28 : 01:20:12
Also look for INSERT...SELECT in Books On Line, SQL Server help file

Madhivanan

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

kingroon
Starting Member

29 Posts

Posted - 2005-12-28 : 17:50:59
Thanks so much shallu1_gupta,

Would it be possible to then concatenate two of the SELECTed results? So that for example the combined [concat.] values of SELECTed Column1 and Column2 are inserted into Column12? Once again employing dodgy code..

** MORE DODGY CODE **

INSERT INTO tblTableOne(Column12, Column3, Column4)
SELECT Column1, Column2 , '<Text for Column3>', '<And text for column4'>"
FROM tblTableTwo

** END OF MORE DODGY CODE **

It seems that the number of SELECT columns out numbers the number of INSERT columns, and would hence error.

Thanks,
KingRoon

Chaotician Man,
Slice the lines of virgin pathways.
Harmony Hero.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-12-28 : 17:56:37
INSERT INTO tblTableOne(Column12, Column3, Column4)
SELECT Column1, Column2 , Column1 + Column2, Column2 + Column1
FROM tblTableTwo

If you can write a SELECT that outputs what you want inserted into your table, then you can do it. To concatenate columns in T-SQL, you use +.

Tara Kizer
aka tduggan
Go to Top of Page
   

- Advertisement -