SQL SERVER(T-SQL)

  • 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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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.

  • sivaj2k (1/19/2012)


    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.

    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/

  • Hi,

    Thanks.........a lot

  • 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