January 19, 2012 at 3:24 am
Hi all,
I am using SQL SERVER 2008
What is the query(T-SQL) to get the Keywords in result set.it should be displayed in the below order
My result set should be
SELECT
FROM
WHERE
GROUP BY
The query i tried to get this is
Select 'Select'
union
Select 'from'
union
select 'where'
union
select 'group by'
but it produce the result set in different order...
can any one help on this
January 19, 2012 at 3:40 am
when you have a result set where the order matters, you must have an ORDER BY clause; in your example, you'd need an additional column that you want to order the results with, because ordering by alphabetical order of your data's not the order you are looking for.
SQL will organize the data in the order that allows it to return the data the fastest way possible, which is often not the same as the order you entered data into the table. Remember SQL is free to return data in any order unless you explicitly tell it to.
SELECT *
FROM (SELECT 1 AS mySortColumn,
'Select' AS Keyword
UNION
SELECT 2,
'from'
UNION
SELECT 3,
'where'
UNION
SELECT 4,
'group by') x
ORDER BY mySortColumn
Lowell
January 19, 2012 at 4:14 am
Hi
This is the other way.
Select 'Select'
union all
Select 'from'
union all
select 'where'
union all
select 'group by'
Regards
Siva Kumar J.
January 19, 2012 at 4:22 am
sivaj2k (1/19/2012)
HiThis is the other way.
Select 'Select'
union all
Select 'from'
union all
select 'where'
union all
select 'group by'
Regards
Siva Kumar J.
Actually Lowell’s way is the correct one. Even if the union all query seems to work, there is no garentee that it will always work. At any CU or SP it might work different. If the order is important, then you should specify how to order the records. Never trust the default sorting.
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
January 19, 2012 at 9:04 pm
Hi,
Thanks.........a lot
January 19, 2012 at 9:05 pm
Hi,
thanks sivakumar....
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply