Results 1 to 10 of 10

Thread: [RESOLVED] sql delete statement

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Resolved [RESOLVED] sql delete statement

    hello, I just need to fix this SQL statement, can comebody tell what's wrong. It is saying:
    incorrect syntax near 'where'

    str = "delete from service where serviceid = '" + serviceid + "'"

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: sql delete statement

    Two things that are probably not the actual fault :

    you should use "&" and not "+" for string concatenation

    you should use parameters not string concatenation for passing in parameters

    However neither are likely to cause that error. The most likely thing I can think of is that the field serviceid isn't a string and by placing it in single quotes you are treating it as one. I'd check the table design.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: sql delete statement

    Moved To Database Development

    What database are you using?

  4. #4
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: sql delete statement

    use parameters in queries: depending on ur db, use the appropriate command. ie for access use Oledbcommand rather than sqlcommand.

    Code:
    Dim DeleteCommand As New SqlCommand("Delete from services where serviceid = @ServiceId", con)
    
    
    DeleteCommand.Parameters.AddWithValue("@ServiceId", serviceid)
    that said. Are you sure there is a value in the serviceid variable. maybe use & rather that + if you choose to keep your method.

    beaten to it. Keystone paul and I are thinking along the same lines;-)

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: sql delete statement

    Quote Originally Posted by MrtforCode View Post
    hello, I just need to fix this SQL statement, can comebody tell what's wrong. It is saying:
    incorrect syntax near 'where'

    str = "delete from service where serviceid = '" + serviceid + "'"
    Try setting it out like:

    $query = "delete * from service where serviceid = '$serviceid'";
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: sql delete statement

    I'll try to use parameter for serviceid, but it is a string and it gets a value.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: sql delete statement

    ok, thanks, parameters helped. that works now
    resolved

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: [RESOLVED] sql delete statement

    nightwalker your suggestion gives an error
    $str = "delete from service where serviceid = $'ServiceId'"
    character is not valid

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: [RESOLVED] sql delete statement

    oh no, I am still getting error
    syntax error near serviceid
    I rushed a bit

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] sql delete statement

    Quote Originally Posted by MrtforCode View Post
    nightwalker your suggestion gives an error
    $str = "delete from service where serviceid = $'ServiceId'"
    character is not valid
    Oops, remove the "$" if ServiceId isn't a variable but the field name.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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