have a stored procedure with 1 input and 2 reference parameters. When assigning the stored procedure to a linq-query the query is not executed. What should I do to really execute the query which fills the 2 reference parameters. All examples I found at the internet use 'foreach' statements but the sp isn't returning a list of results.
the stored procedure:
ALTER PROCEDURE [dbo].[usp_GetStraatenPlaatsnaam]
-- Add the parameters for the stored procedure here
-- <#Param1, sysname, #p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
-- <#Param2, sysname, #p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
#POSTCODE nvarchar(6)
,#STRAAT nvarchar(17) out
,#PLAATS nvarchar(18) out
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
#STRAAT = PCT_STRAATNAAM_TNT
,#PLAATS = PCT_WOONPLAATS_TNT
FROM
PCTR
WHERE
PCT_POSTCODE = #POSTCODE
END
The Procedure is a given fact with no allowance to change. How can I call and execute this procedure using LinqToSQL? The Procedure is already used for a number of other processes and has worked for ages.
You are using output parameter as 'out' keyword which merely used in PL-Sql(Oracle)....Instead, you have to use 'OUTPUT' keyword for output parameter in T-Sql(Sql Server)....
ALTER PROCEDURE [dbo].[usp_GetStraatenPlaatsnaam]
-- Add the parameters for the stored procedure here
-- <#Param1, sysname, #p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
-- <#Param2, sysname, #p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
#POSTCODE nvarchar(6)
,#STRAAT nvarchar(17) OUTPUT
,#PLAATS nvarchar(18) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
#STRAAT = PCT_STRAATNAAM_TNT
,#PLAATS = PCT_WOONPLAATS_TNT
FROM
PCTR
WHERE
PCT_POSTCODE = #POSTCODE
END
Related
I have a procedure in which I am making query as string then prepare query and execute.
Here is the procedure
CREATE DEFINER=`root`#`%` PROCEDURE `dim_add_customer`(
IN _customer_id BIGINT(20) ,
IN _first_name VARCHAR(50) ,
)
BEGIN
SET #_query := CONCAT('first_name = "',_first_name,'"');
SET #_query := CONCAT('UPDATE customer_detail SET ',#_query,' WHERE customer_id = ',_customer_id);
PREPARE stmt FROM #_query;
END$$
DELIMITER ;
Now when I call
call dim_add_customer(1,'abc\\')
Then there is issue in creating string query.The query it made
UPDATE customer_detail SET first_name = "abc\" WHERE customer_id = 1
is there any best solution to solve this ?
You shouldn't build the queries by concat.
You should use the parameters in the query like
SET #_query="UPDATE customer_detail
SET first_name=#_first_name
WHERE customer_id = #_customer_id"
I'm not sure if you can declare your variables directly from the input parameters like
CREATE DEFINER=`root`#`%` PROCEDURE `dim_add_customer`(
IN #_customer_id BIGINT(20) ,
IN #_first_name VARCHAR(50) ,
)
or you have to
SET #_customer_id = _customer_id
SET #_first_name = _first_name
CAVEAT: I'm used to the MsSql-way of creating procedures with variables; I might have misunderstood something, but at least creating sql by concat should be your last resort.
Creating queries by concat is the equivalent of
x=1
q=concat("y=",x,"+2")
eval (q)
instead of
x=1
y=x+2
any body help me..
i am call sp not succses,
--==================================================================================
Query: call `sp_MasterDataPegawai`('','0123555','neni','P','001','001',1,'',null)
Error Code: 1414
OUT or INOUT argument 9 for routine #maninds_std_mwt.sp_MasterDataPegawai is not a variable or NEW pseudo-variable in BEFORE trigger
--==================================================================================
and this is my sp :
--==================================================================================
DELIMITER $$
USE `#maninds_std_mwt`$$
DROP PROCEDURE IF EXISTS `sp_MasterDataPegawai`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_MasterDataPegawai`(
IN p_IdPegawai CHAR(10),
IN NIK CHAR(10),
IN p_NamaLengkap VARCHAR(50),
IN p_JenisKelamin CHAR(1),
IN p_KdDivisi CHAR(3),
IN p_KdJabatan CHAR(3),
IN p_KdStatusAktif TINYINT(1),
IN p_status CHAR(1),
OUT OutputId VARCHAR(10)
)
BEGIN
/* ======Local variabel==========*/
DECLARE pLoc_TempIdPegawai VARCHAR(10);
DECLARE pLoc_TempIdPegawai_i INTEGER;
DECLARE i INTEGER;
DECLARE pLoc_KdTitle CHAR(2);
DECLARE pLoc_KdJenisPegawai CHAR(3);
/*===============================*/
IF p_JenisKelamin = 'L' THEN
SET pLoc_KdTitle = '01';
ELSE
SET pLoc_KdTitle = '02';
END IF;
SET pLoc_KdJenisPegawai = '001';
SELECT pLoc_TempIdPegawai = `IdPegawai`,COUNT(*) FROM `tbl_data_pegawai` WHERE `IdPegawai` = p_IdPegawai;
IF COUNT(*) = 0 THEN
SELECT pLoc_TempIdPegawai_i = MAX(RIGHT(`IdPegawai`,6)) FROM `tbl_data_pegawai` WHERE `IdPegawai` <> '7777777777';
IF pLoc_TempIdPegawai_i IS NULL THEN
SET pLoc_TempIdPegawai = CONCAT(p_JenisKelamin,pLoc_KdJenisPegawai,'000001');
ELSE
SET i = RIGHT(pLoc_TempIdPegawai_i,6) + 1;
SET pLoc_TempIdPegawai = CONCAT(p_JenisKelamin ,`fc_FormatNomor`(pLoc_KdJenisPegawai,3),fc_FormatNomor(i,6));
END IF;
INSERT INTO `tbl_data_pegawai`
(
`IdPegawai`,
`NIK`,
`KdTitle`,
`NamaLengkap`,
`JenisKelamin`,
`TempatLahir`,
`Alamat`,
`TglLahir`
)
VALUES
(
pLoc_TempIdPegawai,
p_NIK,
pLoc_KdTitle,
p_NamaLengkap,
p_JenisKelamin,
NULL,
NULL,
NULL
);
/*insert ke tabel tbl_data_current_pegawai */
INSERT INTO `tbl_data_current_pegawai`
(
`IdPegawai`,
`KdJenisPegawai`,
`KdJabatan`,
`KdDivisi`,
`KdAgama`,
`KdPendidikan`,
`StatusEnabled`
)
VALUES
(
pLoc_TempIdPegawai,
pLoc_KdJenisPegawai,
p_KdJabatan,
p_KdDivisi,
NULL,
NULL,
p_KdStatusAktif
);
SET OutputId = pLoc_TempIdPegawai;
-- else
IF UPPER(p_Status)= 'A' THEN
UPDATE `tbl_data_pegawai`
SET
`IdPegawai`=p_IdPegawai,
`KdTitle`=pLoc_KdTitle,
`NamaLengkap`=p_NamaLengkap,
`JenisKelamin`=p_JenisKelamin
WHERE `IdPegawai`=p_IdPegawai AND `KdTitle`=pLoc_KdTitle;
/* Update tbl_data_current_pegawai */
UPDATE `tbl_data_current_pegawai`
SET
`IdPegawai`=p_IdPegawai,
`KdJabatan`=p_KdJabatan,
`KdDivisi`=p_KdDivisi,
`StatusEnabled`=p_KdStatusAktif
WHERE `IdPegawai`=p_IdPegawai;
ELSE
DELETE FROM `tbl_data_pegawai` WHERE `IdPegawai`=p_IdPegawai;
DELETE FROM `tbl_data_current_pegawai` WHERE `IdPegawai`=p_IdPegawai;
SET OutputId = p_IdPegawai;
END IF;
END IF;
END$$
DELIMITER ;
--==================================================================================
how clear this error?
i am sorry because my english language not good.
thank you
When we specify the parameter as OUT or INOUT, the stored procedure should be able to operate on that parameter. So it expects the paramter to be a variable which the caller can use later. If we specify a VALUE, its not possible for the Stored Procedure to manipulate that value, thus it will throw an 1414 error.
We can pass values only for IN parameter of the Stored procedure.
So define a session variable and then send that variable as a parameter.
13.2.1 CALL Syntax
...
To get back a value from a procedure using an OUT or INOUT parameter,
pass the parameter by means of a user variable, and then check the
value of the variable after the procedure returns. (If you are calling
the procedure from within another stored procedure or function, you
can also pass a routine parameter or local routine variable as an IN
or INOUT parameter.)
...
Try:
-- call `sp_MasterDataPegawai`('','0123555','neni','P','001','001',1,'',null)
call `sp_MasterDataPegawai`('','0123555','neni','P','001','001',1,'',#`_OutputId`);
Need to call the parameters in mysql like this
call sp_MasterDataPegawai('','0123555','neni','P','001','001',1,'',#message);
select #message ;
I have a table called Std_Components which acts like an index for list of components with associated tables. The column AssociatedTable holds the name of table that actually contains the component data.
Please check images below -
Here is table data for Std_SteeringPumps
I am trying to create a stored procedure that will copy Std_Components table as well as all associated tables with new name. For ex. Lets say if i provided 001 as a parameter to this stored procedure i should be able create new tables like C001_Components, C001_SteeringPumps and so on.
This is what I have done so far:
ALTER PROCEDURE [dbo].[sgi_sp_CreateTablesForNewCompany]
-- Add the parameters for the stored procedure here
#CompanyId varchar(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- declare variables
declare #qry as varchar(2000)
declare #compTblName as varchar(100)
set #compTblName = 'C'+#companyId +'_Components'
-- Check if table already exists
IF object_id(#compTblName) is not null
return
-- Create main component index table by copying standard component table --
set #qry = 'Select * into '+#compTblName+' From Std_Components;';
--print #qry
--execute (#qry)
set #qry =#qry + 'Update C'+#companyId +'_Components Set AssociatedTable=''C'+#companyId +'''+substring(AssociatedTable,4,200);';
--print #qry
--exec #qry
-- Create all child tables --
Select * Into #TempTbl From dbo.Std_Components
Declare #Id int
While (Select Count(*) From #TempTbl) > 0
Begin
declare #rowTableName as varchar(50)
declare #compNewTbl as varchar(50)
Select Top 1 #rowTableName=AssociatedTable, #Id = Id From #TempTbl
set #compNewTbl = 'C'+#companyId + substring(#rowTableName,4,200);
set #qry = #qry + 'Select * into '+#compNewTbl+' From ' + #rowTableName + ';'
--print #qry
--exec #qry
Delete #TempTbl Where Id = #Id
End
print #qry
exec #qry
END
Here is the output of the print statement for the query it generates -
Select * into C001_Components From Std_Components;
Update C001_Components Set AssociatedTable='C001'+substring(AssociatedTable,4,200);
Select * into C001_SteeringPumps From Std_SteeringPumps;
But when the stored procedure is executed, I get the following error -
Msg 203, Level 16, State 2, Procedure sgi_sp_CreateTablesForNewCompany, Line 56
The name 'Select * into C001_Components From Std_Components;Update C001_Components Set AssociatedTable='C001'+substring(AssociatedTable,4,200);Select * into C001_SteeringPumps From Std_SteeringPumps;' is not a valid identifier.
Can anybody help me out resolve this issue.
Thanks for sharing your time and wisdom.
The error you're getting is because the EXEC statement (the last line of the stored procedure) needs to have brackets around the #qry variable so that it becomes
exec(#qry)
Without the brackets it's treating the entire SQL string as stored procedure name.
The non valid indentifier is around the AssociatedTable part
Set AssociatedTable='C001'+substring(AssociatedTable,4,200); will not run as there is no scope for AssociatedTable to substring - the string needs to contain the name of the table completely to be able to be executed
Instead of
exec #qry;
You need
exec sp_executesql #qry;
You'll also need to change the type of #qry to NVARCHAR. Note that because of the dynamic sql, the proc is prone to SQL Injection and other escaping issues (i.e. ensure that #CompanyId is validated)
I have a senario where i have to pass parameters to a stored procedure from a temptable
#student(table)
StudentID Class
10008 A
10009 A
10010 C
The sproc accepts 2 parameters StudentID and Class.
Student_Fail #StudentID,#Class
I would like to execute this stored procedure for all the studentID(3 times).
How can this be done? using while loop?
Well ideally you should re-write the stored procedure so that it can just use the #temp table directly, or create a different stored procedure, or just replicate in this code what the stored procedure tries to do for a single row. (Set-based operations are almost always better than processing one row at a time.)
Short of that, you'd have to use a cursor or while loop (and no they aren't really different).
DECLARE #StudentID INT, #Class CHAR(1);
DECLARE c CURSOR LOCAL FAST_FORWARD
FOR SELECT StudentID, Class FROM #student;
OPEN c;
FETCH c INTO #StudentID, #Class;
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC dbo.Student_Fail #StudentID, Class;
FETCH c INTO #StudentID, #Class;
END
CLOSE c;
DEALLOCATE c;
As you've indicated, a while loop will do:
declare #StudentID int
declare #Class char(1)
while exists (select 1 from #student)
begin
select top 1 #StudentID = StudentID
, #Class = Class
from #student
exec Student_Fail #StudentID, #Class
delete from #student where #StudentID = StudentID
end
Yes, this could be implemented as a WHILE loop, or as a CURSOR, since in this case they will do essentially the same thing, a row-by-row operation.
However, the ideal solution would be to re-implement the Student_Fail fail stored procedure to make it set-based instead of procedural.
For example, you can change your stored procedure to accept a table-valued parameter.
First, create the table type:
CREATE TYPE dbo.StudentClassTableType AS TABLE
( StudentID int, Class varchar(50) )
Next, alter the stored procedure (or create a new stored procedure) to accept the table type:
CREATE PROCEDURE dbo.usp_FailStudents
(#tvpStudentsToFail dbo.StudentClassTableType READONLY)
-- Perform set-based logic using your table parameter.
UPDATE sc
SET Fail = 1
FROM dbo.StudentClass sc
JOIN #tvpStudentsToFail fail
ON fail.StudentID = sc.StudentID
AND fail.Class = sc.Class
Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'