Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Create function problem in db2 9.1 luw

$
0
0
Hi,
I have a table and data as follows;

create table htab (id char(2), name varchar(20));

insert into htab values('1','a');
insert into htab values('1','b');
insert into htab values('2','c');
insert into htab values('3','d');
insert into htab values('3','e');
insert into htab values('3','f');
insert into htab values('4','g');

select * from htab

id name
1 a
1 b
2 c
3 d
3 e
3 f
4 g

I want to show data as

id names
1 a,b
2 c
3 d,e,f
4 g

I have the Function in SQL Server which is working fine as
============
create function dbo.fxn_get_names(@id char(2))
returns varchar(100)
as
begin
declare @names varchar(100)
select @names=isnull(@names+',','') + name from htab where id = @id
return @names
end
GO


select distinct id, names=dbo.fxn_get_names(id) from htab
GO
======================
Actually i want to convert it to DB2 9.1 LUW and tried as follows
xxxxxxxxxxxxxxxxx
CREATE FUNCTION MYFNC(MID CHAR)
RETURNS VARCHAR(100)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE MNAMES VARCHAR(100);
SET MNAMES='';
SELECT (MNAMES || ',' || NAME) MNAMES FROM HTAB WHERE ID = MID;
RETURN MNAMES
END
xxxxxxxxxxxxxxxxx
Getting the following problem
-------------
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token ")" was found following "E MNAMES VARCHAR(100".
Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=5.
SQLSTATE=42601

SET MNAMES=''
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "MNAMES" was found following "SET ". Expected
tokens may include: "JOIN <joined_table>". SQLSTATE=42601

SELECT (MNAMES || ',' || NAME) MNAMES FROM HTAB WHERE ID = MID
SQL0206N "MID" is not valid in the context where it is used. SQLSTATE=42703

RETURN MNAMES END
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "MNAMES" was found following "RETURN ".
Expected tokens may include: "JOIN <joined_table>". SQLSTATE=42601

SQL0104N An unexpected token "MNAMES" was found following "RETURN ". Expected tokens may include: "JOIN <joined_table>
-------------

Anyone can give me the solution...


-- Humayun

Viewing all articles
Browse latest Browse all 13329

Trending Articles