Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert data into single table from multiple tables


Insert data into single table from multiple tables

Author
Message
abhi777_aiwa
abhi777_aiwa
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
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
Sreejith Sreedharan
Sreejith Sreedharan
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 89
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.
Todd Carrier
Todd Carrier
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1742 Visits: 926
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)
habib_nub
habib_nub
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 0
i want to insert multiple data into multiple tables column
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45006 Visits: 39880
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sean.ellefsen
sean.ellefsen
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 10
After a new posting has been applied I am removing my request.
drew.allen
drew.allen
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2617 Visits: 9894
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
sean.ellefsen
sean.ellefsen
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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.
zineb
zineb
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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 Smile
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search