首页 > 数据库 >mysql到达梦存储过程常见问题

mysql到达梦存储过程常见问题

时间:2023-09-04 14:35:48浏览次数:40  
标签:存储 常见问题 name create mysql test table id select

1.1 变量的使用

create or replace procedure e_test()

as

begin

 set strsql ='select id into @eid from test2 order by id limit 1,10';

 insert into test select id,name from test2 where id in (eid);

 set stst =strsql;

 execute stst;

end;

变量需要提前定义

create or replace procedure e_test()

as

strsql varchar(1000);

stst varchar(1000);

eid int;

begin

 set strsql ='select id  into eid from test2 order by id limit 1,10';

 insert into test select id, name from test2 where id in (eid);

 set stst =strsql;

 execute stst;

end;

执行变量不能使用execute ,采用print

create or replace procedure e_test()

as

strsql varchar(1000);

stst varchar(1000);

eid int;

begin

 set strsql ='select id  into eid from test2 order by id limit 1,10';

 insert into test select id, name from test2 where id in (eid);

 set stst =strsql;

 print stst;

end;

1.2 Interval包含变量计算

mysql

select date_format(date_add(current_date(),interval -DAY(current_date())+1 day),'%Y-%M-%D');

达梦interval只能接常量字符串,不支持计算表达式 

select date_format (trunc(current_date()-DAY(current_date())+1) ,'%Y-%M-%D')    ;

1.3 DATEDIFF 函数

mysql

SELECT DATEDIFF(date'2003-01-01', DATE '2002-01-01');

达梦

SELECT DATEDIFF(day,DATE'2002-01-01', DATE '2003-01-01');

1.4 Delete using 连表删除

mysql

delete from s using st1 s,(select id from st2 where cbf=0 and status=1)z where s.id=z.id

达梦

delete from st1 s where exists (select 1 from st2 z where s.id=z.id and   z.cbf=0 and z.status=1)

1.5 GROUP_CONCAT

mysql

SELECT GROUP_CONCAT(name SEPARATOR ';')  from wm_test;

达梦

select replace(WM_CONCAT(name),',', ';') from wm_test;

select listagg( name,';') within group (order by name) from wm_test

--如报字符串截断错误,改成listagg2

select listagg2( name,';') within group (order by name) from wm_test

1.6 convert

mysql

select convert('12',signed);

select convert('2012-01-01',date)

达梦

select convert(integer,'12');

select convert(date ,'2012-01-01')

1.7 information_schema.columns

mysql

select table_name ,CHARACTER_MAXIMUM_LENGTH from information_schema.columns

where table_schema ='TEST' and CHARACTER_MAXIMUM_LENGTH is not null;

达梦

select  u.table_name ,max(u.data_length) from dba_tab_columns u,dba_tables d

where u.table_name =d.table_name and d.owner='DMHR' and u.data_type='VARCHAR' group by u.table_name;

1.8 FIND_IN_SET

mysql

select t.* from TABLE_T t where find_in_set('6', t.ids) > 0

达梦老版本不支持,新版本执行,改写如下

https://eco.dameng.com/community/article/a92bc26bbf73142116a7ec0d3daadcdc

1.9 Concat

mysql

select concat('a');

达梦需要两个参数

select concat('a','');

1.10 存储过程动态sql

 

create or replace procedure atmp ()

 

as

 

begin

 

drop table if exists test;

 

create table test (id int,name varchar(50));

 

insert into test values (1,'aa'),(2,'bb');

 

commit;

 

end;

将ddl语句改成动态sql之后,编译不报错,但执行报错

create or replace procedure atmp ()

as

begin

execute immediate 'drop table if exists test';

execute immediate 'create table test (id int,name varchar(50))';

insert into test values (1,'aa'),(2,'bb');

commit;

end;

需要存储过程里面的表全部改写成动态sql

 

create or replace procedure atmp ()

 

as

 

begin

 

execute immediate 'drop table if exists test';

 

execute immediate 'create table test (id int,name varchar(50))';

 

execute immediate 'insert into test values (1,''aa''),(2,''bb'')';

 

commit;

 

end;

--如果动态sql语句里面语法错误,编译不报错,但调用存储过程时会报错。如下所示

create or replace procedure atmp ()

as

begin

execute immediate 'drop table if exists test';

execute immediate 'create table test (id int(8),name varchar(50))';

execute immediate 'insert into test values (1,''aa''),(2,''bb'')';

commit;

end;

 

1.11 存储过程调用包含临时表

创建两个存储过程,存储过程tmpc2 包含创建临时表tmp2和调用存储过程tmpc,而存储过程tmp包含临时表tmp2的使用。编译存储过程tmpc2 时会提示存储过程tmpc无效,编译存储过程tmpc时,会提示tmp2表不存在,陷入死循环。

create or replace procedure tmpc2 ()

as

begin

  execute immediate 'drop table if exists tmp2';

  execute immediate 'create temporary table tmp2( id int,name varchar(200))';

  call tmpc();

end;

 

create or replace procedure tmpc ()

as

begin

insert into tmp2 values (1,'aa');

end;

将临时表改成普通表

drop table if exists tmp2;

create temporary table tmp2( id int,name varchar(200));

create or replace procedure tmpc ()

as

begin

insert into tmp2 values (1,'aa');

commit;

end;

create or replace procedure tmpc2 ()

as

begin

 execute immediate 'truncate table tmp2';

  call tmpc();

end;

 

 

 

标签:存储,常见问题,name,create,mysql,test,table,id,select
From: https://www.cnblogs.com/fangzpa/p/17676914.html

相关文章

  • MySQL忘记root密码解决方案
    Linux系统-部署-运维系列导航 场景一:重置root密码mysql登录密码为password()算法加密,解密成本太高,以下为通用方案;原理:mysql提供了特殊启动方式,即跳过权限表验证,启动后,登录不需要提供密码;登录后,即可修改mysql数据库的user表,重置密码,然后刷新权限,重启mysql服务即可;注意:此时my......
  • docker桌面端安装mysql
    参考 https://www.yzktw.com.cn/post/735256.html1,搜索Images,mysql,选择合适的版本pull2,Images,中点击三角箭头,run,会弹出新建Containers弹框Containersname,随便写Ports需要写0Volumes选安装地址EnvironmentVarialbes需要加上密码MYSQL_ROOT_PASSWORD: 123456 3,运行......
  • keepalived实现MySQL MGR高可用(keepalived 2.2.7 + mysql 5.7.35)
    Linux系统-部署-运维系列导航 一、架构介绍MySQLMGR实现了MySQL服务的高可用、高扩展、高可靠,但在客户端只能同时连接一台服务实例,在master切换后,客户端无法感知并自动切换,所以需要搭配keepalived实现MySQLMGR集群在客户端的高可用。 二、架构搭建架构设计MySQLMGR一......
  • Linux 服务器下C++开发找不到mysql.h
    问题背景腾讯云服务器,linuxcentOS7内核,mysql版本5.5为宝塔腾讯云版自动安装C++用cmake编译时找不到mysql.h解决1.首先尝试yum安装mysql-devel包,但是yum让我直接下载了mariadb相关的包,下载时它,与原有包冲突bt-mysql55-5.5.62-1.el7.x86_64,但是删除原有包后mysql启动有问......
  • 浅谈Mysql读写分离的坑以及应对的方案 | 京东云技术团队
    一、主从架构为什么我们要进行读写分离?个人觉得还是业务发展到一定的规模,驱动技术架构的改革,读写分离可以减轻单台服务器的压力,将读请求和写请求分流到不同的服务器,分摊单台服务的负载,提高可用性,提高读请求的性能。上面这个图是一个基础的Mysql的主从架构,1主1备3从。这种架构是客户......
  • 将MySQL分区表转换成普通表
    将MySQL分区表转换成普通表MySQL支持分区表,这种表可以将数据分散到多个存储区中。但是,有时候我们可能需要把分区表转换成普通表,本文将介绍如何完成这个操作。备份数据在进行任何表修改操作前,都需要先进行数据备份。在备份数据之前,需要关闭所有对该表的写入操作。取消分区要将分区表......
  • mysql中文全文搜索
    在MySQL5.7.6之前,全文索引只支持英文全文索引,不支持中文全文索引,需要利用分词器把中文段落预处理拆分成单词,然后存入数据库。从MySQL5.7.6开始,MySQL内置了ngram全文解析器,用来支持中文、日文、韩文分词。本文使用的MySQL版本是5.7.22,InnoDB数据库引擎。为什么要用全文索引呢?......
  • 浅谈Mysql读写分离的坑以及应对的方案
    一、主从架构为什么我们要进行读写分离?个人觉得还是业务发展到一定的规模,驱动技术架构的改革,读写分离可以减轻单台服务器的压力,将读请求和写请求分流到不同的服务器,分摊单台服务的负载,提高可用性,提高读请求的性能。上面这个图是一个基础的Mysql的主从架构,1主1备3从。这种架构是......
  • mysql 大表如何ddl
    大家好,我是蓝胖子,mysql对大表(千万级数据)的ddl语句,在生产上执行时一定要千万小心,一不小心就有可能造成业务阻塞,数据库io和cpu飙高的情况。今天我们就来看看如何针对大表执行ddl语句。通过这篇文章,你能了解到下面的知识点,传统ddl和onlineddl的区别mysql的ddl经过了几个版......
  • kubernetes存储方案(一): Heketi+glusterfs
    环境介绍主机名ip安装软件gluster-server110.1.30.30gluster-server,Heketigluster-server210.1.30.32gluster gluster安装GlusterFS(所有节点)配置hosts解析cat>>/etc/hosts<<EOF10.1.30.30gluster-server110.1.30.32gluster-server2E......