April 1, 2014 at 1:25 am
How to write a query to add data from one table to every record in another table withou using cursor or while loop. Column values every Record from Table 1 should appeneded as columns of Table 2 for every record in Table 2.
E.g. Table#1 has 2 rows & 2 columns and Table#2 has 2 rows and 2 columns.
Table 1 Table 2
Col 1 Col 2 Col1 Col2
1 AAA A11 A12
2 BBB B11 B12
Result should be
Col1 Col2 Col3 Col4
1 AAA A11 A12
1 AAA A11 A12
2 BBB B11 B12
2 BBB B11 B12
April 1, 2014 at 4:09 am
Do you mean this:
INSERT INTO Table2 (Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM Table1
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
April 1, 2014 at 8:04 am
A cross join?
SELECT t1.Col1, t1,Col2, t2.Col1 as Col3, t2.Col2 as col4
from Table1
cross join Table2
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply