免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 9939 | 回复: 20

怎样SQL存储过程中执行动态SQL语句,急急急!! [复制链接]

论坛徽章:
0
发表于 2003-01-07 11:29 |显示全部楼层
create procedure referesh(in odd_table_name varchar(100), in ods_table_name varchar(100))
language sql
begin
  我想实现如下逻辑
  create table ods_table_name as
  select * from odd_table_name
  因为ods_table_name和odd_table_name都需要在运行时确定,所以需要使用动态SQL技术,请问如何实现??


end @

论坛徽章:
0
发表于 2003-01-07 11:54 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

最好用c写。

论坛徽章:
0
发表于 2003-01-07 12:06 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

能在SQL存储过程中实现此种功能吗?哪位高手知道的话请答复一下,先谢了。

论坛徽章:
0
发表于 2003-01-07 17:05 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

1、db2根据一个表的机构创建另外的一个表是:create table table1 like table2,create table table1 as select * from table2可以用吗?我记得用过不可以的啊。2、纯sql的好象不能动态表名,列名等这种功能。

论坛徽章:
0
发表于 2003-01-07 20:48 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

vlife你说的非常对(1、db2根据一个表的机构创建另外的一个表是:create table table1 like table2,create table table1 as select * from table2可以用吗?我记得用过不可以的啊。),你说的第二点(2、纯sql的好象不能动态表名,列名等这种功能)不知道有没有高手清楚请给一个准确的回答

论坛徽章:
0
发表于 2003-01-07 21:03 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

(转贴)
你试试下面的代码

create procedure referesh(in odd_table_name varchar(100), in ods_table_name varchar(100))
language sql
begin
declare sSql varchar(1000) &#59;
sSql = 'create table ' || ods_table_name ||
' as select * from ' || odd_table_name &#59;
prepare s1 from sSql&#59;
execute s1&#59;

end @



论坛徽章:
0
发表于 2003-01-07 21:04 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

(转贴)
sorry 刚才有个地方写错了

sSql = 之前要加上 Set

应该是
Set sSql = ......



论坛徽章:
0
发表于 2003-01-07 21:05 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

(转贴)
mcf你好,你所说的我都试了,我的代码如下
--文件名称:refresh.db2
--目 标:以刷新的方式把odd(Operational Data Definition )中的数据整理到ODS(Operational
-- Data Store)中
--摘 要:

--当前版本:1.0
--作 者:周海明
--完成日期:

--取代版本:
--原作者 :
--完成日期:

create procedure refresh(in odd_table varchar(100), in ods_table varchar(100), out errorCode integer, out errorLabel varchar(255))
language sql
P1 begin
declare SQLCODE integer default 0&#59;
declare stmt varchar(1024)&#59;

declare EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
set errorCode = SQLCODE&#59;

set errorCode = 0&#59;

set stmt = 'delete from ?'&#59;
set errorLabel = 'refresh:The line number is 28.'&#59;
--使用prepare语句为这个语句生成存取方案
prepare prep_stmt from stmt&#59;
set errorLabel = 'rerresh:The line number is 31.'&#59;
--使用execute语句执行动态sql语句
execute prep_stmt using ods_table&#59;


set stmt = 'insert into ? '||
'select * from ?'&#59;
set errorLabel = 'refresh:The line number is 38.'&#59;
--使用prepare语句为这个语句生成存取方案
prepare prep_stmt from stmt&#59;
set errorLabel = 'refresh:The line number is 41.'&#59;
--使用execute语句执行动态sql语句
execute prep_stmt using ods_table, odd_table&#59;

set errorLable = ''&#59; --当全部sql语句都正确执行时把errorLable变量赋值为空串

end P1

但我在DB2 8.1版本的开发中心编译它的时候系统报错,报错信息如下
TEST.refresh ― 构建已启动。
创建 存储过程 返回 -198。
[IBM][CLI Driver][DB2/NT] SQL0198N PREPARE 或 EXECUTE IMMEDIATE 语句的语句字符串为空白或空。 SQLSTATE=42617


TEST.refresh ― 构建失败。
TEST.refresh ― 回滚成功完成。

我查了查SQL0198N错误信息的内容,如下
sqlcode: -197

sqlstate: 42877

SQL0198N The statement string of the PREPARE or EXECUTE IMMEDIATE statement is blank or empty.

Explanation: The host variable that was the object of the PREPARE or EXECUTE IMMEDIATE statement either contained all blanks or was an empty string.

The PREPARE or EXECUTE IMMEDIATE could not be completed.

User Response: Correct the logic of the program to ensure that a valid SQL statement is provided in the operand of the PREPARE or EXECUTE IMMEDIATE statement before it is executed.

我编译其它不含动态SQL的SQL过程都能通过,不知道这有什么问题,请高手帮忙回答一下!

论坛徽章:
0
发表于 2003-01-07 21:11 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

vlife所说的“纯sql的好象不能动态表名,列名”,但下面的代码(我没用动态表名、列名)
create procedure refresh(in id integer)
language sql
P1: begin
  declare SQLCODE integer default 0&#59;
  declare stmt varchar(1024)&#59;

  declare EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
    set errorCode = SQLCODE&#59;

  set errorCode = 0&#59;

  set stmt = 'insert into t_login(F_ID) '||
             'values(?)'&#59;
  set errorLabel = 'refresh:The line number is 28.'&#59;
  --使用prepare语句为这个语句生成存取方案
  prepare ps from stmt&#59;
  set errorLabel = 'rerresh:The line number is 31.'&#59;
  --使用execute语句执行动态sql语句
  execute ps using id&#59;

  set errorLable = ''&#59; --当全部sql语句都正确执行时把errorLable变量赋值为空串

end P1
编译后出现跟上面一样的错误SQL0198N,所以我认为不是“纯sql的好象不能动态表名,列名”而是有其它的问题

论坛徽章:
0
发表于 2003-01-08 10:25 |显示全部楼层

怎样SQL存储过程中执行动态SQL语句,急急急!!

  你用变量来代替?试试,比如
  set stmt='select * from ?'  --你以前的语句
  prepare s1 from stmt&#59;
  execute s1 using inout_tablename&#59;
换成
  set your_table=input_tablename&#59;
  set stmt='select * from '||your_table&#59;
  prepare s1 from stme&#59;
  execute s1&#59;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP