Issue to execute mysql stored procedure file in phpmyadmin - mysql

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

Related

sql server, procedures and function

I have a Studentstatus table which gets a list of students from student table.
I made this procedure to update or create a new record, but I get the following error:
Procedure or function SaveAdvStudStata has too many arguments specified.
CREATE PROCEDURE [dbo].[SaveAdvStudStata]
#studentId int,
#stata int,
#description varchar(MAX),
#date DATE,
#issuedBy nvarchar(128)
AS
BEGIN
if exists (select StudentId
from dbo.StudentStata
where StudentId = #studentId
)
begin
Update dbo.StudentStata
set Stata = #stata
,Description = #description
,Date = #date
,IssuedBy = #issuedBy
where StudentId = #studentId
end
else
begin
Insert into dbo.StudentStata(StudentId
,Stata
,Description
,Date
,IssuedBy
)
VALUES (#studentId
,#stata
,#description
,#date
,#issuedBy
)
end
END

MySQL stored procedure is not working

I am new to Mysql. I have a stored procedure,below is my SP
CREATE DEFINER=`root`#`localhost` PROCEDURE `insertGroup`(IN startdate date,
IN enddate date,
IN amount int,
IN groupname char(50))
BEGIN
DECLARE c_id INT;
set c_id =(select id from c_begin where startdate = startdate and enddate = enddate and amount = amount);
insert into c_group (c_beginid,Groupname)
select * from (select c_id,groupname) as temp
where not exists (
select c_beginid,groupname from c_group where c_beginid = c_id and groupname = groupname
) ;
END
I am calling it like
call insertGroup ('2014-01-01','2015-12-31',100000,'GroupD');
But it is not inserting any row. I am trying to insert into "c_group" if the c_beginid and groupname is not exists.
But when i try like below
insert into c_group (c_beginid,Groupname)
select * from (select 1,'GroupD') as temp
where not exists (
select c_beginid,groupname from c_group where c_beginid = 1 and groupname = 'GroupD'
) limit 1 ;
it is working.
So What went wrong in my "insertGroup" SP. Can any one help me ?
Thanks ,
Rename your input parameter to make it clear to the DB engine when you mean the parameter and when the column name.
where startdate = startdate and enddate = enddate and amount = amount
is always true since the engine thinks you compare a column to itself.

MySQL stored procedure will execute but conditions not working

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

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.

Use of transaction in stored procedure

I create a stored procedure and I want to use transaction in this. So how can I use transaction in this stored procedure? Which part will start the transaction because INSERT and UPDATE both are together in this stored procedure.
This is my stored procedure:
ALTER PROCEDURE [dbo].[usp_InsertUpdateADAlertRecipient]
#AD_User_ID int,
#AD_Client_ID int,
#AD_Org_ID int,
#IsActive bit,
#Created datetime,
#Createdby int,
#Updated datetime,
#Updatedby int,
#AD_Alertrecipient_ID int,
#AD_AlertRule_ID int,
#AD_Role_ID int,
#Sendemail bit
AS
SET NOCOUNT ON
IF EXISTS(SELECT [AD_Alertrecipient_ID] FROM [dbo].[AD_AlertRecipient] WHERE [AD_Alertrecipient_ID] = #AD_Alertrecipient_ID)
BEGIN
UPDATE [dbo].[AD_AlertRecipient] SET
[AD_User_ID] = #AD_User_ID,
[AD_Client_ID] = #AD_Client_ID,
[AD_Org_ID] = #AD_Org_ID,
[IsActive] = #IsActive,
[Created] = #Created,
[Createdby] = #Createdby,
[Updated] = #Updated,
[Updatedby] = #Updatedby,
[AD_AlertRule_ID] = #AD_AlertRule_ID,
[AD_Role_ID] = #AD_Role_ID,
[Sendemail] = #Sendemail
WHERE
[AD_Alertrecipient_ID] = #AD_Alertrecipient_ID
select #AD_Alertrecipient_ID
END
ELSE
BEGIN
INSERT INTO [dbo].[AD_AlertRecipient] (
[AD_User_ID],
[AD_Client_ID],
[AD_Org_ID],
[IsActive],
[Created],
[Createdby],
[Updated],
[Updatedby],
--[AD_Alertrecipient_ID],
[AD_AlertRule_ID],
[AD_Role_ID],
[Sendemail]
) VALUES (
#AD_User_ID,
#AD_Client_ID,
#AD_Org_ID,
#IsActive,
#Created,
#Createdby,
#Updated,
#Updatedby,
--#AD_Alertrecipient_ID,
#AD_AlertRule_ID,
#AD_Role_ID,
#Sendemail
)
SELECT SCOPE_IDENTITY()
END
Thanks and waiting for your reply .