I need postgresql version of this function

Multi tool use
I need postgresql version of this function
I am newbie to postgresql and I need a postgresql version of this function - could you help me please?
CREATE FUNCTION [dbo].[getpersonname]
(@commid INT)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @personnname varchar(255)
SELECT
@personnname = COALESCE(@personname + ',', '') +
ISNULL(CONVERT(VARCHAR(50), pers.personnname ),'')
FROM
dbo.comlink comli
INNER JOIN
dbo.person pers ON personid= comli_personid
WHERE
comli.comli_commid = @commid
RETURN @personnname
END
1 Answer
1
If I understand that code correctly it returns all names separated with commas.
That can easily be done with a simple SQL statement in Postgres:
create FUNCTION get_person_name(p_commid int)
RETURNS text
AS
$$
SELECT string_agg(pers.personnname, ',')
FROM dbo.comlink comli
JOIN dbo.person pers ON personid = comli_personid
WHERE comli.comli_commid= p_commid;
$$
language sql;
This can also be done in SQL Server using the same query in SQL Server 2017 and Azure SQL Database, which support
STRING_AGG
.– Dan Guzman
Jul 1 at 10:13
STRING_AGG
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What does the function do?
– a_horse_with_no_name
Jul 1 at 5:11