MySQL stored procedure will execute but conditions are not working. Could someone clear up this issue?
The empty value is not checking with or condition. Does it need a replacement for or?
DELIMITER $$
CREATE DEFINER=`rebar`#`%` PROCEDURE `SearchInProgress`(
ClientID bigint,
GCName varchar(250),
TeamID int,
USPMID Bigint,
JobReceivedDate datetime,
importanceID Bigint
)
begin
select * from jobdetails
where
(clientid = ClientID or ClientID = "") and
(GCName = GCName or GCName ="") and
(TeamID = TeamID or TeamID ="") and
(ReceivedDate = JobReceivedDate or JobReceivedDate = "") and
(ImportanceID = importanceID or importanceID = "") and
(JobID in (select jobid from JobCoordinatorDetails where USProjectManagerID = USPMID) );
end
I think it could be brackets
This (clientid = ClientID or ClientID = "") could be written like this
(clientid = ClientID) OR (ClientID = "" )
Its justa thought. I might be wrong
DELIMITER $$
DROP PROCEDURE if exists SearchInProgress $$
CREATE PROCEDURE `SearchInProgress`(
aclientID bigint,
aGCName varchar(250),
aTeamID int,
aUSProjectManagerID bigint,
aReceivedDate datetime,
aImportanceID bigint
)
begin
select * from jobdetails
where
(clientid = aclientID or aclientID = '') and
(GCName = aGCName or aGCName = '') and
(TeamID = aTeamID or aTeamID = '') and
(ReceivedDate = aReceivedDate or aReceivedDate = '0000-00-00 00:00:00') and
(ImportanceID = aImportanceID or aImportanceID = '') and
(JobID in (select jobid
from JobCoordinatorDetails
where USProjectManagerID = aUSProjectManagerID) or aUSProjectManagerID = '')
;
end
The parameter name and the column name must be different. I've prepended a to the parameter names.
An empty datetime value is replaced by '0000-00-00 00:00:00': https://dev.mysql.com/doc/refman/5.7/en/datetime.html
Related
Trying to create a Procedure to (Insert, Delete and, Update) values in employee_details.
CREATE DEFINER=`root`#`localhost` ALTER PROCEDURE `alter_employeedetails`(in employee_id int(11), employee_name VARCHAR(30), employee_join_date date,
employee_desgination varchar(30), employee_salary bigint(20), employee_address varchar(30),
employee_contact varchar(30), employee_email_id varchar(30)
BEGIN
IF #StatementType = 'Insert'
BEGIN
insert into employee_details values
(employee_id, employee_name, employee_join_date, employee_desgination, employee_salary, employee_address, employee_contact, employee_email_id)
END
IF #StatementType = 'Update'
BEGIN
UPDATE employee_details SET
(employee_name = #employee_name, employee_join_date = #employee_join_date, employee_designation = #employee_desgination,
employee_salary = #employee_salary, employee_address = #employee_address, employee_contact = #employee_contact, employee_email_id = #employee_email_id)
WHERE employee_id = #employee_id
END
else IF #StatementType = 'Delete'
BEGIN
DELETE FROM employee_details where employee_id = #employee_id
END
end
Quite a few errors in that code...
You forgot to prefix all the parameters with the "#" symbol.
Forgot to include "#StatementType" as a parameter.
Update had brackets around it.
You cannot specify int(11) (employee_id) or bigint(20)
(salary). It's either int / bigint (you don't specify the length
for int/bigint datatypes). And is salary correct as bigint? MSSQL has a "money"
datatype, or you could use decimal(8,2) or something similar. You
might be multiplying the salary by 100 to shift the decimal place for
all I know?
When inserting, do you really want to insert a employee Id? This would normally be an auto-incrementing primary key
Insert statement missing the fields you were populating. Required if using the "values" keyword like you had specified.
Hopefully this is closer to what you want.
ALTER PROCEDURE alter_employeedetails
(#StatementType as varchar(25), #employee_id int, #employee_name VARCHAR(30), #employee_join_date date,
#employee_designation varchar(30), #employee_salary bigint, #employee_address varchar(30),
#employee_contact varchar(30), #employee_email_id varchar(30))
AS
BEGIN
IF #StatementType = 'Insert'
BEGIN
insert into employee_details
(employee_id, employee_name, employee_join_date, employee_designation, employee_salary, employee_address, employee_contact, employee_email_id)
values
(#employee_id, #employee_name, #employee_join_date, #employee_designation, #employee_salary, #employee_address, #employee_contact, #employee_email_id)
END
ELSE IF #StatementType = 'Update'
BEGIN
UPDATE employee_details
SET
employee_name = #employee_name,
employee_join_date = #employee_join_date,
employee_designation = #employee_designation,
employee_salary = #employee_salary,
employee_address = #employee_address,
employee_contact = #employee_contact,
employee_email_id = #employee_email_id
WHERE employee_id = #employee_id
END
ELSE IF #StatementType = 'Delete'
BEGIN
DELETE FROM employee_details where employee_id = #employee_id
END
END
I have a stored procedure in mySQL when running script in phpmyadmin it give syntax error. When create manually it execute.Anyhelp would be appreciated
It shows error when i run through SQL Query when I do manually phpmyadmin stored procedure function it create it
CREATE PROCEDURE `sp_ConsolidatedFollowFeedUserList`(
_uid INT,
_startTime INT,
_endTime INT,
_activityType VARCHAR(150),
`_follow_to` INT,
`_offset` INT,
`_limit` INT
)
BEGIN
SELECT
feed.id `feed_id`,
activity.id ` activity_id`,
activity.uid AS `uid`,
activity.source_id AS follow_to,
activity.activity_type,
activity.source_id,
activity.parent_id,
activity.parent_type,
activity.post_id
FROM
`user_feed` feed
JOIN user_activity activity ON feed.activity_id = activity.id
WHERE
feed.uid = _uid
AND feed.`status` = 'ACTIVE'
AND activity.`status` = 'ACTIVE'
AND activity.activity_type IN(_activityType)
AND activity.created BETWEEN startTime AND endTime
AND activity.source_id = _follow_to
ORDER BY
activity.created DESC
LIMIT
offset, limit;
END
If your stored procedure is in a file, then you don't need the `` to protect the column fields. Then your parameter names have to match exactly offset is not matching _offset.
CREATE PROCEDURE `sp_ConsolidatedFollowFeedUserList`(
_uid INT,
_startTime INT,
_endTime INT,
_activityType VARCHAR(150),
_follow_to INT,
_offset INT,
_limit INT)
BEGIN
SELECT
feed.id AS feed_id,
activity.id AS activity_id,
activity.uid AS uid,
activity.source_id AS follow_to,
activity.activity_type,
activity.source_id,
activity.parent_id,
activity.parent_type,
activity.post_id
FROM
user_feed AS feed
JOIN user_activity activity ON feed.activity_id = activity.id
WHERE
feed.uid = _uid
AND feed.status = 'ACTIVE'
AND activity.status = 'ACTIVE'
AND activity.activity_type IN (_activityType)
AND activity.created BETWEEN _startTime AND _endTime
AND activity.source_id = _follow_to
ORDER BY activity.created DESC
LIMIT _offset, _limit;
END
I want to have a mySQL function that returns a string which is based on input parameters. So I have no blanks.
I tried the following but it tells me there is a syntax error at the beginning but I cannot see it.
CREATE DEFINER = CURRENT_USER FUNCTION `NewProc`(`add1` varchar,`add2` varchar,`add3` varchar,`town` varchar,`USState` varchar,`pcode` varchar,`country` varchar)
RETURNS varchar(250)
BEGIN
DECLARE fulladdress VARCHAR;
fulladdress = add1;
if (add2 <>'')
{fulladdress = CONCAT(fulladdress,', ',add2);}
if (add3 <> '')
{fulladdress = CONCAT(fulladdress,', ',add3);}
fulladdress = CONCAT(fulladdress,', ',town);
if (USState <> '')
{fulladdress = CONCAT(fulladdress,', ',USState);}
if (pcode <> '')
{fulladdress = CONCAT(fulladdress', ',pcode;}
fulladdress = CONCAT(fulladdress,', ',country)};
RETURN fulladdress;
END;
I would use the concat_ws function instead, then you would only have to specify the separator once, and it ignores null parameter values. Note that you'll have to adjust the size of the varchar parameters to fit your needs. Something like this should give the desired result:
DELIMITER //
CREATE DEFINER = CURRENT_USER
FUNCTION `NewProc`(
add1 varchar(20),
add2 varchar(20),
add3 varchar(20),
town varchar(20),
USState varchar(20),
pcode varchar(20),
country varchar(20)
)
RETURNS varchar(250)
BEGIN
DECLARE fulladdress VARCHAR(250);
if (add2 = '') then set add2 = null; end if;
if (add3 = '') then set add3 = null; end if;
if (town = '') then set town = null; end if;
if (USState = '') then set USState = null; end if;
if (pcode = '') then set pcode = null; end if;
RETURN concat_ws(', ', add1, add2, add3, town, USState, pcode, country);
END //
DELIMITER ;
The function can probably be written in a smarter way, but my knowledge of MySQL is rather limited...
I have a function in mysql like below:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE listId int;
SELECT ListID into listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue) AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN listId;
END$$
But it always returns Null values when I use
SELECT fnGetDropDownValue('General', 24, 18);
I don't know what I am doing wrong :(
After having the case sensitive issue with mysql columns I used to have variable names to start with _ to avoid it messing with column names. Now the stored procedure looks like this:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE _listId int;
SELECT ListID into _listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue)
AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN _listId;
END$$
This way it will work on any platform and it may be useful for others.
Is this a good candidate for a MERGE command?
Must the source data also be another table or can it be variables that are passed?
If it must be a table, is inserting the passed variables into a temp table sane?
Could you help me with the syntax?
CREATE PROCEDURE [dbo].[usp_ConvertToMerge]
#GL_DT date
,#SRC_SYS_ID varchar(60)
,#MLR_SRC_SYS_CD char(3)
,#TRSRY_FEED_DT date
,#Data varchar(20)
AS
BEGIN
IF NOT EXISTS (SELECT
#GL_DT
FROM
MLR_REBATE_IBOR_INFO_2
WHERE
[GL_DT] = #GL_DT
AND [SRC_SYS_ID] = #SRC_SYS_ID
AND [MLR_SRC_SYS_CD] = #MLR_SRC_SYS_CD
AND [TRSRY_FEED_DT] = #TRSRY_FEED_DT)
BEGIN
INSERT INTO [dbo].[MLR_REBATE_IBOR_INFO_2]
([GL_DT],
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data])
SELECT
#GL_DT
,#SRC_SYS_ID
,#MLR_SRC_SYS_CD
,#TRSRY_FEED_DT
,#Data
END
ELSE
BEGIN
UPDATE [dbo].[MLR_REBATE_IBOR_INFO_2]
SET [Data] = #Data
WHERE [GL_DT] = #GL_DT
AND [SRC_SYS_ID] = #SRC_SYS_ID
AND [MLR_SRC_SYS_CD] = #MLR_SRC_SYS_CD
AND [TRSRY_FEED_DT] = #TRSRY_FEED_DT
END
END
GO
I think I did it:
CREATE PROCEDURE MyMergeTest
#GL_DT date
,#SRC_SYS_ID char(20)
,#MLR_SRC_SYS_CD char(3)
,#TRSRY_FEED_DT date
,#Data varchar(20)
AS
BEGIN
MERGE MLR_REBATE_IBOR_INFO_2 AS target
USING
(
SELECT
#GL_DT
,#SRC_SYS_ID
,#MLR_SRC_SYS_CD
,#TRSRY_FEED_DT
,#Data
) AS source
(
GL_DT
,SRC_SYS_ID
,MLR_SRC_SYS_CD
,TRSRY_FEED_DT
,Data
)
ON (
target.GL_DT = source.GL_DT AND
target.SRC_SYS_ID = source.SRC_SYS_ID AND
target.MLR_SRC_SYS_CD = source.MLR_SRC_SYS_CD AND
target.TRSRY_FEED_DT = source.TRSRY_FEED_DT
)
WHEN MATCHED THEN
UPDATE SET Data = source.Data
WHEN NOT MATCHED THEN
INSERT
(
[GL_DT],
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data]
)
VALUES
(
[GL_DT], --<<it looks like these can eiether be the variable eg, #GL_DT, or prefixed by 'source.'
[SRC_SYS_ID],
[MLR_SRC_SYS_CD],
[TRSRY_FEED_DT],
[Data]
);
END