MySql MVC3 EF Code First with Stored Procedure - mysql

I have a stored procedure created in MySql to return a resultset as
CREATE PROCEDURE GetCount()
BEGIN
SELECT count(*)
FROM mytable;
END$$
I am trying to call this from my MVC3 application using EF CF as:
int count = myContext.Database.SqlQuery<int>("GetCount").First();
It is falling over the call to test in my application with an 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 'GetCount' at line 1
Is this not supported using MySql? Obviously the above works perfectly fine with MS Sql Server 2008. So just wondering if this is a problem with the MySql side.
Thanks

Try
int count = myContext.Database.SqlQuery<int>("CALL GetCount()").First();

Related

Finding errors in Mysql Stored Procedure. Unable to understand the error

What can be the issue in this stored procedure? I have syntax error in this code which I am unable to understand. The sql query inside the code runs perfectly fine but when I try to write it as a stored procedure it throws syntax error.
The error is:
[ERROR in query 1] PROCEDURE 1bot already exists
[ERROR in query 2] 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 'DELIMITER' at line 1
DELIMITER ;;
CREATE PROCEDURE `1bot`()
BEGIN
SELECT
COUNT(distinct orders.customer_id) as Total_Customers,
date_format(orders.created_at,'%M %Y') as month,
COUNT(orders.`increment_id`) as Unique_single_orders,
SUM(orders.`base_subtotal`) as total_rev,
SUM(billing.`total` + 5) as COGS, (
SUM(orders.`base_subtotal`) - SUM(billing.total + 5)) as marketing_expense,
(AVG(orders.`base_subtotal`) - AVG(billing.total + 5)) as avg_acquisition_cost
FROM `sales_flat_order` as orders
LEFT JOIN `sales_flat_order_item` as items on orders.`entity_id` = items.`order_id`
LEFT JOIN `vingo_billing` as billing on orders.`increment_id` = billing.`order_number`
WHERE orders.`created_at` between '2016-09-01 00:00:00' and '2017-01-23'
AND items.sku LIKE "%1bot%"
AND orders.status != "closed"
and orders.shipping_invoiced != '15'
and billing.total > 0
and orders.`total_qty_ordered` < 2;
END ;;
DELIMITER ;
PROCEDURE 1bot already exists
This is self-explanatory. You apparently already have created a procedure named 1bot. MySQL gives you an error because the alternative would be to silently replace the existing procedure with the one you are defining, which may be different. MySQL is protecting you from clobbering a procedure you might need.
If you want to clobber the existing procedure and replace it with new code, you must drop it first:
DROP PROCEDURE `1bot`;;
CREATE PROCEDURE `1bot`() ...
Note that a similar error is given if you try to CREATE TABLE and name a table that already exists.
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 'DELIMITER'
You should know that the DELIMITER statement is only recognized in the mysql command-line client. The purpose is to change the statement delimiter, which is normally ; or \G, because otherwise you couldn't define a stored procedure or trigger with a body that contains multiple semicolon-delimited statements.
Whereas in most other interfaces, e.g. phpMyAdmin, MySQL Workbench, or when running SQL statements in an application, you can only run one statement at a time anyway, so there's no chance of ambiguity. Therefore DELIMITER is not necessary—and not allowed—in those interfaces.

Error while inserting picture into sql table

I am trying to insert a 'png ' image into an sql table field(called barchart,which is of type blob) with the below query.
INSERT INTO disease_symptom_soc(barchart) Values ((SELECT BULKColumn FROM OPENROWSET(BULK N'/home/barchartC2936861.png', SINGLE_BLOB) AS Image)) where disease_id='C2936861';
I am getting the below error.What could be the reason?
ERROR 1064 (42000): 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 '(BULK N'/home/barchart' at line 1
i guess u can use LOAD_FILE
Example:
INSERT INTO expBLOB(ID,IMAGE) VALUES(1,LOAD_FILE('/some/path/image.png'))
If you can, then change the DataType of the column to 'image'. Depending on the sql server version you are using. Otherwise if it oracle based db , you will have to create a loadfile proc and execute it as follows:
http://arjudba.blogspot.com/2008/06/how-to-insert-blob-dataimage-video-into.html
MySql OpenrowSet isn't used to copy files into the database but to query an external file (csv, xls) with a connection string, for example
OPENROWSET ('MSDASQL','mysql connection string','query')
I suggest you to load the file with a simple program made in any language (I don't know if you are developing in php, c#...) and pass it already loaded in memory to the db with a simple insert query.

Declare and Set not recognized syntax for my SQL server

I am running an SQL server version: 5.1.44 using XAMPP. I've been looking through SQL tutorials that use the commands DECLARE and SET.
When I run
DECLARE #iVariable INT, #vVariable VARCHAR(100), #dDateTime DATETIME
SET #iVariable = 1
It responds
#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 'DECLARE #iVariable INT, #vVariable VARCHAR(100), #dDateTime DATETIME SET #iVari' at line 1
Can anyone shed any light on why this isn't working?
I assume you use MySQL as the above error is not related to SQL Server on any levels...I also assume you don't want to create a Function or Stored Procedure but use some variables in a query window (kind of "immediate window").
In this case you don't need the "DECLARE" keyword (as this has to be used inside Stored Procedures -also has to be the very first command after the 'BEGIN' keyword-).
So you should be able to do this:
SET #iVariable = 1
or even this:
SET #iVariable = 15;
SET #iVariable = "foo"
Yes, I know what you think now (about variables and their type)... but it's MySQL, not SQL Server or Sybase ;)

How to call stored procedure with OUT parameter in Rails 3.2 - updated

I have created the following SP in MYSQL
CREATE PROCEDURE usp_BookDetailsCount(OUT bookCount INT)
BEGIN
SELECT count(*) INTO bookCount
FROM books;
SELECT bookCount;
END $$
And I am trying to get the SP out param value using rails 3.2.8.
My code in Model
count = 0
r = ActiveRecord::Base.connection.select_value("CALL usp_BookDetailsCount(OUT count)")
return count
I got the following error in Browser
Mysql::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 'OUT count)' at line 1: CALL usp_BookDetailsCount(OUT count)
But i am able to get the stored Procedure values without OUT Parameter in rails
There is no specific AR API method for interacting with stored procedures, I'm afraid. Try #execute:
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-execute

Stored procedure execution in Entity Framework in SQL Server 2005

I am using MVC3 and Entity Framework for calling a stored procedure in SQL Server 2008. The procedure has no parameters and in the profiler I get the proc execution as
exec sp_executesql N'sp_GetDashSessionboardRoomTimeSlot', N'#p0 nvarchar(4000)', #p0=NULL
This is how I call in the c# code.
SqlParameter sa = null;
var query = from dashboardData in TBSCIDBContext.Database.SqlQuery<SessionDashboardData>("sp_GetDashSessionboardRoomTimeSlot", sa)
select dashboardData;
This works fine. But I have changed my database to SQL Server 2005 and I need to use it
So when I call the same procedure using the same C# code the profiler returns the same exec statement but the sql gives error
Incorrect syntax near 'sp_GetDashSessionboardRoomTimeSlot'
pls help me in how to fix this problem as I need to use this stored procedure and in SQL Server 2005
changed it to
var query = from dashboardData in TBSCIDBContext.Database.SqlQuery("sp_GetDashSessionboardRoomTimeSlot")
select dashboardData;
it works
Change the ProviderManifestToken in SSDL (in your EDMX) to 2005. You probably have it set to 2008.