There is no common key in either table, the resulting records will be used in an horizontal report, that's why I need this. Thanks for the reply :)
Main Topics
Browse All TopicsHi, I've been searching and thinking of a way to do this all day yesterday and I'm about to give up and do it in C#. But before I do I'd really like to know if anyone can think of a way to do this in SQL. What I want to do is make a query that returns rows based on other tables. That's not clear, let's say I've got the following tables:
tbl1
a b c
d e f
g h i
tbl2
1 2 3
4 5 6
7 8 9
0 1 2
What I want is to get something like this:
a b c 1 2 3
d e f 4 5 6
g h i 7 8 9
NULL NULL NULL 0 1 2
I don't want to use the CREATE TABLE by the way, I hate the idea of temp tables, they should never be used :)
Thanks for any input!
I use SQL Server 2000.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
dprovencher said:
>>There is no common key in either table
That complicates things, and now I think it's time for temp tables :)
SELECT IDENTITY(int, 1, 1) AS ID, column1, column2, column3
INTO #t1
FROM tbl1
SELECT IDENTITY(int, 1, 1) AS ID, column1, column2, column3
INTO #t2
FROM tbl2
SELECT #t1.column1 AS T1C1, #t1.column2 AS T1C2, #t1.column3 AS T1C3, #t2.column1 AS T2C1,
#t2.column2 AS T2C2 #t2.column3 AS T2C3
FROM #t1 FULL OUTER JOIN
#t2 ON #t1.ID = #t2.ID
ORDER BY COALESCE(#t1.ID, #t2.ID)
DROP TABLE #t1
DROP TABLE #t2
Here's my code, first a UDF for each table (don't know how else to do this):
CREATE FUNCTION fct_tbl_cie_identity ()
RETURNS @Table TABLE (NoCompagnie NVARCHAR(1024), Nom NVARCHAR(1024), RowIndex INT IDENTITY) AS
BEGIN
INSERT INTO @Table
SELECT NoCompagnie, Nom FROM tbl_cie
RETURN
END
And the actual query:
SELECT *
FROM
fct_tbl_cie_identity() cie FULL OUTER JOIN
fct_tbl_contrat_identity()
fct_tbl_chantier_identity(
fct_tbl_activite_identity(
ORDER BY COALESCE(cie.RowIndex, contrat.RowIndex, chantier.RowIndex, activite.RowIndex)
Business Accounts
Answer for Membership
by: JCinDEPosted on 2009-01-23 at 06:37:06ID: 23449211
You generally need to have some sort of key to identify which row in tbl1 goes with which row in tbl2. (Unless you're looking for a matrix of the two, which is pretty simple to get with just "select * from tbl1, tbl2" but I don't get the sense that's what you want.)
I really need more information to go on. The question is pretty vague.
By the way: Never say never. Temp tables have their place