1

Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?

I have following query and I want to exclude the column RowNum from the result, how can I do it ?

SELECT  *
FROM    
   (SELECT    
       ROW_NUMBER() OVER ( ORDER BY [Report].[dbo].[Reflow].ReflowID ) AS RowNum, *
    FROM      
       [Report].[dbo].[Reflow]
    WHERE     
       [Report].[dbo].[Reflow].ReflowProcessID = 2) AS RowConstrainedResult
WHERE   
   RowNum >= 100 AND RowNum < 120
ORDER BY 
   RowNum

Thanks.

Community
  • 1
  • 1
Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

6

It's considered bad practice to not specify column names in your query.

You could push the data into a #temp table, then ALTER the columns in that #temp to DROP a COLUMN, then SELECT * FROM #temp.

This would be inefficent, but it will get you the result you are asking for. By default though, it's best to get into the way of specifying all the columns you require. If someone ALTERs your initial table, even using the push #temp method above, you'll end up with different columns.

nealkernohan
  • 788
  • 1
  • 6
  • 15
2

Do not use * but give the field lsit you are interested in. That simple. Using a "*" is bad practice anyawy as the order is not defined.

TomTom
  • 61,059
  • 10
  • 88
  • 148
0

Because you want to order the results based on RowNum's values, you can not exclude this column from your results. You can save the result of your query in a temp table and then make another query on temp table and mention the columns that you want to show in the results(instead of select *). Such an approach will show all columns except RowNum which are ordered based on RowNum's values.

zari
  • 1,709
  • 1
  • 12
  • 18
0

This should work, I dont know the names of your columns so used generic names. Try not to use * its considered bad practice, makes it difficult for people to read your code.

SELECT [column1],
       [column2],
       [etcetc]

FROM (    SELECT ROW_NUMBER() OVER(ORDER BY RowConstrainedResult.RowNum) [RN],
          *

          FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY [Report].[dbo].[Reflow].ReflowID ) AS RowNum, *
                    FROM      [Report].[dbo].[Reflow]
                    WHERE     [Report].[dbo].[Reflow].ReflowProcessID = 2
                  ) AS RowConstrainedResult
          WHERE   RowNum >= 100
          AND RowNum < 120
Vince Pergolizzi
  • 6,396
  • 3
  • 19
  • 16