Unable to retrieve value returned from mysql function
Since a week am working with MYSQL , got to execute the Stored Procedure as well as Views but Facing some problem retrieving the values returned from a function. Here's the Function:
CREATE DEFINER=`root`@`localhost` FUNCTION `GetProductIdsStringByEnquiryId`
(
InEnquiryId int
) RETURNS varchar(4000) CHARSET utf8
BEGIN
DECLARE InProductIds varchar(4000);
DECLARE ProductId varchar(50);
DECLARE x,y,z INT;
DECLARE sp1_cursor CURSOR FOR SELECT ProductId FROM enquiryproductid where
EnquiryId=InEnquiryId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET z = 1;
SET InProductIds='';
OPEN sp1_cursor;
REPEAT
FETCH sp1_cursor INTO ProductId;
SETInProductIds=concat(InProductIds,ProductId,',');
UNTIL (z=1)
END REPEAT;
CLOSE sp1_cursor;
RETURN InProductIds ;
END
I was initially working with SQL SERVER 2005, and the function which I have written in their I tried converting it as above in MYSQL, Here's the SQL Function Code:
CREATE function [dbo].[GetBranchIdsStringByEmployeeId]
(
@EmployeeId as integer
)
returns nvarchar(4000)
as
begin
declare @BranchIds as nvarchar(4000)
set @BranchIds=''
if exists(select 1 from dbo.BranchEmployees where EmployeeId=@EmployeeId)
begin
select @BranchIds=@BranchIds+cast(BranchId as nvarchar(50))
+',' from dbo.BranchEmployees where EmployeeId=@EmployeeId
order by BranchId
end
return @BranchIds
end
Can anybody Please Let me know if the Function What I have written开发者_如何学运维 in MYSQL is in ProperManner or not? Please do help me out. Thank You.
Not read fully through it, but few comments
Variable assignment in mysql uses :=
(in set @variable
it is ok to use =
, in select @variable:=@variable+1
)
Are you trying to
SELECT group_concat(BranchId)
FROM dbo.BranchEmployees
WHERE EmployeeId = @EmployeeId
?
精彩评论