1

In MySQL Workbench the MySQL query

SET @rownum=-1;
SELECT @rownum:=@rownum+1 AS row_num
FROM someTable

returns a table where the value of row_num starts at 0 and goes up by 1 for each row:

+---------+
| row_num |
+---------+
| 0       |
+---------+
| 1       |
+---------+
| 2       |

  ....

I am trying to execute the same query from C#.

string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
    "UID=" + uid + ";" + "PASSWORD=" + password + ";" + "ALLOW USER VARIABLES = true;";

MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command;
MySqlDataAdapter adapter = new MySqlDataAdapter();

connection.Open();
command = connection.CreateCommand();

command.CommandText = "SELECT @rownum := @rownum + 1 AS row_num FROM someTable";
command.Parameters.Add("@rownum", MySqlDbType.Int32);
command.Parameters["@rownum"].Value = -1;
adapter.SelectCommand = command;

DataTable table = new DataTable();
adapter.Fill(table);

The last line of the above results in the following MySqlException

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':= -1 + 1 AS row_num FROM someTable'

Why does the query not work from C#?

5
  • why : after @rownum ? Commented Dec 18, 2016 at 19:49
  • You dont declare @rownum like in the original query Commented Dec 18, 2016 at 19:55
  • @Ehsan: becasue := is the assignment operator in MySQL. Using @rownum = @rownun + 1 in the MySQL query results in the comparison -1 = (-1 + 1) and so each row has the value 0 for row_num Commented Dec 18, 2016 at 19:56
  • @Mihai: isn't `@rownum declared indirectly via command.Parameters? Commented Dec 18, 2016 at 20:04
  • @Mihai: and you can see that MySQL recognizes @rownum as a variable because the error message shows -1 after the := Commented Dec 18, 2016 at 20:09

2 Answers 2

2

I found that this solution only works if specify the parameter "AllowUserVariables=True" in the connectionstring

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

2 Comments

Please add this type of answer in the comment box
I disagree with Pratik, this should be the accepted answer. This is the reason it didn't work in the first place, from the original question, and it helped me. If you look at his 'Connection-String' he has "Allow User Variables" incorrectly.
1

Remove these lines for parameters:

command.Parameters.Add("@rownum", MySqlDbType.Int32);
command.Parameters["@rownum"].Value = -1;

And use this query:

command.CommandText = "SET @rownum=-1;SELECT @rownum := @rownum + 1 AS row_num FROM someTable";

The way your are doing is both "@rownum" will be replaced with -1. So you end up with a query like this:

SELECT -1:= -1 + 1 AS row_num FROM someTable

5 Comments

Execution of the query and any variable assignment takes place in the MySQL engine. Your string.Format results in exactly the same CommandText as my code uses and results in the same error too
Are you sure? I quickly had made an edit to my post. Please make sure you are using the latest edit because yes, the first one was incorrect but I made the edit so fast. You must have picked it up before I edited my answer.
I have tried both (before the edit and after). Both result in the same error
Another approach. Please try that.
The connection string is wrong. Paddy should be marked as the correct answer.

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.