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. Database
  4. how to get select parameter too in input parameter in stored procedure

how to get select parameter too in input parameter in stored procedure

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
18 Posts 7 Posters 0 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.
  • L Lost User

    Dhyanga wrote:

    I don't know whelther we can send fieldname as input parameter to get the value of that field.

    You could join on the system-tables, but to what use? You'll be concatenating constants in a way that can hardly be considered helpfull. If writing Sql is that much of a problem, consider a ORM-framework.

    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

    D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #8

    I had got the required output simply writing the sql query but I wanted it on stored procedure. I know i can do it something like this in my query.

    string colname;
    int ID;
    string query;
    .
    .
    .
    query = "select " + colname + " from ItemList where ID = " + ID ;
    SqlCommand cmd = new SqlCommand(query, sqlconn);
    .
    .
    .
    .

    This had solved my problem but I wanted it on stored procedure. and I was stuck giving the column name itself as input parameter.

    suchita

    L 1 Reply Last reply
    0
    • J jschell

      Dhyanga wrote:

      I don't know whelther we can send fieldname as input parameter

      You can't. Your choices are 1. Create the SQL dynamically in C# and then execute it. 2. Create the SQL dynamically in SQL and then execute it in SQL. 3. Use a fixed set of fixed SQL statements and select one based on the data passed in.

      D Offline
      D Offline
      Dhyanga
      wrote on last edited by
      #9

      Thanks Jschell.. Just curious if that can be possible. If not, i can just execute it without using stored procedure. I was trying to use better way than that if I could...

      suchita

      J 1 Reply Last reply
      0
      • D Dhyanga

        I had got the required output simply writing the sql query but I wanted it on stored procedure. I know i can do it something like this in my query.

        string colname;
        int ID;
        string query;
        .
        .
        .
        query = "select " + colname + " from ItemList where ID = " + ID ;
        SqlCommand cmd = new SqlCommand(query, sqlconn);
        .
        .
        .
        .

        This had solved my problem but I wanted it on stored procedure. and I was stuck giving the column name itself as input parameter.

        suchita

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #10

        I can only point to jschells' comment :)

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        D 1 Reply Last reply
        0
        • L Lost User

          I can only point to jschells' comment :)

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          D Offline
          D Offline
          Dhyanga
          wrote on last edited by
          #11

          Thanks Eddy for your time and views. :)

          suchita

          1 Reply Last reply
          0
          • D Dhyanga

            Hi I have a problem writing an query with select parameter as input paramter in the stored procedure.

            CREATE PROCEDURE [dbo].[SelectItemList]
            @ColName string,
            @ID int

            AS
            BEGIN TRANSACTION
            SELECT @ColName FROM ItemList where ID = @ID

            COMMIT TRANSACTION

            Is it possible something like this with select paramter also included in input paramter to the stored procedure? Thanks in advance,

            Dhyanga

            K Offline
            K Offline
            Karthik Harve
            wrote on last edited by
            #12

            Hi, try like this..

            CREATE PROCEDURE [dbo].[SelectItemList]
            @ColName string,
            @ID int

            AS
            BEGIN TRANSACTION
            SELECT @ColName = ColumnName FROM ItemList where ID = @ID

            COMMIT TRANSACTION

            Karthik Harve

            M 1 Reply Last reply
            0
            • K Karthik Harve

              Hi, try like this..

              CREATE PROCEDURE [dbo].[SelectItemList]
              @ColName string,
              @ID int

              AS
              BEGIN TRANSACTION
              SELECT @ColName = ColumnName FROM ItemList where ID = @ID

              COMMIT TRANSACTION

              Karthik Harve

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #13

              Am I missing something, you need dynamic SQL to meet this requirement, you solution does not work!

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • D Dhyanga

                Hi I have a problem writing an query with select parameter as input paramter in the stored procedure.

                CREATE PROCEDURE [dbo].[SelectItemList]
                @ColName string,
                @ID int

                AS
                BEGIN TRANSACTION
                SELECT @ColName FROM ItemList where ID = @ID

                COMMIT TRANSACTION

                Is it possible something like this with select paramter also included in input paramter to the stored procedure? Thanks in advance,

                Dhyanga

                M Offline
                M Offline
                Michael Potter
                wrote on last edited by
                #14

                You have to use sp_executesql and construct the query string yourself. Do realize that you can open yourself up to SQL injection attacks if you don't properly test the variables.

                CREATE PROCEDURE SelectItemList
                (
                @ColName string,
                @ID int
                )
                AS

                 DECLARE @sql NVARCHAR(4000)
                
                 SET @sql = N'SELECT ' + @ColName + N' FROM ItemList where ID = ' + CAST(@ID AS VARCHAR(10))
                
                 EXEC sp\_executesql @sql
                
                D J 2 Replies Last reply
                0
                • M Michael Potter

                  You have to use sp_executesql and construct the query string yourself. Do realize that you can open yourself up to SQL injection attacks if you don't properly test the variables.

                  CREATE PROCEDURE SelectItemList
                  (
                  @ColName string,
                  @ID int
                  )
                  AS

                   DECLARE @sql NVARCHAR(4000)
                  
                   SET @sql = N'SELECT ' + @ColName + N' FROM ItemList where ID = ' + CAST(@ID AS VARCHAR(10))
                  
                   EXEC sp\_executesql @sql
                  
                  D Offline
                  D Offline
                  Dhyanga
                  wrote on last edited by
                  #15

                  Thank you Micheal. That's what I wanted but we need to give the @ColName nvarchar size too. It worked fantastic.

                  suchita

                  M 1 Reply Last reply
                  0
                  • D Dhyanga

                    Thank you Micheal. That's what I wanted but we need to give the @ColName nvarchar size too. It worked fantastic.

                    suchita

                    M Offline
                    M Offline
                    Michael Potter
                    wrote on last edited by
                    #16

                    Glad I could help.

                    1 Reply Last reply
                    0
                    • D Dhyanga

                      Thanks Jschell.. Just curious if that can be possible. If not, i can just execute it without using stored procedure. I was trying to use better way than that if I could...

                      suchita

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #17

                      Dhyanga wrote:

                      i can just execute it without using stored procedure

                      My solutions work with or without stored procs.

                      1 Reply Last reply
                      0
                      • M Michael Potter

                        You have to use sp_executesql and construct the query string yourself. Do realize that you can open yourself up to SQL injection attacks if you don't properly test the variables.

                        CREATE PROCEDURE SelectItemList
                        (
                        @ColName string,
                        @ID int
                        )
                        AS

                         DECLARE @sql NVARCHAR(4000)
                        
                         SET @sql = N'SELECT ' + @ColName + N' FROM ItemList where ID = ' + CAST(@ID AS VARCHAR(10))
                        
                         EXEC sp\_executesql @sql
                        
                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #18

                        Keep in mind that as formatted that solution is open to SQL injection attacks.

                        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