1

I have a query that runs just fine on MariaDB server via GUI, however as soon I will try to run through Dapper C# I will get an error:

MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $ BEGIN NOT ATOMIC

IF (@EndDate < NOW()) THEN SET @GroupId = 3; ' at line 1'

I am using regulard IDbConnection i.e.

using (IDbConnection con = new MySqlConnection(SQLConfig.UI))

and to call the query:

return con.Query<MembershipDynamic.MembershipStatus>(sqlQuery, new{ @MembershipId = membershipId}).Single();

Same query works perfectly well with the same membershipId.

I have no idea why exactly same query wouldn't run via Dapper.

My connection string is regular one with only one extra parameter

AllowUserVariables=True

As the query is not a problem as runs fine through anything else, I am guessing that connection string is missing something ???

I also tried to set

 public MembershipsRepo()
    {
        DapperExtensions.DapperExtensions.SqlDialect = new DapperExtensions.Sql.MySqlDialect();
    }

Here is the query. I've tried without DELIMITER on direct implementation of MySqlConnection but it will only return result for first select (???) ignoring the rest... The SQL is modified MS SQL procedure.

SET @TypeId = NULL;
SET @StartDate = NULL;
SET @EndDate = NULL;
SET @GroupId = NULL;
SET @Status = NULL;

SELECT @TypeId := TypeId, @StartDate := StartDate, @EndDate := EndDate
FROM Membership_Memberships
WHERE MembershipId = @MembershipId;

DELIMITER $
BEGIN NOT ATOMIC

IF (@EndDate < NOW()) THEN
SET @GroupId = 3;
SET @STATUS = 'Expired';
END IF;

IF (SELECT COUNT(Id) FROM Membership_Trials WHERE MembershipId = 
@MembershipId AND Active = 1 ) > 0  THEN
SET @GroupId = 4;
SET @Status = 'Trial';

ELSE
SELECT @GroupId := T.GroupType, @Status := T.Name FROM Membership_Types T 
WHERE T.Id = @TypeId;
-- RETURN  Select @GroupId, @Status;
END IF;

END $
DELIMITER ;

SELECT @GroupId, @Status;

Dapper / ADO.NET works fine with simple queries, but fails with a bit more advanced queries :/

***** EDIT

After some investigation it looks like MySQL is limited to 1 query per request, so pretty much not possible to write even a bit more advanced query :/ (with variables, as those are set by Select).

Only way is to create procs which I won't, or go back to Microsoft SQL or use some other SQL...

It would be a huge hit as I've invested almost 6 months into moving from SQL to MySQL :/ (architecture, replication, etc. etc.)

:(((((((((((

11
  • 1
    Can I perhaps see what the value of sqlQuery is or where it comes from? Also: note that not everything that works in a GUI tool is the query. For example, in SQL Server (example purely for my familiarity) people often think that GO is part of the SQL syntax - it isn't: the GUI (SSMS) / command-line tools themselves detect those tokens and does something with it - not actually sending them. If you use GO in an ADO.NET query on SqlConnection: it will fail. So I wonder if DELIMETER $ BEGIN NOT ATOMIC (from the question) is something similar. Commented Mar 12, 2019 at 8:19
  • BEGIN NOT ATOMIC is required to run IF statements in MySQL. I think that Dapper just won't properly work with bigger and more advanced queries (it was stored proc on MS SQL, but I want to ditch all stored procs). I was reading around and looks like Dapper never was designed for that in a first place... I think that I might just create custom MySQL connector that I would be using for more advanced queries. Query currently is written based on MySQL documentation. Commented Mar 12, 2019 at 8:37
  • dapper doesn't care (much) what your query is - it has very little interest in it - it can be small, large, simple, complex: not dapper's problem. It is the ADO.NET / MySqlClient layer that cares about that. So: does your query work in ADO.NET? If it doesn't: it won't work via dapper, because dapper's job is to demystify the ADO.NET API and provide utility features around that. But the actual query is ferried down to ADO.NET / MySqlClient. Commented Mar 12, 2019 at 8:59
  • I have tried to remove delimited / atomic, but result is 0 / null, even if variable set to specific value before return select. Commented Mar 12, 2019 at 8:59
  • "I have tried to remove delimited / atomic, but result is 0 / null, even if variable set to specific value before return select." since you haven't shown your query, it is very hard to comment on that Commented Mar 12, 2019 at 9:00

1 Answer 1

0

Ok looks like we've lost few months of work.

MySQL is a not something that can replace Microsoft SQL.

Basically stored procedures will return all selects (and to join / process all cases etc. IT HAS to do a lot of selects / joins / if's).

Unfortunately this is a deal breaker as it would result returning 100's of MB of unnecessary data and break whole platform. (currently running on Microsoft SQL)

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

Comments

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.