Results 1 to 4 of 4

Thread: [RESOLVED] incorrect syntax near % sql parameter

  1. #1

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Resolved [RESOLVED] incorrect syntax near % sql parameter

    Hi,

    I have this sql statement which will be passed to sql server 2008. This works fine.

    Code:
    string query = "Select * from Patient where LastName like '" + family + "%'";
    However, when i tried to use a parameterized query like this:
    Code:
    string query = "Select * from Patient where LastName like '" + "@family" + " %'";
    it gives me an error.

    I tried putting a blank space before the @ sign. but its not doing right.

    Any ideas? Btw, @family is sqldbtype.varchar.

    tnx
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: incorrect syntax near % sql parameter

    Remove the qoutes around the parameter
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: incorrect syntax near % sql parameter

    Bravo for attempting to use parameters....

    Just need a little tweaking...
    Code:
    string query = "Select * from Patient where LastName like (@family + '%')";
    First problem was that you had your parameter inside of tick marks, making it a literal string... which isn't what you want... but the "%" does need to be a literal... and it just simply needs to be added to the parameter. Viola!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: incorrect syntax near % sql parameter

    Hi tg and gary,

    Code:
    Bravo for attempting to use parameters....
    Ur welcome, we had this practice to use sql parameters when talking to sql server to prevent injections.

    Code:
    string query = "Select * from Patient where LastName like (@family + '%')";
    Works like a charm...thanks...I'll put this topic under my signature..

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

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



Click Here to Expand Forum to Full Width