Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Insert data into single table from multiple tables Expand / Collapse
Author
Message
Posted Monday, June 9, 2008 11:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 16, 2010 5:35 PM
Points: 6, Visits: 30
Hello Please Help me OUT,


I am trying to insert data from different tables into single table

Say I have a result table

resultTB with following columns
[resID] (int)identity , [PQID] int is not null, [col1] navarchar , [col2] nvarchar, [newColID]is not null


I have my results in different tables, following is my list

TB1
[PQID] int is not null, [col1] nvarchar , [col2] nvarchar


I have to insert specific data into result table from TB1 for specific PQID
along with that I have newColID= '1'

So how can I insert these two separate values into result table..

Can anyone please suggest me any way to resolve this problem through sql programming
Post #513892
Posted Monday, June 9, 2008 12:17 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, December 3, 2013 1:57 PM
Points: 394, Visits: 83
1.If your new column is static then

declare @newColID int, @PQID int
Set @newColID = 1
set @PQID =1234

Insert into resultTB( [PQID] , [col1] , [col2], [newColID])
select PQID,col1,col2, @newColID
from TB1 where TB1.[PQID] = @PQID

2.If your new column resides in another table(TB2)
If your new column is in a different table (related to TB1 through PQID)

Insert into resultTB( [PQID] , [col1] , [col2], [newColID])
select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID
from TB1 INNER JOIN TB2 ON TB1.PQID = TB2.PQID
where TB1.[PQID] = @PQID

Or

Insert into resultTB( [PQID] , [col1] , [col2], [newColID])
select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID
from TB1 , TB2
where TB1.[PQID] = @PQID
and TB1.PQID = TB2.PQID

Hope this is what u are looking for.
Post #513914
Posted Monday, June 9, 2008 12:19 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:15 PM
Points: 1,363, Visits: 795
Aiwa,

I am confused by the source of your data to be inserted into resultTB. You say you have 2 different tables as source, but only list TB1.

Also, is newColID a column in TB1?
If TB1 is your only source table...
INSERT into resultTB
(PQID,col1,col2,newColID) --notice we do not explicity insert the identity column
SELECT PQID,col1,col2,newColID
FROM TB1 where PQID = ? and newColID = 1




Todd Carrier
MCITP - Database Administrator (SQL 2008)
MCSE: Data Platform (SQL 2012)

Post #513917
Posted Wednesday, June 25, 2008 4:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 25, 2008 4:49 AM
Points: 1, Visits: 0
i want to insert multiple data into multiple tables column
Post #523153
Posted Wednesday, June 25, 2008 6:51 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:35 PM
Points: 36,943, Visits: 31,444
habib_nub (6/25/2008)
i want to insert multiple data into multiple tables column


You need to include a wee bit more info about what you want to do than that. Otherwise, the answer is simply "Make a SELECT to select the multiple data that you want and use it in one or more INSERTs to insert into multiple tables column."

You might take a peek at the link in my signature and you might want to make you're own thread instead of piggy backing on someone else's post...


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #523240
Posted Friday, April 20, 2012 3:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, April 21, 2012 12:55 AM
Points: 3, Visits: 10
After a new posting has been applied I am removing my request.
Post #1287499
Posted Friday, April 20, 2012 6:32 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
Please don't hijack old threads. If you have a new question, start a new thread.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Post #1287534
Posted Friday, April 20, 2012 9:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, April 21, 2012 12:55 AM
Points: 3, Visits: 10
My apologies to the Business Intelligence Analyst with no tact.
I didn't hijack the threat. I wasn't paying attention. My apologies to the Forum Moderators.
I'll ask question elsewhere.
Post #1287556
Posted Saturday, June 23, 2012 2:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 25, 2012 11:11 AM
Points: 1, Visits: 3
Hi,

I need your help on a similar case
I need to insert data from different tables into single table

I've a table named costs
and I've to fill it with datas from 3 tables: trademark,tariff and currency
Iam little bit confused

well the all thing Iam working on is while generating a template "word file"monitored by a stored procedure.
this sp should include this insert code in order that the table will be filled automatically once a template is generated.
I don't know if it sounds clear for you.
I hope that someone knows how to deal with that
and thank you anyway :)
Post #1320234
Posted Monday, June 25, 2012 10:41 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:36 AM
Points: 2,836, Visits: 5,066
zineb (6/23/2012)
Hi,

I need your help on a similar case
...


Please open the new thread!


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1320759
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse