前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PL/SQL编程—变量

PL/SQL编程—变量

作者头像
翎野君
发布2023-05-12 15:44:28
2430
发布2023-05-12 15:44:28
举报
文章被收录于专栏:翎野君
代码语言:javascript
复制
SQL> declare
  2  c_tax_rate number(3,2):=0.03;
  3  v_name varchar2(20);
  4  v_passwd varchar2(20);
  5  v_sale number(7,2);
  6  v_tax_sale number(7,2);
  7  begin
  8  select name,passwd,salary into v_name,v_passwd,v_sale from mytest where id=&inpt;
  9  v_tax_sale:=v_sale*c_tax_rate;
 10  dbms_output.put_line('name:'||v_name||' passwd:'||v_passwd||' taxsale:'||v_tax_sale);
 11  end;
 12  /
 
name:123 passwd:123 taxsale:3
 
PL/SQL procedure successfully completed

在声明变量的时候可以设置类型为动态的取表中的字段的类型一致

代码语言:javascript
复制
SQL> insert into mytest values('4','125555','passwd','50');
 
1 row inserted
 
SQL> select * from mytest;
 
ID    NAME                 PASSWD                                                                                         SALARY
----- -------------------- -------------------- --------------------------------------------------------------------------------
1     123                  123                                                                                               100
2     gaodingle!!!!        123                                                                                               100
3     gagaga               123                                                                                               100
4     125555               passwd                                                                                             50
 
SQL>
SQL> declare
  2  c_tax_rate number(3,2):=0.03;
  3  v_name varchar2(4);
  4  v_passwd varchar2(20);
  5  v_sale number(7,2);
  6  v_tax_sale number(7,2);
  7  begin
  8  select name,passwd,salary into v_name,v_passwd,v_sale from mytest where id=&inpt;
  9  v_tax_sale:=v_sale*c_tax_rate;
 10  dbms_output.put_line('name:'||v_name||' passwd:'||v_passwd||' taxsale:'||v_tax_sale);
 11  end;
 12  /
 
declare
c_tax_rate number(3,2):=0.03;
v_name varchar2(4);
v_passwd varchar2(20);
v_sale number(7,2);
v_tax_sale number(7,2);
begin
select name,passwd,salary into v_name,v_passwd,v_sale from mytest where id=4;
v_tax_sale:=v_sale*c_tax_rate;
dbms_output.put_line('name:'||v_name||' passwd:'||v_passwd||' taxsale:'||v_tax_sale);
end;
 
ORA-06502: PL/SQL: 数字或值错误 :  字符串缓冲区太小
ORA-06512: 在 line 9
 
SQL>
SQL> declare
  2  c_tax_rate number(3,2):=0.03;
  3  v_name mytest.name%type;
  4  v_passwd varchar2(20);
  5  v_sale number(7,2);
  6  v_tax_sale number(7,2);
  7  begin
  8  select name,passwd,salary into v_name,v_passwd,v_sale from mytest where id=&inpt;
  9  v_tax_sale:=v_sale*c_tax_rate;
 10  dbms_output.put_line('name:'||v_name||' passwd:'||v_passwd||' taxsale:'||v_tax_sale);
 11  end;
 12  /
 
name:125555 passwd:passwd taxsale:1.5
 
PL/SQL procedure successfully completed
代码语言:javascript
复制
SQL> declare
  2  --定义一个pl/sql记录类型叫做 test_record_type 可以理解为定义一个类,这个类型中包括了name,passwd,salary
  3  type test_record_type is record (name mytest.name%type,passwd mytest.passwd%type,salary_gaga mytest.salary%type);
  4  --定义一个变量这个变量是test_record_type类型 好比类new一个对象
  5  sp_record test_record_type;
  6  begin
  7  select name,passwd,salary into sp_record from mytest where id=&inpt;
  8  dbms_output.put_line('name:'||sp_record.name||' salary:'||sp_record.salary_gaga);
  9  end;
 10  /
 
name:123 salary:100
代码语言:javascript
复制
SQL> declare
  2  type sp_test_table is table of mytest.name%type index by binary_integer;
  3  test_table sp_test_table;
  4  begin
  5  select name into test_table(0) from mytest where id=&inpt;
  6  dbms_output.put_line('name:'||test_table(0));
  7  end;
  8  /
 
name:gagaga
 
PL/SQL procedure successfully completed
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档