How to write a sql query for select statment when number of columns changing dynamically

  • Please help me for writing sql query when i have 20 columns in a table and i want to retrive only few columns data with out using * command in select statement.

  • Please be a little more specific.

    Usually, you would use SELECT col1, col2 ... FROM YourTable.

    But that sounds too basic to be the question here... Or are we talking about homework?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • With reference to the subject line:

    How to write a sql query for select statment when number of columns changing dynamically

    Try this:

    SELECT

    QUOTENAME(C.[name])

    --, *

    FROM

    [sys].[columns] AS C

    INNER JOIN [sys].[sysobjects] AS S

    ON [C].[object_id] = .[id]

    WHERE

    [xtype] = 'U'

    AND .[name] = 'YourTableNameGoesHere'

    You can then assign this to a variable and then exec with EXEC sp_executesql

    gsc_dba

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply