MySql.EntityFrameworkCore generating invalid SQL syntax on SaveChanges - mysql

I am trying to use .NET 7 and EntityFramework with MySQL.
I am using MySql.EntityFrameworkCore v7.0.0 installed via NUGET.
I am able to scaffold my context and entities ("reverse engineer / db first"). Also I can successfully use the generated DbContext and query the entities.
When I attempt to do an update to an entity I get a MySqlException on calling SaveChanges().
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 'RETURNING row_updated_time;
I did some Google searches and I can see that the keyword RETURNING is not valid syntax in MySql (but it is valid syntax in Oracle and PostGres -- this is interesting, and odd).
The complete SQL that is generated for this UPDATE (amounting to two SQL statements in one call):
UPDATE `state_agency` SET `abbreviation` = #p0
WHERE `code` = #p1
RETURNING `row_updated_time`;
SELECT `row_updated_time`
FROM `state_agency`
WHERE ROW_COUNT() = 1 AND `code` = #p1;
Is this problem familiar to anyone?
Can you suggest a possible reason why I am seeing Oracle/Postgres syntax get generated by the MySql provider?
My gut tells me that there is some sort of configuration problem and the Oracle syntax is coming out of a different provider, rather than the MySql provider.

I just ran into this exact problem and after some additional research found out the keyword RETURNING is valid mySQL syntax added in version 8.0.21
https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R
This got me headed down a versioning mismatch direction. Eventually I landed here:
https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql
I figured out in my case the server's version of mySQL was 5.7.29 and I had to specify that.
var connectionString = "server=localhost;user=root;password=1234;database=ef";
var serverVersion = new MySqlServerVersion(new Version(5, 7, 29));
// Replace 'YourDbContext' with the name of your own DbContext derived class.
services.AddDbContext<YourDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(connectionString, serverVersion)
);

Related

Getting a formatting error on the output of any MySQL stored procedure I try

I am sure that I am missing some small but important detail, but need to help to see why I am consistantly getting an error when I add in the SELECT #output in my input like this. I have looked at many aritcles and answers but none of them are quite what I am looking at:
let connection = mysql.createConnection(config,{CLIENT_MULTI_RESULTS: true});
(this line is the issue)
**let sql = 'CALL sp_whatever(?,#usernameOut);select #usernameOut;'**
await connection.query(sql, [param1],
function(err,rows){
console.log("INSIDE MySQL1");
I am doing this in Node JS and most examples are acutally in PHP. I have not found anything that is exactly what I am looking for (why am I getting a formatting error when I set it up like other examples or tutorials?)
I am using MySQL 5.7 on my Azure LInux server and the MySQL stored procedure looks like this: (in case the issue is inside the Stored Procedure itself)
CREATE DEFINER=`someDB`#`%` PROCEDURE `GetUsername`(
IN userIdVal INT,
OUT usernameOut NVARCHAR(45)
)
BEGIN
SELECT username INTO usernameOut
FROM players
WHERE userId = userIdVal AND avatarId = 0 AND Gender IS NULL AND active = 1 ;
END
This is the error I am getting:
err.message: ER_PARSE_ERROR: 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 'select #username' at line 1
It turns out that my real problem was a Node JS one. I was unable to get the output because it was a second command. I had read that I needed to add in multipleStatements: true if I wanted/needed to process multiple commands. What I didn't figure out until tonigt was that it had to be added to the config file to work correctly. Works great now!

SQL syntax error has occurred

Here is my simple query:
my $SQLp = "SELECT MAX([PawnPayments].[CreationTimeDate]) as MaxTransDate
FROM [PawnSafeDBCE].[dbo].[PawnPayments]
INNER JOIN [PawnSafeDBCE].[dbo].[PawnPaymentDetails]
ON [PawnPayments[.[PaymentID] = [PawnPaymentDetails].[PaymentID]
WHERE [PawnPaymentDetails].[TicketID[ = '$TicketID'
AND [PawnPaymentDetails].[StoreID] ='$StoreID'
Note that query is written on Perl engine. I keep receiving an error that says:
"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 '[PawnPayments].[CreationTimeDate]) as MaxTransDate:"
I believe the error has to do with the bracket notation, but unfortunately, I am having to use this style due to a poorly constructed 3rd party table. Any help? Am I missing something obvious?
Huge EDIT: The table I am querying is actually on a SQL server, not a MySQL server! My database runs on the MySQL server, but this 3rd party database runs on an older version of Microsoft SQL.
I don't know why you have all those square brackets around your table and column names, but they aren't necessary and they aren't standard SQL. That's what is causing your syntax error.
my $SQLp = "SELECT MAX(PawnPayments.CreationTimeDate) as MaxTransDate
FROM PawnSafeDBCE.dbo.PawnPayments
INNER JOIN PawnSafeDBCE.dbo.PawnPaymentDetails
ON PawnPayments.PaymentID = PawnPaymentDetails.PaymentID
WHERE PawnPaymentDetails.TicketID = '$TicketID'
AND PawnPaymentDetails.StoreID ='$StoreID'";
I'll also add that having variables interpolated in your SQL statement like that is potentially leaving you open to SQL injection attacks. Far better to use bind points in your SQL and use extra arguments to execute to fill in the values (assuming you're using DBI).
my $SQLp = "SELECT MAX(PawnPayments.CreationTimeDate) as MaxTransDate
FROM PawnSafeDBCE.dbo.PawnPayments
INNER JOIN PawnSafeDBCE.dbo.PawnPaymentDetails
ON PawnPayments.PaymentID = PawnPaymentDetails.PaymentID
WHERE PawnPaymentDetails.TicketID = ?
AND PawnPaymentDetails.StoreID = ?";
my $sth = $dbh->prepare($SQLp);
$sth->execute($TicketID, $StoreID);
Update: As Bill Karwin points out in a comment, the database.schema.table syntax makes no sense in a MySQL database. So I think you're a little confused. The error message you are getting definitely mentions MySQL, so you're connecting to a MySQL server, using DBD::MySQL - but perhaps you should be connecting to an MSSQL server instead.
It might be useful if you showed us your database connection code - the call that sets up your $dbh (or equivalent) variable.
You say you are querying a MS SQL database, but the error message clearly says you are using a MySQL database or a MySQL database driver.
If you are querying a MS SQL database, fix your connection string.
If you are querying a MySQL database, use a MySQL-compatible query. MySQL uses backticks to quote identifiers (not square brackets like MS SQL).
[PawnPayments].[CreationTimeDate]
should be
`PawnPayments`.`CreationTimeDate`
Note that your code suffers from injection bugs due to incorrect quoting of value inserted into the SQL query. (It's not good enough just to put quotes around the values!) These can cause your code to fail, and they could make you vulnerable to injection attacks. Fix the quoting, or use replaceable parameters.

MySQL error "An alias was previously found" when an alias isn't in the query

I have a very odd error while trying to perform an update on a database. This is on an Ubuntu 16.04 server using MySQL 5.7.19-0ubuntu0.16.04.1. The query is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
The MySQL error is:
1064 - 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 'offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701' at line 1
I am doing this in phpMyAdmin, and it gives a bit more information:
2 errors were found during analysis.
An alias was previously found. (near " " at position 50)
An alias was previously found. (near "'test'" at position 51)
If I try this update directly in the phpMyAdmin user interface (search for record, edit field value, submit form) it works, and the query shown is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
which appears to be identical. HOWEVER, if I do a string comparison between the two I get:
So while they appear to be the same, there is a difference somewhere.
The queries were created from a table in a database, using concatenation and referencing cells in a source table. For example:
="UPDATE athlet_teamseason SET offkeyreturners = '"&data!I2&"' WHERE athlet_teamseason.id = "&data!A2&";"
I have thousands of these and they all produce the same error. I've done this dozens of times in older servers, might be an issue with MySQL 5.7?
Thanks to Uueerdo, I eliminated non-printing characters in my query.

(ASP) MS Access -> MySQL: Error in Select, where [..] strings

I am using a portal system on my website and modified the ASP code heavily.
Since the website is growing, I want to migrate from MS Acces to MySQL.
Now, I think the portal I'm using (and some code I inputted) aren't MySQL compatable, because when I switch to the MySQL database, I get the following error.
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[MySQL][ODBC 5.1 Driver][mysqld-5.1.55-community]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 '[EzModuleID],
[ModName] From EzCore_Plugin Where IsModActive='1'' at line 1
[website]\WWWROOT\BOXX\INCLUDES../../includes/include.asp, line 3736
The SQL string regarding this line is the following:
Select [EzModuleID], [ModName] From EzCore_Plugin Where [IsModActive] = 1;
Im new to MySQL and I can't find why this is giving an error.
I've tried the quote's around 1, removing [], removing the space..
I think that when I figure out why this is causing an error, I can continue modifying the rest to make the website work on mysql.
Lose the square brackets
(I might as well post this as the answer rather than a comment)
In MySQL column and table names can be escaped with the backtick character ` or if the ANSI SQL mode is enabled with double quotes ".
Your WHERE clause (according to the error message) is Where IsModActive='1'. This works if IsModActive is a text column. If it is numeric, drop the single quotes. If IsModActive is a Boolean, change the clause to Where IsModActive IS true.
See: is operator

Migration from SQL Server 2008 to Azure

I'm using SQLAzureMW v3.8.8 but I get lots of errors in the script generated. And the problem is that i don't know in which line is each error generated.
Error #: 105 -- Unclosed quotation mark after the character string
'CREATE PROCEDURE [dbo].[spAdminParametrosGet]
Error #: 156 -- Incorrect syntax near the keyword 'ELSE'.
Error #: 40512 -- Deprecated feature 'NOLOCK or READUNCOMMITTED in
UPDATE or DELETE' is not supported in this version of SQL Server.
Incorrect syntax near '
The TSQL script generates sql stored procedures as strings and are created using dynamic SQL. Some stored procedures have comments inside of it.
May that be the cause or any suggestion to quickly migrate the database to Azure?
It is very much possible that some of the SP and other statements which you want to migrate from SQL Server to SQL Azure are not compatible. Here is a list of supported and unsupported TSQL features:
http://msdn.microsoft.com/en-us/library/windowsazure/ee336250.aspx
Also you haven't mentioned what is your source SQL Server is? As not all SQL Server will full features are supported by SAMQ (3.8 or 4.01)
Also please match your TSQL statements from the unsupported list below and check if any of listed below are part of your TSQL statements:
http://msdn.microsoft.com/en-us/library/windowsazure/ee336253.aspx