Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2004
    Posts
    3

    Question Unanswered: Problem with sp_executesql

    I try to write query that use sp_executesql to query data by Like operation with 1 parameter like below:
    execute sp_executesql N'SELECT DISTINCT au_id,
    au_lname,au_fname
    FROM authors
    WHERE au_lname LIKE @au_lname
    ',
    N'@au_lname nVarChar',
    @au_lname = N'%Cas%'

    but It return all rows regardless of changing condition to any value.

    But if i don't use sp_executesql like below:

    SELECT DISTINCT au_id,
    au_lname,au_fname
    FROM authors
    WHERE au_lname LIKE N'%Cas%'

    It's correct!

    Can anyone tell me why?

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Change your code as follows:

    N'@au_lname nVarChar', --->>> N'@au_lname nVarChar(5)',
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  3. #3
    Join Date
    Jul 2004
    Posts
    3

    Thumbs up That Great!!

    Thank you very much for feedback!

Posting Permissions

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