Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. Using a parameter to select a field in Select query

Using a parameter to select a field in Select query

Scheduled Pinned Locked Moved MySQL
csharpdatabasetutorialquestion
6 Posts 3 Posters 71 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #1

    Hi guys, I just wanted to know if it was possible to use a parameter to select which field is returned. For example if I have a query like this:

    SELECT @Field FROM Table WHERE ID = @ID

    and I add the parameters like this:

    cmd.Parameters.AddWithValue("@Field", myFieldName);
    cmd.Parameters.AddWithValue("@ID", ID);

    Where myFieldName is a string variable, it just returns the name of the Field rather than the value of the field where id = ID. Any ideas ? At the moment I am using something like this:

    SELECT " + myFieldName + " FROM Table WHERE ID = @ID

    but this doesn't sit pretty with me somehow. I am using this in a MySqlCommand from a C# application by the way, not a stored proc or anything.

    Live for today. Plan for tomorrow. Party tonight!

    W 1 Reply Last reply
    0
    • W Wayne Gaylard

      Hi guys, I just wanted to know if it was possible to use a parameter to select which field is returned. For example if I have a query like this:

      SELECT @Field FROM Table WHERE ID = @ID

      and I add the parameters like this:

      cmd.Parameters.AddWithValue("@Field", myFieldName);
      cmd.Parameters.AddWithValue("@ID", ID);

      Where myFieldName is a string variable, it just returns the name of the Field rather than the value of the field where id = ID. Any ideas ? At the moment I am using something like this:

      SELECT " + myFieldName + " FROM Table WHERE ID = @ID

      but this doesn't sit pretty with me somehow. I am using this in a MySqlCommand from a C# application by the way, not a stored proc or anything.

      Live for today. Plan for tomorrow. Party tonight!

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      As you said, concatenation is often 'ugly'. However, since you're defining the structure of the returned rows in the select portion, SQL isn't designed to by as dynamic as sometimes would be needed. One way to do this could be that you use CASE[^] statement. Something like:

      SELECT
      CASE @field
      WHEN 1 THEN table.column1
      WHEN 2 THEN table.column2
      ...
      END CASE AS GuessWhatColumn,
      ...

      However, depending on the situation you may have to cast all of the columns to some common data type (such as string) and that may cause some other problems.

      The need to optimize rises from a bad design.My articles[^]

      W 1 Reply Last reply
      0
      • W Wendelius

        As you said, concatenation is often 'ugly'. However, since you're defining the structure of the returned rows in the select portion, SQL isn't designed to by as dynamic as sometimes would be needed. One way to do this could be that you use CASE[^] statement. Something like:

        SELECT
        CASE @field
        WHEN 1 THEN table.column1
        WHEN 2 THEN table.column2
        ...
        END CASE AS GuessWhatColumn,
        ...

        However, depending on the situation you may have to cast all of the columns to some common data type (such as string) and that may cause some other problems.

        The need to optimize rises from a bad design.My articles[^]

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        Hi Mika, Thanks for your time. Your suggestion seems to be just the trick I was looking for. I am trying to set up a localisation scheme for an app, and I want to store my dictionaries in a database table. I was looking to support 3 languages and so each column would hold the string for each language i.e my fields would be key, language1, language2, language3, and returned field would depend upon language selected . As each field would return a string, data type would not be an issue. Once again, many thanks :thumbsup:

        Live for today. Plan for tomorrow. Party tonight!

        W L 2 Replies Last reply
        0
        • W Wayne Gaylard

          Hi Mika, Thanks for your time. Your suggestion seems to be just the trick I was looking for. I am trying to set up a localisation scheme for an app, and I want to store my dictionaries in a database table. I was looking to support 3 languages and so each column would hold the string for each language i.e my fields would be key, language1, language2, language3, and returned field would depend upon language selected . As each field would return a string, data type would not be an issue. Once again, many thanks :thumbsup:

          Live for today. Plan for tomorrow. Party tonight!

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          No problem at all :)

          The need to optimize rises from a bad design.My articles[^]

          1 Reply Last reply
          0
          • W Wayne Gaylard

            Hi Mika, Thanks for your time. Your suggestion seems to be just the trick I was looking for. I am trying to set up a localisation scheme for an app, and I want to store my dictionaries in a database table. I was looking to support 3 languages and so each column would hold the string for each language i.e my fields would be key, language1, language2, language3, and returned field would depend upon language selected . As each field would return a string, data type would not be an issue. Once again, many thanks :thumbsup:

            Live for today. Plan for tomorrow. Party tonight!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            FWIW: In such situations, I would be inclined to use separate tables, one per language. I don't like adding columns to existing tables much, I prefer adding a table. However that probably does not really help your original quest, unless you start renaming the tables, say have language1, language2,... and rename one of them to languageCurrent (that is, assuming all users of your app instantiation agree on the language!). :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            W 1 Reply Last reply
            0
            • L Luc Pattyn

              FWIW: In such situations, I would be inclined to use separate tables, one per language. I don't like adding columns to existing tables much, I prefer adding a table. However that probably does not really help your original quest, unless you start renaming the tables, say have language1, language2,... and rename one of them to languageCurrent (that is, assuming all users of your app instantiation agree on the language!). :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              Hi Luc, generally I would agree with you with regard to adding a table rather than adding columns, but in this case I do not anticipate adding any other languages. In Zimbabwe we have three official languages, namely English, Shona, and Ndebele, and so I am only going to support those three languages right from the outset, with English being the default. Many thanks for your input though.

              Live for today. Plan for tomorrow. Party tonight!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups