0

I am trying to take the data from excel sheet cells and update the mysql table using this query

sqlstr = "UPDATE " & table4 & " SET price = CASE WHEN key_id = '" & Cells(i, 1) & "' " & " THEN " & Cells(i, 8) & " " & " WHEN key_id = '" & Cells(i, 2) & "' " & " THEN " & Cells(i, 9) & " " & " ELSE: price "

It gives me an error:

Syntax to use near ':price'

Well, if i remove at the end the word price it gives me an error:

Syntax to use near "

I guess I am missing some quotes somewhere. How can I fix this issue?

3
  • Please show the results of sqlstr after it has been created. Commented Mar 29, 2014 at 16:03
  • I don't understand what you mean. The query doesn't even go through Commented Mar 29, 2014 at 16:08
  • Print the value of sqlstr so we can see what it looks like after variable substitution. Commented Mar 29, 2014 at 16:09

1 Answer 1

1

Error 1:

" ELSE: price shoule be " ELSE " & price

Error 2:

You are missing and END for the CASE clause

sqlstr = "UPDATE " & table4 & " SET price = CASE WHEN key_id = '" & Cells(i, 1) & "' THEN " & Cells(i, 8) & " WHEN key_id = '" & Cells(i, 2) & "' THEN " & Cells(i, 9) & " ELSE " & price & " END"

Sign up to request clarification or add additional context in comments.

6 Comments

It says Syntax near ':price END" at line 1
Oh holy crap!!!! IT WORKS!!!!!!!!!!!!!! THANKK YOUUUU SO MUCH!!!!!! You the man!!!
How can I start to update the table from the excel sheet from row two and not the row one. Because row one contains the name fields
It appears you are using a loop with 'i' being the ROW. If you had 'For i = 1 to 999', then change it to 'For i = 2 to 999' the solution is setting the value of 'i'
The rows loop should be 2nd to Rows.count
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.