Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    6

    Smile Unanswered: using like with a parameter

    I am a new to SQL server. I want to create a proc to search a table using the like command. It uses a parameter as the search string. The following is the code I am using to create the proc

    CREATE PROCEDURE TestSearch
    (
    @Search nvarchar(50)
    )
    AS
    exec(
    "SELECT
    ItemID,
    Title
    FROM Test
    where Title like '%" + @Search + "%'
    ")
    GO

    I am receiving the following error.

    Server: Msg 170, Level 15, State 1, Procedure TestSearch, Line 7
    Line 7: Incorrect syntax near 'SELECT
    ItemID,
    Title
    FROM Test
    where Title like '%'.

    Does any one have some idea about what is wrong with my code.
    Thanks for your help.

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322

    Re: using like with a parameter

    1st. It doesn't like the double quotes..make them single quote

    2nd. WHT are you trying to do this dynamically within a sproc?


    Just do:

    SELECT ItemID, Title FROM Test
    WHERE Title like '%' + @Search + '%'

    is all you need...
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Oct 2003
    Posts
    6

    Re: using like with a parameter

    Thank you very much!

    Originally posted by Brett Kaiser
    1st. It doesn't like the double quotes..make them single quote

    2nd. WHT are you trying to do this dynamically within a sproc?


    Just do:

    SELECT ItemID, Title FROM Test
    WHERE Title like '%' + @Search + '%'

    is all you need...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •