DB2 自定义函数 20
这是函数脚本CREATEFUNCTIONPUT_LINE(SMALLINT,VARCHAR(4000))RETURNSVARCHAR(1)NOTFENCEDRETURNS...
这是函数脚本
CREATE FUNCTION PUT_LINE
(SMALLINT,VARCHAR(4000))
RETURNS VARCHAR(1)
NOT FENCED RETURNS NULL ON NULL INPUT NO SQL DBINFO
EXTERNAL ACTION LANGUAGE C PARAMETER STYLE DB2SQL
EXTERNAL NAME 'put_line!put_line';
运行完了总报-104错误,错误如下:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;'put_line!put_line';END-OF-STATEMENT, DRIVER=3.57.82
求高手给看看呐!!! 展开
CREATE FUNCTION PUT_LINE
(SMALLINT,VARCHAR(4000))
RETURNS VARCHAR(1)
NOT FENCED RETURNS NULL ON NULL INPUT NO SQL DBINFO
EXTERNAL ACTION LANGUAGE C PARAMETER STYLE DB2SQL
EXTERNAL NAME 'put_line!put_line';
运行完了总报-104错误,错误如下:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;'put_line!put_line';END-OF-STATEMENT, DRIVER=3.57.82
求高手给看看呐!!! 展开
2个回答
推荐于2017-10-07 · 知道合伙人教育行家
关注
展开全部
Db2 自定义函数和存储过程初步
一、自定义函数
先做个简单的,将输入参数原样返回。
CREATE FUNCTION ADMINISTRATOR.FUN1
(AAA VARCHAR(4)
)
RETURNS VARCHAR(4)
SPECIFIC ADMINISTRATOR.FUN1
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
STATIC DISPATCH
CALLED ON NULL INPUT
EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE bbb VARCHAR(4);
set bbb = aaa;
return bbb;
END;
这是经过辅助工具生成的源码,我们可以发现如下几个特点:
1、 在函数名定义中加入(输入参数名 数据类型)
2、 随后定义返回值类型
3、 用BEGIN ATOMIC和END;作为起止标示
4、 用set 定义赋值
5、 用return定义返回值
创建成功的函数怎莫说没找到?不要从字面上理解,很有可能是你输入函数的参数数据类型不匹配造成的,这在面向对象中不是叫多态吗。
改一下就可以输入整数了:
CREATE FUNCTION ADMINISTRATOR.FUN2
(AAA INTEGER
)
RETURNS INTEGER
SPECIFIC ADMINISTRATOR.SQL060220111756000
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
STATIC DISPATCH
CALLED ON NULL INPUT
EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE bbb INTEGER;
set bbb = aaa;
return bbb;
END;
以上写的函数叫什莫玩意,下面做个和数据库打交道的,反正函数主要就是用于做对照的,返回值唯一。
CREATE FUNCTION ADMINISTRATOR.FUN3 (AAA INTEGER )
RETURNS VARCHAR(20)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE bbb VARCHAR(20);
set bbb = (select MONTH from IWH.LOOKUP_TIME where MONTH_ID = AAA);
return bbb;
END;
好了,懂得一些皮毛了。
二、存储过程
存储过程和函数很类似,只是用于批量实现一段逻辑的,而不是为了那个返回值,还有就是定义格式有些不同。
db2中提供了很多例子,下面就是一个定义游标和loop循环的。
-----------------------------------------------------------------------------
-- Licensed Materials - Property of IBM
--
-- Governed under the terms of the International
-- License Agreement for Non-Warranted Sample Code.
--
-- (C) COPYRIGHT International Business Machines Corp. 1995 - 2002
-- All Rights Reserved.
--
-- US Government Users Restricted Rights - Use, duplication or
-- disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-----------------------------------------------------------------------------
--
-- SOURCE FILE NAME: loop.db2
--
-- SAMPLE: To create the LOOP_UNTIL_SPACE SQL procedure
--
-- To create the SQL procedure:
-- 1. Connect to the database
-- 2. Enter the command "db2 -td@ -vf loop.db2"
--
-- To call the SQL procedure from the command line:
-- 1. Connect to the database
-- 2. Enter the following command:
-- db2 "CALL loop_until_space (?)"
--
-- You can also call this SQL procedure by compiling and running the
-- C embedded SQL client application, "loop", using the loop.sqc
-- source file available in the sqlproc samples directory.
-----------------------------------------------------------------------------
--
-- For more information on the sample scripts, see the README file.
--
-- For information on creating SQL procedures, see the Application
-- Development Guide.
--
-- For information on using SQL statements, see the SQL Reference.
--
-- For the latest information on programming, building, and running DB2
-- applications, visit the DB2 application development website:
-- http://www.software.ibm.com/data/db2/udb/ad
-----------------------------------------------------------------------------
CREATE PROCEDURE loop_until_space(OUT counter INT)
LANGUAGE SQL
BEGIN
DECLARE v_firstnme VARCHAR(12);
DECLARE v_midinit CHAR(1);
DECLARE v_lastname VARCHAR(15);
DECLARE v_counter SMALLINT DEFAULT 0;
DECLARE c1 CURSOR FOR
SELECT firstnme, midinit, lastname
FROM employee
ORDER BY midinit DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET counter = -1;
-- initialize OUT parameter
SET counter = 0;
OPEN c1;
fetch_loop:
LOOP
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
-- Use a local variable for the iterator variable
-- because SQL procedures only allow you to assign
-- values to an OUT parameter
SET v_counter = v_counter + 1;
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF;
END LOOP fetch_loop;
CLOSE c1;
-- Now assign the value of the local
-- variable to the OUT parameter
SET counter = v_counter;
END;
说明:
1、 注释中写的很明白了,如何创建和执行。就是要注意,如果你想把代码拿出来执行,要把END @改为END;
2、 不用C的 loop.sqc就可以了
3、 注意与函数的不同:
a) 起止标示begin\end不同
b) 返回值定义不同,不定义return而在函数名定义中加OUT。。。。。。
格式定义
1.(IN deptNumber SMALLINT, OUT medianSalary DOUBLE)
使用(51,?)
2.游标
DECLARE c1 CURSOR FOR
SELECT firstnme, midinit, lastname
FROM employee
ORDER BY midinit DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET counter = -1;
Open C1
Close C1
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
3、Loop循环
fetch_loop:
LOOP
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
SET v_counter = v_counter + 1;
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF;
END LOOP fetch_loop;
4、if
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF
5、 CASE v_mod
WHEN 0 THEN
END CASE;
6、WHILE v_counter < (v_numRecords / 2 + 1) DO
SET v_salary1 = v_salary2;
FETCH c1 INTO v_salary2;
SET v_counter = v_counter + 1;
END WHILE;
一、自定义函数
先做个简单的,将输入参数原样返回。
CREATE FUNCTION ADMINISTRATOR.FUN1
(AAA VARCHAR(4)
)
RETURNS VARCHAR(4)
SPECIFIC ADMINISTRATOR.FUN1
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
STATIC DISPATCH
CALLED ON NULL INPUT
EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE bbb VARCHAR(4);
set bbb = aaa;
return bbb;
END;
这是经过辅助工具生成的源码,我们可以发现如下几个特点:
1、 在函数名定义中加入(输入参数名 数据类型)
2、 随后定义返回值类型
3、 用BEGIN ATOMIC和END;作为起止标示
4、 用set 定义赋值
5、 用return定义返回值
创建成功的函数怎莫说没找到?不要从字面上理解,很有可能是你输入函数的参数数据类型不匹配造成的,这在面向对象中不是叫多态吗。
改一下就可以输入整数了:
CREATE FUNCTION ADMINISTRATOR.FUN2
(AAA INTEGER
)
RETURNS INTEGER
SPECIFIC ADMINISTRATOR.SQL060220111756000
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
STATIC DISPATCH
CALLED ON NULL INPUT
EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE bbb INTEGER;
set bbb = aaa;
return bbb;
END;
以上写的函数叫什莫玩意,下面做个和数据库打交道的,反正函数主要就是用于做对照的,返回值唯一。
CREATE FUNCTION ADMINISTRATOR.FUN3 (AAA INTEGER )
RETURNS VARCHAR(20)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE bbb VARCHAR(20);
set bbb = (select MONTH from IWH.LOOKUP_TIME where MONTH_ID = AAA);
return bbb;
END;
好了,懂得一些皮毛了。
二、存储过程
存储过程和函数很类似,只是用于批量实现一段逻辑的,而不是为了那个返回值,还有就是定义格式有些不同。
db2中提供了很多例子,下面就是一个定义游标和loop循环的。
-----------------------------------------------------------------------------
-- Licensed Materials - Property of IBM
--
-- Governed under the terms of the International
-- License Agreement for Non-Warranted Sample Code.
--
-- (C) COPYRIGHT International Business Machines Corp. 1995 - 2002
-- All Rights Reserved.
--
-- US Government Users Restricted Rights - Use, duplication or
-- disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-----------------------------------------------------------------------------
--
-- SOURCE FILE NAME: loop.db2
--
-- SAMPLE: To create the LOOP_UNTIL_SPACE SQL procedure
--
-- To create the SQL procedure:
-- 1. Connect to the database
-- 2. Enter the command "db2 -td@ -vf loop.db2"
--
-- To call the SQL procedure from the command line:
-- 1. Connect to the database
-- 2. Enter the following command:
-- db2 "CALL loop_until_space (?)"
--
-- You can also call this SQL procedure by compiling and running the
-- C embedded SQL client application, "loop", using the loop.sqc
-- source file available in the sqlproc samples directory.
-----------------------------------------------------------------------------
--
-- For more information on the sample scripts, see the README file.
--
-- For information on creating SQL procedures, see the Application
-- Development Guide.
--
-- For information on using SQL statements, see the SQL Reference.
--
-- For the latest information on programming, building, and running DB2
-- applications, visit the DB2 application development website:
-- http://www.software.ibm.com/data/db2/udb/ad
-----------------------------------------------------------------------------
CREATE PROCEDURE loop_until_space(OUT counter INT)
LANGUAGE SQL
BEGIN
DECLARE v_firstnme VARCHAR(12);
DECLARE v_midinit CHAR(1);
DECLARE v_lastname VARCHAR(15);
DECLARE v_counter SMALLINT DEFAULT 0;
DECLARE c1 CURSOR FOR
SELECT firstnme, midinit, lastname
FROM employee
ORDER BY midinit DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET counter = -1;
-- initialize OUT parameter
SET counter = 0;
OPEN c1;
fetch_loop:
LOOP
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
-- Use a local variable for the iterator variable
-- because SQL procedures only allow you to assign
-- values to an OUT parameter
SET v_counter = v_counter + 1;
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF;
END LOOP fetch_loop;
CLOSE c1;
-- Now assign the value of the local
-- variable to the OUT parameter
SET counter = v_counter;
END;
说明:
1、 注释中写的很明白了,如何创建和执行。就是要注意,如果你想把代码拿出来执行,要把END @改为END;
2、 不用C的 loop.sqc就可以了
3、 注意与函数的不同:
a) 起止标示begin\end不同
b) 返回值定义不同,不定义return而在函数名定义中加OUT。。。。。。
格式定义
1.(IN deptNumber SMALLINT, OUT medianSalary DOUBLE)
使用(51,?)
2.游标
DECLARE c1 CURSOR FOR
SELECT firstnme, midinit, lastname
FROM employee
ORDER BY midinit DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET counter = -1;
Open C1
Close C1
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
3、Loop循环
fetch_loop:
LOOP
FETCH c1 INTO
v_firstnme, v_midinit, v_lastname;
SET v_counter = v_counter + 1;
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF;
END LOOP fetch_loop;
4、if
IF v_midinit = ' ' THEN
LEAVE fetch_loop;
END IF
5、 CASE v_mod
WHEN 0 THEN
END CASE;
6、WHILE v_counter < (v_numRecords / 2 + 1) DO
SET v_salary1 = v_salary2;
FETCH c1 INTO v_salary2;
SET v_counter = v_counter + 1;
END WHILE;
ZESTRON
2024-09-04 广告
2024-09-04 广告
表面污染分析包括评估表面上存在的颗粒、残留物或物质。通过利用显微镜、光谱学和色谱法等技术,分析人员可以识别和表征污染物,以确定其成分和来源。这种分析在电子、制药和制造等各个行业中至关重要,以确保产品质量、性能和安全性。了解表面污染有助于实施...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询