MySQL stored procedure parameter not working - c#

I am new to MySQL and I am trying to port some stored procedures from SQL Server. I created a basic stored procedure with parameter passing in MySQL.
The stored procedure simply takes a parameter and executes a 'drop if exist' based on the parameter supplied.
When running in MySQL results in an error below.
Seems that somehow, the parameter passed 'mytablename' into the stored procedure is executed as part of the statement instead of a parameter.
Any advice?
SQL statement:
call mydb.spDroptableifexist('customerdata');
Results:
0 row(s) affected, 1 warning(s): 1051 Unknown table 'mydb.mytablename'
This is the stored procedure:
USE `mydb`;
DROP procedure IF EXISTS `spDroptableifexist`;
DELIMITER $$
USE `mydb`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spDroptableifexist`(IN mytablename VARCHAR(255))
BEGIN
drop table if exists mytablename;
END$$
DELIMITER ;

From the manual:
User variables are intended to provide data values. They cannot be
used directly in an SQL statement as an identifier or as part of an
identifier, such as in contexts where a table or database name is
expected
To use a variable as a table name, you need to prepare a statement and execute it:
SET #sql = CONCAT('DROP TABLE IF EXISTS ', mytablename);
PREPARE stmt FROM #sql;
EXECUTE stmt;

Related

Stored procedure cannot create complex type in entity framework

I have added this store procedure through phpmyadmin:
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectUser`()
LANGUAGE SQL
NOT DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
COMMENT ''
Select * from User
In my edmx but when create a complex type and do the Get column information that gives
The store procedure returns no columns
try add
SET FMTONLY OFFin your sql statement

Passing an array of objects to SQL stored procedure

I have 2 tables
payment (payment_id, otherCosts, GarageCosts)
spareparts (payment_id, sparepartId, sparePartQty)
In payment table payment_id is autogenerated. Apart from otherCosts and garagecosts values, in my C# asp.net application there is an array of objects with
{ sparepartId : 'Somevalue', sparePartQty : 'somevalue' }
What I need to do is in a stored procedure first enter the record into payment table with garage costs and others costs value. Then return the last generated payment ID and enter it to spareParts table as paymentId for each of the value pairs in the array.
What is the way to achieve this? Please help.
Based on your tags I am going to assume we are talking about SQL Server / T-SQL.
You could do all this in one stored procedure:
CREATE PROCEDURE dbo.Foo ... /* input parameters */
AS
BEGIN
DECLARE #PaymentId int
INSERT INTO payment(otherCosts, GarageCosts) VALUES (...)
SET #PaymentId = SCOPE_IDENTITY()
INSERT INTO spareparts(payment_id, sparepartId, sparePartQty) VALUES(#PaymentId, ...)
END
GO
You may want to also look into ##IDENTITY but make sure you read about ##IDENTITY and SCOPE_IDENTITY and understand the risks associated with the first one.
If you need to have two separate sprocs you can do that too and here is how the first sproc would look like. Note that the #PaymentId is an output parameter which means that the caller can retrieve it and pass it to the second procedure.
CREATE PROCEDURE dbo.Foo
/* input parameters */
#PaymentId int OUT
AS
BEGIN
INSERT INTO payment(otherCosts, GarageCosts) VALUES (...)
SET #PaymentId = SCOPE_IDENTITY()
END
GO
Edit - after the scope of the question was clarified:
If you need to call the second stored procedure and pass it an array of parameters, with SQL Server 2008 or newer you can use TVP (Table Value Parameters). To see how you can use them in stored procedures and how you can pass them from C# code see Table Value Parameters in SQL Server 2008 and .NET (C#) or Table-Valued Parameters.
You can also use TVPs with the solution where you only have one sproc.
To solve your problem try this
First insert your data in payment table with otherCosts and GarageCosts.
Then create a procedure to get the latest stored payment_id from payment table
create procedure select_last_payment_id
as
begin
select top 1 payment_id
from payment
order by payment_id desc
end
Lastly get that payment_id by running stored procedure and assigning it to payment_id of spareparts table and storing spareparts data.
Hope it works for you.

OUT or INOUT argument for routine db is not a variable

I created a procedure with out parameter in MYSQL. Its working fine while am calling through query. My problem is, I want to add this procedure in dot net DataTable. ServerExplorer-->RightClick--> AddTableAdaptor -->use Existing Stored Procedure
DROP PROCEDURE IF EXISTS SP_OUTLETCREATION;
CREATE PROCEDURE SP_OUTLETCREATION(
IN SP_OutletId smallint,
IN SP_OutletName varchar(20),
out SP_MSG varchar(50)
)...
I got the error
OUT or INOUT argument 3 for routine xxx is not a variable or NEW pseudo-variable in BEFORE trigger
I am using the MYSQL Connector Net 6.3.6. Please help me to fix this issue.

Error: Unknown Return Type, The return type for the following stored procedures could not be detected

I am using the following technologies:
MS SQL 2008 R2
Visual Studio 2010
Silverlight project
LINQ to SQL
When dragging a stored procedure into the data model, and the stored procedure returns an OUTPUT parameter, it works fine. However when the stored procedure returns a result set created with dynamic SQL I receive the following error:
"Unknown Return Type, The return type for the following stored procedure could not be detected."
If the stored procedure does not use dynamic SQL, it works fine.
(For example: SELECT column1 from table2)
The stored procedure however uses dynamic SQL to query a specified view for the data. Simplified below for illustration purposes:
CREATE PROCEDURE GetViewData
#ViewName nvarchar(150)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Sql nvarchar(MAX)
SET #Sql = 'SELECT Column1, Column2, Column3 FROM' + #ViewName
EXEC sp_executesql #Sql
END
Instead of returning the result set directly, I can place the result set into a temp table. This however gives the same error.
According to the article from Ritesh, when placing the result set into a table type variable this should do the trick, however this is not possible in dynamic sql as the scope is only within the executed dynamic sql.
Ritesh's article:
http://riteshkk2000.blogspot.com/2010/08/error-unknown-return-type-return-types.html
Upon further investigation I realized that the meta data received by LINQ to SQL does not contain the necessary information to define the type.
Apparently with SQL 2012 this can be resolved by using "WITH RESULTS SET" to actually define the meta data manually.
I resolved this by creating another stored procedure, calling GetViewData without using dynamic SQL and populating a table type variable:
CREATE PROCEDURE CallGetViewData
#ViewName nvarchar(150)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #ViewResultSet TABLE
(
column1 nvarchar(50),
column2 int,
column3 float
)
INSERT INTO #ViewResultSet
EXEC GetViewData #ViewName
SELECT column1, column2, column3 FROM #ViewResultSet
END
Now when I drag stored procedure CallGetViewData into the data model, it detects the return type correctly.
One trick you can try is to manually create a model that represents the shape of the object that the Stored Proc will return. Then using the designer, don't drag the proc into the method pane, but rather drop it on the type you created manually. LINQ to SQL will then use that class as the result type.
If you already have the method defined for the stored proc, you can change the Return Type using the F4 property window to any type that matches the result shape that you've defined in the model.

Is it possible to use in C#/SQL Server a table variable as a parameter for stored procedures?

I'm aware that I can use a table value parameter as another option and I was already able to implement it. The reason I'm trying to use this approach(Table variable not Table value) is because I'm studying and I want to learn other ways on how to insert to database through a stored procedure.
Here's the current query I have:
CREATE PROCEDURE TVar
(
DECLARE #TblVar TABLE
(Product nvarchar(max), Qty int)
)
AS
Insert Into sample_tbl Select * FROM #TblVar
Is it possible to have this kind of approach wherein in the C# code I will use a datatable to pass as a parameter which can be done when using a table value parameter. I'm having errors when execute the query .
Here's the error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Yes. You can use a table value parameter as a parameter to a stored procedure, and you can call from C#.
You need to separately declare a TABLE TYPE:
CREATE TYPE MyTableType AS TABLE
(
Product nvarchar(max),
Qty int
);
GO
and then use like so:
CREATE PROCEDURE TVar
(
#TblVar as dbo.MyTableType READONLY
)
AS
...
These articles show how to build a .NET DataTable to call the stored procedure from C#:
C# and Table Value Parameters
Table Value Parameters in SQL Server 2008 and .NET (C#)

Categories