August 23, 2017 at 1:55 pm
I used code below to select 10 temp tables as one row table.
All 10 tables has the same column [FileID].
How to make final table only list one [FileID] column?
SELECT DISTINCT
*
FROM #1 A01
LEFT OUTER JOIN #2 A2
ON A01.FileId = A2.FileId
LEFT OUTER JOIN #3 A3
ON A01.FileId = A3.FileId
LEFT OUTER JOIN #4 A4
ON A01.FileId = A4.FileId
LEFT OUTER JOIN #5 A5
ON A01.FileId = A5.FileId
LEFT OUTER JOIN #6 A6
ON A01.FileId = A6.FileId
LEFT OUTER JOIN #7 A7
ON A01.FileId = A7.FileId
LEFT OUTER JOIN #8 A8
ON A01.FileId = A8.FileId
LEFT OUTER JOIN #9 A9
ON A01.FileId = A9.FileId
August 23, 2017 at 2:33 pm
Should be fairly straightforward considering you have aliases on each table, what have you tried and what problem were you having?
August 23, 2017 at 5:23 pm
Basically, you need to explicit list the columns you want returned from the tables instead of using the *.
August 24, 2017 at 5:28 am
One stored procedure will display more than 10 tables in which each table has more than 30 columns and some columns' name is the same.
I try to modify it to display as only one table.
It will take a lot time to list all columns in each table.
August 24, 2017 at 5:51 am
That is where tools like Red-Gates SQL Prompt come in handy to code refactoring.
You could also look at the sys.columns or information_schema for the columns in the tables in question and pull them out on bulk for each table and refactor that way.
But that's the joys of code refactoring.
August 24, 2017 at 5:58 am
Our company just purchased Red-Gates SQL Prompt.
How to use code refactoring for this issue?
I checked menu but didn't find out.
August 24, 2017 at 6:16 am
Put cursor next to the * in the SELECT. Press TAB. Remove columns you don't want.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 24, 2017 at 6:29 am
Great!
Thank you, GilaMonster!
You save my life.
August 27, 2017 at 10:24 am
adonetok - Wednesday, August 23, 2017 1:55 PMI used code below to select 10 temp tables as one row table.
All 10 tables has the same column [FileID].
How to make final table only list one [FileID] column?
SELECT DISTINCT
*
FROM #1 A01
LEFT OUTER JOIN #2 A2
ON A01.FileId = A2.FileId
LEFT OUTER JOIN #3 A3
ON A01.FileId = A3.FileId
LEFT OUTER JOIN #4 A4
ON A01.FileId = A4.FileId
LEFT OUTER JOIN #5 A5
ON A01.FileId = A5.FileId
LEFT OUTER JOIN #6 A6
ON A01.FileId = A6.FileId
LEFT OUTER JOIN #7 A7
ON A01.FileId = A7.FileId
LEFT OUTER JOIN #8 A8
ON A01.FileId = A8.FileId
LEFT OUTER JOIN #9 A9
ON A01.FileId = A9.FileId
Specify the Column instead of using , Select Distinct * from as there can be duplicate Field in master table on which you are create a join to other tables
August 28, 2017 at 7:09 pm
Yes we give you a kludge and show you how to patch of your mess. But would you rather do this correctly?
Please post DDL and follow ANSI/ISO standards when asking for help.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply