I am getting the following error at command line when creating this new sproc and I can't seem to figure out what's causing it. All column names have been verified in the table along with their data types to be correct.
Error:
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 'CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN Me' at line 2
here is the script:
DELIMITER //
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass
CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN MerchantStoreName VARCHAR (200),
IN RewardCardType VARCHAR (20),
IN RewardCardId BIGINT (20),
IN RewardCardTitle VARCHAR (50),
IN RewardCardDescription VARCHAR (100),
IN MerchantCharity VARCHAR (200),
IN MerchantDonation DOUBLE,
IN RewardCardExpDate DATE,
IN RewardCardreateDate DATE,
IN RewardCardStatus INT (1),
IN RewardCardPassHeader VARCHAR (50)
);
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50);
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20);
DECLARE RCpassbarcode_encoding VARCHAR (200);
DECLARE RCpassback_value1 TEXT;
DECLARE RewardCardTitle TEXT;
SET RCpasssecondary_value1 = 'expires';
SET RCpassbarcode_type = 'PKBarcodeFormatQR';
SET RCpassbarcode_encoding = 'iso-8859-1';
SET RCpassback_value1 = 'RCpassback_value1';
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `mcp_passes_merchants` (
passtype,
`status`,
merchantid,
rewardid,
merchantname,
meta_description,
passtypeidentifier,
formatversion,
teamidentifier,
serialnumber,
backgroundcolor,
visual_1xicon,
visual_2xicon,
visual_logo,
visual_text,
meta_expdate,
primary_label,
primary_value,
secondary_label1,
secondary_value1,
aux_label1,
aux_value1,
barcode_value,
barcode_text,
barcode_type,
barcode_encoding,
datecreated,
nonprofit,
donation,
back_label1,
back_value1);
VALUES (
RewardCardType,
RewardCardStatus,
MerchantNo,
RewardCardId,
MerchantStoreName,
RewardCardTitle,
RCpasstypeidenitifier,
RCpassformatversion,
RCpassteamidentifier,
RCpassserialnumber,
RCpassbackgroundcolor,
RCpassvisual_1xicon,
RCpassvisual_2xicon,
RCpassvisual_logo,
MerchantStoreName,
RewardCardExpDate,
RewardCardPassHeader,
RewardCardTitle,
RCpasssecondary_label1,
RCpasssecondary_value1,
RCpassaux_label1,
RewardCardId,
RewardCardId,
RewardCardId,
RCpassbarcode_type,
RCpassbarcode_encoding,
RewardCardCreateDate,
MerchantCharity,
MerchantDonation,
RewardCardDescription,
RCpassback_value1);
END//
DELIMITER ;
You need to end every statement with a delimiter. In your case it is //. Add this after the drop statement
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass //
here -------------------------^
here is how I fixed it. It turns out I had extra ';' after CREATE PROCEDURE statement.
DELIMITER $$
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass $$
CREATE PROCEDURE sproc_CreateFullRewardCardPass (IN MerchantNo INT (11),IN MerchantStoreName VARCHAR (200), IN RewardCardId BIGINT (20), IN RewardCardTitle VARCHAR (50), IN RewardCardDescription VARCHAR (100), IN MerchantCharity VARCHAR (200), IN MerchantDonation DOUBLE, IN RewardCardExpDate DATE, IN RewardCardreateDate DATE, IN RewardCardStatus INT (1),IN RewardCardPassHeader VARCHAR (50) )
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50) default 'expires' ;
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20) default 'PKBarcodeFormatQR' ;
DECLARE RCpassbarcode_encoding VARCHAR (200) default 'iso-8859-1' ;
DECLARE RCpassback_value1 TEXT default 'RCpassback_value1' ;
DECLARE RewardCardType VARCHAR (20) default 'full' ;
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `tablename` (passtype, `status`, merchantid, rewardid, merchantname, meta_description, passtypeidentifier, formatversion,
teamidentifier, serialnumber, backgroundcolor, visual_1xicon, visual_2xicon, visual_logo, visual_text, meta_expdate, primary_label, primary_value,
secondary_label1, secondary_value1, aux_label1, aux_value1, barcode_value, barcode_text, barcode_type, barcode_encoding, datecreated, nonprofit,
donation, back_label1, back_value1) VALUES (RewardCardType, RewardCardStatus, MerchantNo, RewardCardId, MerchantStoreName, RewardCardTitle, RCpasstypeidenitifier, RCpassformatversion, RCpassteamidentifier, RCpassserialnumber, RCpassbackgroundcolor, RCpassvisual_1xicon, RCpassvisual_2xicon, RCpassvisual_logo, MerchantStoreName, RewardCardExpDate, RewardCardPassHeader, RewardCardTitle,
RCpasssecondary_label1, RCpasssecondary_value1, RCpassaux_label1, RewardCardId, RewardCardId, RewardCardId, RCpassbarcode_type,
RCpassbarcode_encoding, RewardCardCreateDate, MerchantCharity, MerchantDonation, RewardCardDescription, RCpassback_value1);
END $$
DELIMITER ;;
Related
Fairly new to databases and am using mysql 8.0. I am trying to change my delimiter for a stored procedure but it keeps telling me that create is expected. Not sure why. Any suggestions would be greatly appreciated.
This is my code for the procedure.
DELIMITER //
CREATE PROCEDURE CustomerInfo
(
CustomerFirstName CHAR (20),
CustomerLastName CHAR (20),
CustomerAddress CHAR (100),
CustomerState CHAR (20),
CustomerZip INT(5),
CustomerPhoneNumber CHAR (15),
CustomerEmailAddress CHAR (50)
)
BEGIN
INSERT INTO Customers
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
INSERT INTO Invoice
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
END //
DELIMITER ;
I created a stored procedure which should perform the following:
Insert into Agencies Table, creating an id in the PK index row
Save the id in a variable
Insert the id and other data into Users table
The syntax below does not trigger any errors:
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END
However, the Stored Proc doesn't insert my parameters into the tables when executed. So, after searching I try to create the SP like this (including $$ after "END" and resetting the delimiter to a semi-colon ):
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END $$
DELIMITER ;
MySql creates the Stored Proc but gives me the following 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 'DELIMITER' at line 1
When I execute the created Stored Proc it still doesn't insert the data in my parameters.
Any help would be appreciated.
Some problems in your stored procedure:
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables (eg.: aName != #aName).
Avoid naming parameters or variables as columns of your tables.
DELIMITER $$
CREATE PROCEDURE `insertNewAgencyAndAdmin` (
/*
IN `aName` varchar (100),
IN `numTrav` int,
IN `polType` int,
IN `uEmail` varchar(255),
IN `uFName` varchar(40),
IN `uLName` varchar(40),
IN `uTitle` varchar(100)
*/
IN `_aName` varchar (100),
IN `_numTrav` int,
IN `_polType` int,
IN `_uEmail` varchar(255),
IN `_uFName` varchar(40),
IN `_uLName` varchar(40),
IN `_uTitle` varchar(100)
)
BEGIN
/*
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
#`aName`,
#`numTrav`,
#`polType`
);
*/
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
`_aName`,
`_numTrav`,
`_polType`
);
/*
SET #`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
#`agencyID`,
#`uEmail`,
#`uFName`,
#`uLName`,
#`uTitle`
);
*/
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
LAST_INSERT_ID(),
`_uEmail`,
`_uFName`,
`_uLName`,
`_uTitle`);
END$$
DELIMITER ;
SELECT * FROM discounts $$
delimiter $$;
CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)
BEGIN
insert into discounts values (id,title,amount);
END; $$
call insertData(3,"sadasd",56);
/*DROP PROCEDURE insertData */
I have a stored procedure as
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out user_status varchar (30),
out reg_date DATETIME
)
BEGIN
select fname=users_table.first_name,
lname=users_table.last_name,
user_status=users_table.user_status,
reg_date=users_table.reg_date
FROM users_table
WHERE users_table.userid=userid;
END$$
I am using it like
SET #fname=#lname=#user_status=#reg_date=NULL;
CALL GetInfo(
'XYZ',
#fname,
#lname,
#user_status,
#reg_date
);
SELECT #fname,#lname,#user_status,#reg_date;
But I am getting only NULL values. I think that problem is with variable assignment using Select statement.
I know that in MSSQLSERVER, we can assign variable in SELECT statement as I have but I am not sure about MySQL.
You must change the SQL in you procedure:
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out p_user_status varchar (30),
out p_reg_date DATETIME
)
BEGIN
select users_table.first_name,
users_table.last_name,
users_table.user_status,
users_table.reg_date
into fname,lname,p_user_status,p_reg_date
FROM users_table
WHERE users_table.userid=userid
limit 1;
END$$
Variable names must be different than fields
And will work
I am trying to create a transaction procedure in MySQL and I keep receiving the following syntax error which is occurring in my variable declarations:
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 ';
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECL' at line 6
Below is my procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS new_employee$$
CREATE PROCEDURE new_employee(in_Fname VARCHAR(15),in_Lname VARCHAR(15),
in_hire DATE, in_DOB DATE, in_sal Numeric(7,2))
BEGIN
DECLARE out_message VARCHAR;
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR;
DECLARE age_var Int DEFAULT 0;
START TRANSACTION;
Select MAX(Empno)
Into old_empnum
From emp;
Select COUNT(*)
Into emp_duplicate
From emp
Where (in_Fname=Fname AND in_Lname=Lname AND in_hire=HireDate AND in_DOB=DOB);
IF emp_duplicate=0 THEN
SET out_message = 'New employee update successful';
SET out_empnum = old_empnum + 1;
SET time_created = CURRENT_TIMESTAMP();
SET user_var = USER();
SET age_var = DATEDIFF(CURRENT_DATE,in_DOB);
INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);
INSERT INTO log(Empno,DateCreated,Who)
VALUES(out_empnum,time_created,user_var);
SELECT out_message;
SELECT Empno, Fname, Lname, DATEDIFF(CURRENT_DATE,DOB), HireDate
From emp;
ELSE
SET out_message = 'Employee already exists';
SELECT out_message;
END IF;
COMMIT;
END$$
DELIMITER ;
I am using cmd and notepad to execute my code (just in case that's helpful). For the life of me, I cannot see the error with my declarations?????
you need to define the VARCHAR size like this
DECLARE out_message VARCHAR(20);
try this
BEGIN
DECLARE out_message VARCHAR(20); // // varchar size here
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR(20); // varchar size here
DECLARE age_var Int DEFAULT 0;
you forget to datatype size for DECLARE out_message VARCHAR (30) and DECLARE user_var VARCHAR(30);
DECLARE out_message VARCHAR (30); <------------- here
DECLARE emp_duplicate INT DEFAULT 0;
DECLARE old_empnum INT DEFAULT 0;
DECLARE out_empnum INT DEFAULT 0;
DECLARE time_created DATETIME;
DECLARE user_var VARCHAR(30); <------------- here
DECLARE age_var Int DEFAULT 0;
In insert statement you are missing value for Empno
INSERT INTO emp(Empno,Fname,Lname,HireDate,DOB,SAL)
VALUES(in_Fname,in_Lname,in_hire,in_DOB,in_sal);
I have a stored procedure which returns me the identity of the added row, as below:
Create Procedure sp_ADD_CONTACT_EXTRANET_CLIENT
(
#NumCRPCEN nvarchar (255),
#nomContact nvarchar (255),
#prenomContact nvarchar (255),
#telFixe nvarchar (255),
#telPort nvarchar (255),
#mailContact nvarchar(255),
#idPhysique int output
)
as
Begin
INSERT INTO T_Physique values (.....)
SET #idPhysique = ##IDENTITY
RETURN #idPhysique
End
Now I would like to get the output in ADO.NET and I've tried a lot of things but the last one is :
Requeteadd.Parameters.Add("#idPhysique", SqlDbType.Int).Direction = ParameterDirection.Output;
Requeteadd.ExecuteNonQuery();
int IdPhysique = (int)Requeteadd.Parameters["#idPhysique"].Value;
But it shows me an error where it says that returned value is null. Does anyone have an idea ?
Do not use RETURN to output data - this is for error / status codes, and is limited to INT, so as soon as you need to "return" other data types, you need to do something different. You have an output parameter, so why not use it? Also, use SCOPE_IDENTITY(), not ##IDENTITY, since the latter can be affected by triggers and not necessarily give you the identity value you just generated. Finally, always use the schema prefix when creating / referencing objects, and never use the sp_ prefix. You can use usp_ instead but what values does this add, really?
CREATE PROCEDURE dbo.usp_ADD_CONTACT_EXTRANET_CLIENT
#NumCRPCEN nvarchar (255),
#nomContact nvarchar (255),
#prenomContact nvarchar (255),
#telFixe nvarchar (255),
#telPort nvarchar (255),
#mailContact nvarchar(255),
#idPhysique int output
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.T_Physique values (.....);
SET #idPhysique = SCOPE_IDENTITY();
END
GO
Like #RBarryYoung said, the solution is as below:
Create Procedure sp_ADD_CONTACT_EXTRANET_CLIENT
(
#NumCRPCEN nvarchar (255),
#nomContact nvarchar (255),
#prenomContact nvarchar (255),
#telFixe nvarchar (255),
#telPort nvarchar (255),
#mailContact nvarchar(255),
#idPhysique int output
)
as
Begin
INSERT INTO T_Physique values (1, #nomContact, #prenomContact, (Select AdrLngId from T_Adresse where AdrStrRC = '74003'), 'T', 2, null, null, null, null, null, null, null, null, '*', #telFixe, #telPort, null, null, null, 1, #mailContact, null, null, null, null, null)
SET #idPhysique = ##IDENTITY
End
RETURN #idPhysique
the return of the output has to be after the End. For information, in ADO.NET c#, I'm getting the returned value by doing this :
Requeteadd.Parameters.Add("#idPhysique", SqlDbType.Int).Direction = ParameterDirection.Output;
Requeteadd.ExecuteNonQuery();
int IdPhysique = (int)Requeteadd.Parameters["#idPhysique"].Value;