pl/sql

--PL/SQL(procedual Language/SQL):过程化SQL语言。对SQL语言做的一个扩展性的语言。

--要比SQL增加了编程语言的特点。

 

--PL/SQL程序是由三部分组成:声明部分,执行部分,异常处理部分

 

 

-- declare

    --声明部分:变量的声明,函数的声明,存储过程的声明......

-- begin

    --执行部分:函数的调用 ,存储过程的调用,SQL语句 

-- exception

    --异常处理部分

-- end;

-- 注意:执行部分不可以为空

 

--PL/SQL对标识符定义的要求:

   --1.标识符的长度不能大于30个字符

   --2.第一个字符必须是字母

   --3.不区分大小写

   --4.不能是SQL、PL/SQL的关键字

   

--数据类型:

   --char:定长的字符串类型

   --varchar2():不定长的字符串类型

   --binary_integer:带符号的整形。

   --number(p,s):整形,浮点型

   --Date:日期类型

   --boolean:布尔类型

   

 

--declare

   --声明部分:声明部分和异常处理部分如果不需要可以不写,但是执行部分必须写。

begin

  --执行部分:打印一个字符串数据值

  dbms_output.put_line('this is my first pl/sql program!');

  dbms_output.put_line(sysdate);

end;   

 

 

--变量的声明和使用:

  --声明变量:变量名 数据类型;

  --声明变量是直接赋值:变量名 数据类型 := 数据值;

  --声明变量后给其赋值:变量名:=数据值;

 

 

declare

  i number(3);

  -- i:=200; error,才操作不属于声明操作,属于执行操作。

  j number(3):=100;

begin

   i:=200;

   dbms_output.put_line(i);

end;

 

--拼接字符串:||

declare

   num number(3):=100;

   

begin

  dbms_output.put_line('num='||num);

end;

 

--给员工编号为 num 的员工涨 n 元工资。

DECLARE

         num number(3):=100;

         n number(4):=5;

BEGIN

         update employees set salary=salary+n where employee_id=num;  --DML

         commit;  --TCL

       -- error  select * from employees; --DQL

       -- error drop table t_user; --DDL

END;

 

--注意:PLSQL中的执行部分,DML和TCL是可以直接执行的。

--DQL操作必须使用select...into才可以执行

--DDL操作必须使用动态SQL才可执行

 

--select...into语法格式:select 列1,列2 into 变量1,变量2 

 

--需求:将员工标号为100的员工last_name和salary查询出来。

--表名1.列名1%type:表示引用表名1中的列名1的数据类型。

declare

     p_name employees.last_name%type;

     p_salary employees.salary%type;                      

begin

      select last_name,salary into p_name,p_salary from employees where employee_id=100;

      dbms_output.put_line(p_name||','||p_salary);

end;

 

--注意:如果查询语句返回多条结果值,则select...into会报错。

 

--录入:变量名 数据类型 := &input;

declare

   p_id employees.employee_id%type := &input;--类型引用,该变量用来存储用户手动录入的数据值

  

   p_name employees.last_name%type;

   p_salary employees.salary%type;  

begin

  

   select last_name,salary into p_name,p_salary from employees where employee_id=p_id;

    dbms_output.put_line(p_name||','||p_salary||','||p_id);

end;

 

 

 

--'&input':表示如果用户不录入数据,则将null赋值给相应变量。

declare

   p_id employees.employee_id%type := '&input';--类型引用,该变量用来存储用户手动录入的数据值

  

   p_name employees.last_name%type;

   p_salary employees.salary%type;  

begin

  

   --select last_name,salary into p_name,p_salary from employees where employee_id=p_id;

    dbms_output.put_line(p_name||','||p_salary||','||p_id);

end;

 

--PL/SQL(procedual Language/SQL):过程化SQL语言。对SQL语言做的一个扩展性的语言。--要比SQL增加了编程语言的特点。
--PL/SQL程序是由三部分组成:声明部分,执行部分,异常处理部分

-- declare    --声明部分:变量的声明,函数的声明,存储过程的声明......-- begin    --执行部分:函数的调用 ,存储过程的调用,SQL语句 -- exception    --异常处理部分-- end;-- 注意:执行部分不可以为空
--PL/SQL对标识符定义的要求:   --1.标识符的长度不能大于30个字符   --2.第一个字符必须是字母   --3.不区分大小写   --4.不能是SQL、PL/SQL的关键字   --数据类型:   --char:定长的字符串类型   --varchar2():不定长的字符串类型   --binary_integer:带符号的整形。   --number(p,s):整形,浮点型   --Date:日期类型   --boolean:布尔类型   
--declare   --声明部分:声明部分和异常处理部分如果不需要可以不写,但是执行部分必须写。begin  --执行部分:打印一个字符串数据值  dbms_output.put_line('this is my first pl/sql program!');  dbms_output.put_line(sysdate);end;   

--变量的声明和使用:  --声明变量:变量名 数据类型;  --声明变量是直接赋值:变量名 数据类型 := 数据值;  --声明变量后给其赋值:变量名:=数据值;

declare  i number(3);  -- i:=200; error,才操作不属于声明操作,属于执行操作。  j number(3):=100;begin   i:=200;   dbms_output.put_line(i);end;
--拼接字符串:||declare   num number(3):=100;   begin  dbms_output.put_line('num='||num);end;
--给员工编号为 num 的员工涨 n 元工资。DECLARE         num number(3):=100;         n number(4):=5;BEGIN         update employees set salary=salary+n where employee_id=num;  --DML         commit;  --TCL       -- error  select * from employees; --DQL       -- error drop table t_user; --DDLEND;
--注意:PLSQL中的执行部分,DML和TCL是可以直接执行的。--DQL操作必须使用select...into才可以执行--DDL操作必须使用动态SQL才可执行
--select...into语法格式:select 列1,列2 into 变量1,变量2 
--需求:将员工标号为100的员工last_name和salary查询出来。--表名1.列名1%type:表示引用表名1中的列名1的数据类型。declare     p_name employees.last_name%type;     p_salary employees.salary%type;                      begin      select last_name,salary into p_name,p_salary from employees where employee_id=100;      dbms_output.put_line(p_name||','||p_salary);end;
--注意:如果查询语句返回多条结果值,则select...into会报错。
--录入:变量名 数据类型 := &input;declare   p_id employees.employee_id%type := &input;--类型引用,该变量用来存储用户手动录入的数据值     p_name employees.last_name%type;   p_salary employees.salary%type;  begin     select last_name,salary into p_name,p_salary from employees where employee_id=p_id;    dbms_output.put_line(p_name||','||p_salary||','||p_id);end;


--'&input':表示如果用户不录入数据,则将null赋值给相应变量。declare   p_id employees.employee_id%type := '&input';--类型引用,该变量用来存储用户手动录入的数据值     p_name employees.last_name%type;   p_salary employees.salary%type;  begin     --select last_name,salary into p_name,p_salary from employees where employee_id=p_id;    dbms_output.put_line(p_name||','||p_salary||','||p_id);end;

 

转载于:https://www.cnblogs.com/hdj1073678089/p/7460520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值