SQL Clone
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
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 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
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1291 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
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2245 Visits: 953
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
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 0
i want to insert multiple data into multiple tables column
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)SSC Guru (202K reputation)

Group: General Forum Members
Points: 202725 Visits: 41943
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sean.ellefsen
sean.ellefsen
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

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

Group: General Forum Members
Points: 14675 Visits: 11179
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
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 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
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 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
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11626 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