Professional Web Applications Themes

Conditionally show columns in SQL - Microsoft SQL / MS SQL Server

Almost positive that this is impossible without using dynamic sql! -- Dean Savovic [url]www.teched.hr[/url] "Pierre Dippenaar" <pierresentinal.co.za> wrote in message news:d1f00c2a.0307010018.46c481a8posting.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 ...

  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" <pierresentinal.co.za> wrote in message
    news:d1f00c2a.0307010018.46c481a8posting.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. #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" <pierresentinal.co.za> wrote in message
    news:d1f00c2a.0307010018.46c481a8posting.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

Similar Threads

  1. Does MS Access have a Show Columns SQL command
    By UpstateWeb in forum Coldfusion Database Access
    Replies: 1
    Last Post: October 26th, 04:49 PM
  2. DropDownList show the name of columns of dataGrid
    By dalbo in forum ASP.NET Data Grid Control
    Replies: 7
    Last Post: August 24th, 01:38 PM
  3. Show/Hide Textbox Conditionally -- Even if Postback Occurs
    By Aaron in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: September 19th, 08:00 AM
  4. SQL plugin and Show columns query
    By Eli in forum FileMaker
    Replies: 1
    Last Post: July 24th, 11:02 PM
  5. can't get data to show with bound columns
    By headware in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: July 10th, 04:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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