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. Searching with desired column name with value is not working

Searching with desired column name with value is not working

Scheduled Pinned Locked Moved Database
sharepointcomalgorithmshelp
9 Posts 4 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.
  • U Offline
    U Offline
    User 11762623
    wrote on last edited by
    #1

    ALTER PROCEDURE [dbo].[SP_SearchByUserNeed] ( @ColumnName varchar(20), @Value Varchar(100) ) AS BEGIN BEGIN try Select * From Product where @ColumnName like @Value+'%'; END try BEGIN catch print('Error in [SP_SearchByProductName]') END catch END

    C 1 Reply Last reply
    0
    • U User 11762623

      ALTER PROCEDURE [dbo].[SP_SearchByUserNeed] ( @ColumnName varchar(20), @Value Varchar(100) ) AS BEGIN BEGIN try Select * From Product where @ColumnName like @Value+'%'; END try BEGIN catch print('Error in [SP_SearchByProductName]') END catch END

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      You cannot use a variable in this manner. You would need to use dynamic code.

      Mongo: Mongo only pawn... in game of life.

      U 1 Reply Last reply
      0
      • C Corporal Agarn

        You cannot use a variable in this manner. You would need to use dynamic code.

        Mongo: Mongo only pawn... in game of life.

        U Offline
        U Offline
        User 11762623
        wrote on last edited by
        #3

        Hay, Thank you so much for your valuable response. I am agree with you. But i can't find a way to do the replacement of the column name dynamically with a variable. If possible please explain the same. I believe. Thank you

        G C Richard DeemingR 3 Replies Last reply
        0
        • U User 11762623

          Hay, Thank you so much for your valuable response. I am agree with you. But i can't find a way to do the replacement of the column name dynamically with a variable. If possible please explain the same. I believe. Thank you

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Try something like this - as I am on holiday I have not had a chance to test this:

          ALTER PROCEDURE [dbo].[SP_SearchByUserNeed]
          (
          @ColumnName varchar(20),
          @Value Varchar(100)
          )
          AS
          BEGIN
          BEGIN try
          declare @qry nvarchar(max);
          set @qry = 'Select * From Product where @ColumnName like ' + '''' + @Value + '%' + '''';
          exec(@qry);
          END try
          BEGIN catch
          print('Error in [SP_SearchByProductName]')
          END catch
          END

          Hugs and kisses to the downvoter :rose:

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          Richard DeemingR 1 Reply Last reply
          0
          • U User 11762623

            Hay, Thank you so much for your valuable response. I am agree with you. But i can't find a way to do the replacement of the column name dynamically with a variable. If possible please explain the same. I believe. Thank you

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            To create dynamic T-SQL you start with a variable for the code

            DECLARE @strSQL NVARCHAR(1000);
            SET @strSQL = N'Select * From Product where ' + @ColumnName + ' like @Value+''%'';';
            EXECUTE sp_executesql @strSQL, @Value

            Hope this helps

            Mongo: Mongo only pawn... in game of life.

            Richard DeemingR 1 Reply Last reply
            0
            • G GuyThiebaut

              Try something like this - as I am on holiday I have not had a chance to test this:

              ALTER PROCEDURE [dbo].[SP_SearchByUserNeed]
              (
              @ColumnName varchar(20),
              @Value Varchar(100)
              )
              AS
              BEGIN
              BEGIN try
              declare @qry nvarchar(max);
              set @qry = 'Select * From Product where @ColumnName like ' + '''' + @Value + '%' + '''';
              exec(@qry);
              END try
              BEGIN catch
              print('Error in [SP_SearchByProductName]')
              END catch
              END

              Hugs and kisses to the downvoter :rose:

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              That code is vulnerable to SQL injection. To execute dynamic parameterized queries within SQL, you need to use sp_executesql[^].


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • C Corporal Agarn

                To create dynamic T-SQL you start with a variable for the code

                DECLARE @strSQL NVARCHAR(1000);
                SET @strSQL = N'Select * From Product where ' + @ColumnName + ' like @Value+''%'';';
                EXECUTE sp_executesql @strSQL, @Value

                Hope this helps

                Mongo: Mongo only pawn... in game of life.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Almost! You're missing the parameter definition from sql_executesql. I'd also be inclined to validate the column name before concatenating it.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                C 1 Reply Last reply
                0
                • U User 11762623

                  Hay, Thank you so much for your valuable response. I am agree with you. But i can't find a way to do the replacement of the column name dynamically with a variable. If possible please explain the same. I believe. Thank you

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  This should work:

                  ALTER PROCEDURE dbo.SP_SearchByUserNeed
                  (
                  @ColumnName varchar(20),
                  @Value varchar(100)
                  )
                  AS
                  BEGIN
                  DECLARE @RealColumnName sysname;
                  DECLARE @Statement nvarchar(max);

                  -- Validate the column name:
                  SELECT
                      @RealColumnName = name
                  FROM
                      sys.columns
                  WHERE
                      object\_id = OBJECT\_ID('Product')
                  And
                      name = @ColumnName
                  ;
                  
                  If @RealColumnName Is Null
                  BEGIN
                      RAISERROR('Unknown column: "%s"', 16, 1, @ColumnName);
                      Return;
                  END;
                  
                  SET @Statement = N'SELECT \* FROM Product WHERE ' + QuoteName(@RealColumnName) + N' Like @Value + ''%''';
                  EXEC sp\_executesql @Statement, N'@Value varchar(100)', @Value;
                  

                  END

                  This will validate that the column name passed in is a valid column in the Product table, and avoid SQL Injection[^] in the dynamic query.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Almost! You're missing the parameter definition from sql_executesql. I'd also be inclined to validate the column name before concatenating it.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    C Offline
                    C Offline
                    Corporal Agarn
                    wrote on last edited by
                    #9

                    It's early here. :-D

                    Mongo: Mongo only pawn... in game of life.

                    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