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. Using parameter on Query condition not working

Using parameter on Query condition not working

Scheduled Pinned Locked Moved Database
databasesharepointhelpquestion
5 Posts 3 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.
  • F Offline
    F Offline
    firestoper
    wrote on last edited by
    #1

    Hi Guru's I created a stored procedure which on a straight query it will appear like this select BookTitle, Author, Status from LibBooks where Author like '%Buck%' On my SP I created two @parameters to have flexibility on my query, @SearchCategory - column name (Author) @SearchField - text to be searched (Buck) Problem: I noticed that my query does'nt return result, its it possible to use @parameter as substitute to column name on a condition? Something like this: select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' SP that I created Create procedure [dbo].[sp_searchFromCat] ( @SearchCategory varchar(100) = null, @SearchField varchar (100) = null ) as begin select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' end

    M G 2 Replies Last reply
    0
    • F firestoper

      Hi Guru's I created a stored procedure which on a straight query it will appear like this select BookTitle, Author, Status from LibBooks where Author like '%Buck%' On my SP I created two @parameters to have flexibility on my query, @SearchCategory - column name (Author) @SearchField - text to be searched (Buck) Problem: I noticed that my query does'nt return result, its it possible to use @parameter as substitute to column name on a condition? Something like this: select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' SP that I created Create procedure [dbo].[sp_searchFromCat] ( @SearchCategory varchar(100) = null, @SearchField varchar (100) = null ) as begin select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' end

      M Offline
      M Offline
      M H 1 2 3
      wrote on last edited by
      #2

      The easiest way to make the column in the where clause dynamic is to build the query in the procedure then execute the sql code. example from article: Article on dynamic sql [^] use Northwind go declare @RECCNT int declare @ORDID varchar(10) declare @CMD Nvarchar(100) set @ORDID = 10436 SET @CMD = 'SELECT @RECORDCNT=count(*) from [Orders]' + ' where OrderId < @ORDERID' print @CMD exec sp_executesql @CMD, N'@RECORDCNT int out, @ORDERID int', @RECCNT out, @ORDID print 'The number of records that have an OrderId' + ' greater than ' + @ORDID + ' is ' + cast(@RECCNT as char(5)) Another idea is if you only have a couple possible columns you could do something like this Select * From table where column1 = isnull(@Column1Value, column1) and column2 = isnull(@Column2Value, column2) This might not perform well on a large table

      F 1 Reply Last reply
      0
      • F firestoper

        Hi Guru's I created a stored procedure which on a straight query it will appear like this select BookTitle, Author, Status from LibBooks where Author like '%Buck%' On my SP I created two @parameters to have flexibility on my query, @SearchCategory - column name (Author) @SearchField - text to be searched (Buck) Problem: I noticed that my query does'nt return result, its it possible to use @parameter as substitute to column name on a condition? Something like this: select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' SP that I created Create procedure [dbo].[sp_searchFromCat] ( @SearchCategory varchar(100) = null, @SearchField varchar (100) = null ) as begin select BookTitle, Author, Status from LibBooks where @SearchCategory like '%' + @SearchField + '%' end

        G Offline
        G Offline
        gauthee
        wrote on last edited by
        #3

        Based on what condition your query should be executed? where....??

        Gautham

        F 1 Reply Last reply
        0
        • M M H 1 2 3

          The easiest way to make the column in the where clause dynamic is to build the query in the procedure then execute the sql code. example from article: Article on dynamic sql [^] use Northwind go declare @RECCNT int declare @ORDID varchar(10) declare @CMD Nvarchar(100) set @ORDID = 10436 SET @CMD = 'SELECT @RECORDCNT=count(*) from [Orders]' + ' where OrderId < @ORDERID' print @CMD exec sp_executesql @CMD, N'@RECORDCNT int out, @ORDERID int', @RECCNT out, @ORDID print 'The number of records that have an OrderId' + ' greater than ' + @ORDID + ' is ' + cast(@RECCNT as char(5)) Another idea is if you only have a couple possible columns you could do something like this Select * From table where column1 = isnull(@Column1Value, column1) and column2 = isnull(@Column2Value, column2) This might not perform well on a large table

          F Offline
          F Offline
          firestoper
          wrote on last edited by
          #4

          Hi, I guess I'll go to the second suggestion you gave, my brain got tangled after reading the first one. checking on this Thanks

          1 Reply Last reply
          0
          • G gauthee

            Based on what condition your query should be executed? where....??

            Gautham

            F Offline
            F Offline
            firestoper
            wrote on last edited by
            #5

            Hi Gautham, Just got the query working, thanks! set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[sp_searchFromCat] ( @SearchCategory varchar(100) = null, @SearchField varchar (100) = null ) as begin declare @SQL varchar(4000) set @SQL = 'select BookTitle, Author, Status from LibBooks ' + case when @SearchCategory is null then '' else 'where ' + @SearchCategory + ' like ' + '''%' + isnull(@SearchField,'') + '%''' end exec(@SQL) end Dom

            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