Conditionally show columns in SQL

Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.

  1. #1

    Default Re: Conditionally show columns in SQL

    Almost positive that this is impossible without using dynamic sql!

    --
    Dean Savovic
    [url]www.teched.hr[/url]


    "Pierre Dippenaar" <pierre@sentinal.co.za> wrote in message
    news:d1f00c2a.0307010018.46c481a8@posting.google.c om...
    > I would like to show a column in a SQL statement only if a certain
    > condition is true (all within a stored procedure - without using
    > Dynamic SQL):
    >
    > Create Procedure spAuthors
    > @ShowFirstName smallint
    > As
    >
    > SELECT au_lname, au_fname (Only Show Firstname if @ShowFirstName = 1)
    > FROM Authors
    >
    > Is this possible without using Dynamic SQL and the EXEC statement? I
    > tried using the CASE statement but this still dislays a column for
    > FirstName even though the values are not displayed.

    Dean Savovic Guest

  2. Similar Questions and Discussions

    1. Does MS Access have a Show Columns SQL command
      I have recently discovered how to Alter Add and Drop columns from MS Access. This is going to help me a lot with updating multiple databases when I...
    2. DropDownList show the name of columns of dataGrid
      Hello, I make a dataGrid and i like to show the name of the columns of the dataGrid in a DropDownList. For example, if my dataGrid have three...
    3. Show/Hide Textbox Conditionally -- Even if Postback Occurs
      I have a datagrid control that contains several columns of data (all bound columns) and a TextBox in the last column (in an ItemTemplate). If the...
    4. SQL plugin and Show columns query
      Hi, I would like to know how can I execute and see the result of "Show Columns From table.name from db.name" using SQL plugin with MySQL...
    5. can't get data to show with bound columns
      I'm trying to create a DataGrid filled with values from a DataSet and I want the user to be able to show and hide whichever columns they choose. I...
  3. #2

    Default Re: Conditionally show columns in SQL

    Create Procedure spAuthors
    @ShowFirstName smallint
    AS
    SET NOCOUNT ON

    IF @ShowFirstName = 1
    SELECT au_lname, au_fname
    FROM Authors
    ELSE
    SELECT au_lname
    FROM Authors

    I think you shouldn't go too far with making the output of your stored
    procedures dynamic, it will only confuse in the end. If your rowssets are
    reasonably small the performance improvement of not sending the extra data
    over the network is neglegible and it is far easier handled in the client.

    --
    Jacco Schalkwijk MCDBA, MCSD, MCSE
    Database Administrator
    Eurostop Ltd.


    "Pierre Dippenaar" <pierre@sentinal.co.za> wrote in message
    news:d1f00c2a.0307010018.46c481a8@posting.google.c om...
    > I would like to show a column in a SQL statement only if a certain
    > condition is true (all within a stored procedure - without using
    > Dynamic SQL):
    >
    > Create Procedure spAuthors
    > @ShowFirstName smallint
    > As
    >
    > SELECT au_lname, au_fname (Only Show Firstname if @ShowFirstName = 1)
    > FROM Authors
    >
    > Is this possible without using Dynamic SQL and the EXEC statement? I
    > tried using the CASE statement but this still dislays a column for
    > FirstName even though the values are not displayed.

    Jacco Schalkwijk Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139