Syntax error when calling my stored procedure. - sql-server-2008

Stored Procedure
create procedure insertUser
#uname varchar(50),#udob date, #uadd varchar(100),#umob bigint
as
begin
insert into userInfo values(#uname,#udob,#uadd,#umob)
end
go
insertUser 'Samuel' '1990-14-04' 'Shivajinagar Pune' 12345630
Error msg:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '1990-04-14'.

Your call to the SP is incorrect. Separate the parameters with , like below. Also, date should be entered in a standard format (mm-dd-yyyy or yyyy-mm-dd)
insertUser 'Samuel', '1990-04-14', 'Shivajinagar Pune', 12345630

It would be clear when you pass parameter name = value while calling stored procedure.
Call like this,
CREATE PROCEDURE insertUser #uname VARCHAR(50)
,#udob DATE
,#uadd VARCHAR(100)
,#umob BIGINT
AS
BEGIN
INSERT INTO userInfo
VALUES (
#uname
,#udob
,#uadd
,#umob
)
END
GO
EXEC insertUser #uname = 'Samuel'
,#udob = '1990-14-04'
,#uadd = 'Shivajinagar Pune'
,#umob = 12345630

Related

MySql syntax error on procedure parameter

I am trying to write a simple procedure but am encountering a syntax error at the first parameter. As best I can tell I'm following the syntax of CREATE PROCEDURE correctly.
I am limited to accessing my database with phpMyAdmin. Here is the create script I'm trying to run:
DROP PROCEDURE IF EXISTS product_index_swap/
CREATE PROCEDURE product_index_swap (#id INT, #oldIndex INT, #newIndex INT)
BEGIN
DECLARE #swapID;
SET #swapID = (SELECT `id` FROM `product` WHERE `order_index` = #newIndex LIMIT 1);
UPDATE `products` SET `order_index` = (CASE WHEN `id` = #id THEN #newIndex
WHEN `id` = #swapID THEN #oldIndex END)
WHERE `id` IN (#id, #swapID);
END
I am using the option on phpMyAdmin to change the delimiter to /.
I receive a syntax error "near '#id INT, #oldIndex INT....". I thought I may encounter more delimiter errors since I'm not entirely clear on the scope of them. I believe if that was the problem the error would be on a new line in the procedure when it failed to understand a semicolon, not at the parameters declaration.
You're using the Microsoft SQL Server convention of putting # before all the parameters and local variables. MySQL doesn't do this.
In MySQL syntax, procedure parameters have no sigil.
Also parameters are typically declared IN or OUT or INOUT.
CREATE PROCEDURE product_index_swap (IN id INT, IN oldIndex INT, IN newIndex INT)
BEGIN
DECLARE swapID;
...
MySQL variables that have the # sigil are session variables.
See also:
https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
https://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html
https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
In MySQL, the #var variables are session level variables.
Use normal variables without the # and make sure you do not have conflict with column names:
CREATE PROCEDURE product_index_swap (in_id INT, in_oldIndex INT, in_newIndex INT)
BEGIN
DECLARE v_swapID int;
SELECT id into v_swapID
FROM product
WHERE order_index = in_newIndex
LIMIT 1;
UPDATE products
SET order_index = CASE WHEN id = in_id THEN in_newIndex
WHEN id = v_swapID THEN in_oldIndex
END
WHERE id IN (in_id, v_swapID);
END

error code 1414 in mysql when i am call store procedure

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 ;

How to fix error in procedure SQL?

there is a hranimka, when it was created, an error occurs. Maybe she who struck by the ...
The stored procedure:
CREATE PROCEDURE insert_log(
IN LogType INT,
IN LogIdNote INT,
IN LogName VARCHAR,
IN LogTime TIMESTAMP,
IN logTypeCategory INT,
IN LogIdUser INT)
begin
INSERT INTO log (LogType,
LogIdNote,
LogName,
LogTime,
logTypeCategory,
LogIdUser,
LogTypeUser,
LogUrl)
SELECT LogType, LogIdNote, LogName, LogTime, logTypeCategory, LogIdUser, url.URLCategorysubscribetotype, u.UsersTypeAccount FROM users u LEFT JOIN categorysubscribetotype url ON url.CategoryTypeCategorysubscribetotype = LogType WHERE u.idUsers = LogIdUser;
end //
Error:
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 'INT LogType, INT LogIdNote, VARCHAR LogName, TIMESTAMP LogTime,
I' at line 3
I tried only change data types at params.
I think, the next code will give me a good result, but I need save result from SELECT query at variable and insert it at query Insert:
DELIMITER |
CREATE PROCEDURE insert_log(
IN pLogType INT,
IN pLogIdNote INT,
IN pLogName VARCHAR(150),
IN pLogTime TIMESTAMP,
IN plogTypeCategory INT,
IN pLogIdUser INT)
BEGIN
DECLARE user_type INT DEFAULT 0;
DECLARE url VARCHAR(250) DEFAULT;
SET user_type = (SELECT UsersTypeAccount FROM users WHERE idUsers = pLogIdUser);
SET url = (SELECT URLCategorysubscribetotype FROM categorysubscribetotype WHERE CategoryTypeCategorysubscribetotype = pLogType);
INSERT INTO log (pLogType,
pLogIdNote,
pLogName,
pLogTime,
plogTypeCategory,
pLogIdUser,
pLogTypeUser,
pLogUrl)
VALUES (
LogType,
LogIdNote,
LogName,
LogTime,
logTypeCategory,
LogIdUser,
user_type,
url
);
END |
delimiter ;
Your issue is here:
INSERT INTO log (pLogType, //wrong!
pLogIdNote,
pLogName,
pLogTime,
plogTypeCategory,
pLogIdUser,
pLogTypeUser,
pLogUrl)
You have used the parameter as column while they should be VALUES try this Query
DELIMITER //
CREATE PROCEDURE insert_log(
IN pLogType INT,
IN pLogIdNote INT,
IN pLogName VARCHAR(150),
IN pLogTime TIMESTAMP,
IN plogTypeCategory INT,
IN pLogIdUser INT)
BEGIN
DECLARE user_type INT DEFAULT 0;
DECLARE url VARCHAR(250) DEFAULT;
SET user_type = (
SELECT UsersTypeAccount
FROM users
WHERE idUsers = pLogIdUser
);
SET url = (
SELECT URLCategorysubscribetotype
FROM categorysubscribetotype
WHERE CategoryTypeCategorysubscribetotype = pLogType
);
INSERT INTO log (
`LogType`,
`LogIdNote`,
`LogName`,
`LogTime`,
`logTypeCategory`,
`LogIdUser`,
`LogIdUserType`, /*I added this*/
`LogIdUrl`, /*this one too */
)VALUES (
pLogType,
pLogIdNote,
pLogName,
pLogTime,
plogTypeCategory,
pLogIdUser,
user_type,
url
);
END //
DELIMITER ;
Please note You need to adjust this stored procedure, there was few mistakes. for example pLogTypeUser and pLogUrl are undefined and I added comments where you need to change the column name.
Your syntax is wrong. The data types come after the parameter names, the IN/OUT specifiers come before. Something like this:
CREATE PROCEDURE insert_log(
IN LogType INT,
IN LogIdNote INT,
IN LogName VARCHAR(10),
IN LogTime TIMESTAMP,
IN logTypeCategory INT,
IN LogIdUser INT)
begin
...
Edit: Also note that I added a size specifier to the VARCHAR data type, since it requires one. I guessed at 10, but you can replace that value with whatever yours is.

Table type parameter in a stored procedure cause operand type clash error

I want to give an array of identifiers as argument to a stored procedure.
The stored procedure looks like :
ALTER PROCEDURE [dbo].[SearchPerson]
#personType INT = NULL,
#city NVARCHAR(64) = NULL,
#siteIds IntegerList READONLY,
-- some other params...
AS
SELECT
-- some fields...
FROM dbo.PersonView AS pv
WHERE
(
(#personType IS NULL OR pv.PersonType = #personType) AND
(#city IS NULL OR pv.City LIKE '%' + #city + '%') AND
(pv.SiteId in (SELECT si.Value FROM #siteIds AS si)) AND
-- some other params filter...
)
The user table type looks like :
CREATE TYPE [dbo].[IntegerList] AS TABLE(
[Value] [int] NULL
)
When I call the stored procedure from a script in SSMS (I originally have the same problem calling it from .NET code) :
DECLARE #siteIds AS IntegerList,
#personType AS INT = 1
INSERT INTO #siteIds VALUES (1)
EXEC [dbo].[SearchPerson] #personType, #siteIds
I got the error :
Operand type clash: int is incompatible with IntegerList
I found the answer : it was the order of the table type parameter that caused the error !
The table type parameter must be the first in the stored procedure parameters AND ALSO in the arguments passed to the stored procedure call !
The stored procedure :
ALTER PROCEDURE [dbo].[SearchPerson]
#siteIds IntegerList READONLY, -- THIS PARAMETER HAS TO BE THE FIRST !
#personType INT = NULL,
#city NVARCHAR(64) = NULL,
-- some other params...
AS
SELECT
-- some fields...
FROM dbo.PersonView AS pv
WHERE
(
(#personType IS NULL OR pv.PersonType = #personType) AND
(#city IS NULL OR pv.City LIKE '%' + #city + '%') AND
(pv.SiteId in (SELECT si.Value FROM #siteIds AS si)) AND
-- some other params filter...
)
And the call :
DECLARE #siteIds AS IntegerList,
#personType AS INT = 1
INSERT INTO #siteIds VALUES (1)
EXEC [dbo].[SearchPerson] #siteIds, #personType -- PUT #siteIds FIRST !
A sql server bug or am I missing something ?
DECLARE #ErrMsg varchar(1000)
DECLARE #ServiceDates ServiceDatesType
INSERT #ServiceDates (indexId,unitOfDay,dayOfMonth,dateOfMonth)
VALUES
(0,1,11,'9/11/2016 12:00:00 AM'),
(1,1,12,'9/12/2016 12:00:00 AM'),
(2,1,13,'9/13/2016 12:00:00 AM')
EXEC [usp_SaveValidate] 427,4,12,9,2016,#ErrMsg output,#ServiceDates
PRINT #ErrMsg
*/
ALTER PROCEDURE [dbo].[usp_SaveValidate] (
#EpisodeNo INT
,#ProviderId INT
,#ServiceId INT
,#Month INT
,#Year INT
,#ErrorMessage VARCHAR(1000) OUTPUT
,#ServiceDates ServiceDatesType ReadOnly
)
AS
BEGIN
-- Code Here
END
SQL SERVER 2012 - location of table type parameter does not matter, you have to make sure sequence while passing data, you can check above code which is working fine where table type parameter is at the last.

MySql 5.1.32: call another procedure within a stored procedure and set variable

I'm new at creating and working with stored procedures.
After spending several hours on trying, reading tutorials (and yes reading all the related questions at stackoverflow :-) ) I'm stuck.
This works fine:
PROCEDURE GetAgent(IN AgentName VARCHAR(50), OUT AgentID SMALLINT(6))
BEGIN
IF EXISTS (SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent') THEN
SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent';
ELSE
INSERT INTO tbl_lookup(cat, value) VALUES ('agent', AgentName);
SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent';
END IF;
END;
When called like:
Call GetAgent("Firefox 3.6.18", #AgentID);
It gives the proper response: "2"
So far so good. So let's get that into another procedure: (GetOS does the same thing, left out tot minimize reading :-)
PROCEDURE SetSessionInfo(IN OsName VARCHAR(50), IN AgentName VARCHAR(50), IN SessionID BIGINT(30), OUT SessionInfoID SMALLINT(6))
BEGIN
DECLARE nw_AgentID SMALLINT;
DECLARE nw_OSID SMALLINT;
CALL GetOs(OsName, #OsID);
SET NW_OSID = #OSID;
CALL GetAgent(AgentName, #AgentID);
SET NW_AgentID = #AgentID;
IF EXISTS (SELECT id FROM tbl_session_info WHERE session = SessionID) THEN
SELECT id AS SessionInfoID FROM tbl_session_info WHERE session = SessionID;
ELSE
INSERT INTO tbl_session_info(session, agent_id, os_id) VALUES (SessionID, GetAgent(AgentName, #AgentID), GetOs(OsName , #OsID));
SELECT id AS SessionInfoID FROM tbl_session_info WHERE session = SessionID;
END IF;
END;
When called with
Call SetSessionInfo("Windows XP", "Firefox 3.6.18", 857264713, #SessionInfoID)
I get the answer "3" (proper response from GetOS), then the procedure stops and does not insert anything.
After installing Toad I saw the reason: an error: "FUNCTION GetAgent does not exist"
Well, it is not a function, it's a procedure.
So basicly, my question:
How do I call another procedure within a stored procedure and set a variable with the result?
This is why you are getting "FUNCTION GetAgent does not exist" error:
INSERT INTO tbl_session_info(session, agent_id, os_id)
VALUES (SessionID, GetAgent(AgentName, #AgentID), GetOs(OsName , #OsID));
You are trying to call GetAgent as a function (while it is a procedure). But you have already got Agent and OS IDs into variables. Just use them:
INSERT INTO tbl_session_info(session, agent_id, os_id)
VALUES (SessionID, NW_AgentID, NW_OSID);