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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to use stored procedure argument with like operator

how to use stored procedure argument with like operator

Scheduled Pinned Locked Moved Database
databasequestiontutorial
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.
  • M Offline
    M Offline
    Meax
    wrote on last edited by
    #1

    i have this stored procedure in database. ALTER PROCEDURE dbo.SearchForASpecificDocumentByFromBeginsWith ( @SenderName nvarchar(50) ) AS Select * from MyTable Where (SenderName like 'SenderName%') RETURN how can i use the argument (with the like operator) i am passing into this?

    M 1 Reply Last reply
    0
    • M Meax

      i have this stored procedure in database. ALTER PROCEDURE dbo.SearchForASpecificDocumentByFromBeginsWith ( @SenderName nvarchar(50) ) AS Select * from MyTable Where (SenderName like 'SenderName%') RETURN how can i use the argument (with the like operator) i am passing into this?

      M Offline
      M Offline
      Meax
      wrote on last edited by
      #2

      i have found the solution. here it is. ALTER PROCEDURE dbo.SearchForASpecificDocumentByFromBeginsWith ( @SenderName nvarchar(50) ) AS Select * from MyTable Where (SenderName LIKE + @SenderName + '%') RETURN

      C 1 Reply Last reply
      0
      • M Meax

        i have found the solution. here it is. ALTER PROCEDURE dbo.SearchForASpecificDocumentByFromBeginsWith ( @SenderName nvarchar(50) ) AS Select * from MyTable Where (SenderName LIKE + @SenderName + '%') RETURN

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        That can't work. You have too many + operators. The + between LIKE and @SenderName should be removed.

        *Developer Day Scotland - Free community conference *Colin Angus Mackay's Blog *Latest Scottish Developers Newsletter


        Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Remember that the force of gravity can go up as well as down.

        R 1 Reply Last reply
        0
        • C Colin Angus Mackay

          That can't work. You have too many + operators. The + between LIKE and @SenderName should be removed.

          *Developer Day Scotland - Free community conference *Colin Angus Mackay's Blog *Latest Scottish Developers Newsletter


          Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Remember that the force of gravity can go up as well as down.

          R Offline
          R Offline
          Rami Said Abd Alhalim
          wrote on last edited by
          #4

          if you need too many operators you must use dynamic variable for example: -- this stored procedure used to search create PROCEDURE [dbo].[SP__Find_Country] ( @chv_Country_name varchar(150)=null, @Calling_no varchar(5)=null, @Short_Name varchar(3)=null ) AS set nocount on Declare @Condition varchar(8000) Set @condition ='' IF @chv_Country_name is not null set @Condition=' co_name like ''' +@chv_Country_name + '%''' IF @Calling_no is Not Null and @Condition ='' set @Condition=' co_Calling_code like ''' +@Calling_no + '%''' else If @Calling_no is not Null set @Condition=@Condition +' and co_Calling_code like ''' +@Calling_no + '%''' IF @Short_Name is not null and @Condition ='' Set @Condition=' co_Short_Name like ''' +@Short_Name + '%''' else IF @Short_Name is not null Set @Condition=@Condition+' and co_Short_Name like ''' +@Short_Name + '%''' if @Condition <> '' Set @Condition=@Condition+' and co_deleted=0' print @condition if @condition ='' select * from Country where co_deleted=0 -- Updated by Nabeel Adnan Order By Country.co_name -- Updated By Mahmoud Khalil else begin Exec ('select * from Country where '+ @condition + ' Order By co_name ') print 'select * from Country where '+ @condition + ' Order By co_name ' end

          C 1 Reply Last reply
          0
          • R Rami Said Abd Alhalim

            if you need too many operators you must use dynamic variable for example: -- this stored procedure used to search create PROCEDURE [dbo].[SP__Find_Country] ( @chv_Country_name varchar(150)=null, @Calling_no varchar(5)=null, @Short_Name varchar(3)=null ) AS set nocount on Declare @Condition varchar(8000) Set @condition ='' IF @chv_Country_name is not null set @Condition=' co_name like ''' +@chv_Country_name + '%''' IF @Calling_no is Not Null and @Condition ='' set @Condition=' co_Calling_code like ''' +@Calling_no + '%''' else If @Calling_no is not Null set @Condition=@Condition +' and co_Calling_code like ''' +@Calling_no + '%''' IF @Short_Name is not null and @Condition ='' Set @Condition=' co_Short_Name like ''' +@Short_Name + '%''' else IF @Short_Name is not null Set @Condition=@Condition+' and co_Short_Name like ''' +@Short_Name + '%''' if @Condition <> '' Set @Condition=@Condition+' and co_deleted=0' print @condition if @condition ='' select * from Country where co_deleted=0 -- Updated by Nabeel Adnan Order By Country.co_name -- Updated By Mahmoud Khalil else begin Exec ('select * from Country where '+ @condition + ' Order By co_name ') print 'select * from Country where '+ @condition + ' Order By co_name ' end

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Rami Said Abd Alhalim wrote:

            if you need too many operators you must use dynamic variable

            If I need too many operators. Why would I need too many? Why don't I just need the right number?

            Rami Said Abd Alhalim wrote:

            create PROCEDURE [dbo].[SP__Find_Country]

            +10 Points for using stored procedures.

            Rami Said Abd Alhalim wrote:

            set @Condition=' co_name like ''' +@chv_Country_name + '%'''

            And take twice as many away for writing code that is vulnerable to a SQL Injection attack. You should really look into sp_executesql and see how to pass parameters into dynamically built SQL.

            *Developer Day Scotland - Free community conference Delegate Registration Open

            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