跟我学plsql - 定义变量 (一)

 Class 1:

This I will intruduce the PLSQL:

  First, list the useful of plsql:
  1: variable,constant, and data types
  2: Control structures such as conditional statement and loops
  3: Reusable program units that are written once and executed many times
 
Plsql structure:
 declare (optional)
  variables, cursors,user-defined,exception
 
 begin (mandatory)
  sql statement
  plsql statement
 
 exception (optional)
  actions to perform
  when errors occur
 
 end; (mandatory)
 
From above,we can know the declare and exception are optional, the other is mandatory.
Notice , there is a ";" end by "end"
----------
Type:
 Anonymous  (there is no produce name)(can only use on time)
 procedure  (this have its name )
 Function  (this have its name ,and have it return item)
 
ok, let us start with case of "Anonymous"
 
  Because this is anonyous, when it finished, end with '/', it will completed successfully.
 
  if you want to see the output, you should set below setting:
 
 
SQL> declare
  2    v_fname varchar2(20);
  3  begin
  4    select ename into v_fname from emp where empno=7844;
  5  end;
  6  /

PL/SQL 过程已成功完成。

SQL> set serveroutput on;
(this command function is to enable the dbms_output )

SQL> l  (using charactor : l to list the latest command quickly)
  1  declare
  2    v_fname varchar2(20);
  3  begin
  4    select ename into v_fname from emp where empno=7844;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> declare
  2    v_fname varchar2(20);
  3  begin
  4    select ename into v_fname from emp where empno=7844;
  5  dbms_output.put_line('The result is ' || v_fname);
  6  end;
  7  /
The result is TURNER

PL/SQL 过程已成功完成。


Class 2:

Step1:
  After completing this lesson, you should be able to do the following:
 
  1: Recognize valid and invalid identifiers
  2: List the uses of variables
  3: Declare and initialize variables
  4: List and describe various data types
  5: Identify the benefits of using the %TYPE attrubute
  6: Declare,use,and print bind variables
 
Step2:
  variables can be used for:
  1: Temporary storage of data
  2: Manipulation of stored values
  3: Reusability
 
  example:
  select
   first_anme
   department_id
  into
   v_fname,
   v_deptno
  from
   ...
 
 
  A variable name:
 
  1: Must start with a letter
  2: Can include letters or numbers
  3: Can include special characters (such as $,_,and #)
  4: Must contain no more than 30 characters
  5: Must not include reserved words
 
 
  Declaring an initializing PL/SQL:
 
  identifier [CONSTANT] datatype [ not null ]
       [ := | DEFAULT expr ];
       
  example:
 
  DECLARE
   V_hiredate   DATE
   V_deptno     NUMBER NOT NULL :=10;
   V_location    varchar2(13)    := 'Atlant';
   c_comm       CONSTANT NUMBER :=1400;
   
  NOTES:
  c_comm is keep the values of "1400".
 
Step3:

  Let us start with two example:
 
  SQL> declare
  2    v_myname varchar2(20);
  3  begin
  4    dbms_output.put_line('My name is :'|| v_myname);
  5  v_myname :='john';
  6  dbms_output.put_line('My name is :'|| v_myname);
  7  end;
  8  /
My name is :
My name is :john

PL/SQL 过程已成功完成。

SQL> declare
  2    v_myname varchar2(20) :='John';
  3  begin
  4    v_myname := 'Steven';
  5  dbms_output.put_line('My name is :'|| v_myname);
  6  end;
  7  /
My name is :Steven

PL/SQL 过程已成功完成。

Step4:
 Delimiters in String Literals:
 
 let us follow with one workshop:
 
 example:
 
 SQL> l
  1  declare
  2    v_event varchar2(15);
  3  begin
  4    v_event := q'!Father's day!';
  5  dbms_output.put_line('3rd Sunday in June is :'|| v_event );
  6    v_event := q'[Mother's day]';
  7  dbms_output.put_line('2nd Sunday in May is :'|| v_event )
  8* end;
SQL> /
end;
*
第 8 行出现错误:
ORA-06550: 第 8 行, 第 1 列:
PLS-00103: 出现符号 "END"在需要下列之一时:
:= . ( % ;
符号 ";" 被替换为 "END" 后继续。

SQL> 7
  7* dbms_output.put_line('2nd Sunday in May is :'|| v_event )
SQL> c/)/);;
  7* dbms_output.put_line('2nd Sunday in May is :'|| v_event );
SQL> l
  1  declare
  2    v_event varchar2(15);
  3  begin
  4    v_event := q'!Father's day!';
  5  dbms_output.put_line('3rd Sunday in June is :'|| v_event );
  6    v_event := q'[Mother's day]';
  7  dbms_output.put_line('2nd Sunday in May is :'|| v_event );
  8* end;
SQL> /
3rd Sunday in June is :Father's day
2nd Sunday in May is :Mother's day

PL/SQL 过程已成功完成。

SQL>

From above case, we learn:
  1: How to list one line of procedure.
  2: How to clarify the one charactor using "c/".
  3: How to voide the charactor spring of "'" of output. (using q'[]' or q'!!' ).

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
银行管理系统是一个非常复杂的系统,由于篇幅有限,我只能提供一个简单的实现,基于Oracle-PLSQL语言。我们将实现以下功能: 1. 用户账户管理:包括开户、销户、查询、存款、取款等操作。 2. 账户转账:允许用户在不同账户之间进行转账操作。 3. 查询交易记录:用户可以查询历史交易记录。 下面是实现步骤: 1. 创建表格: 我们需要创建以下表格: - 用户信息表(包括账户信息) - 交易记录表 2. 编写存储过程: 我们需要编写一系列存储过程来实现用户账户管理、账户转账和查询交易记录。以下是一些基本存储过程的示例: - 开户: ``` CREATE PROCEDURE create_account( p_name VARCHAR2, p_balance NUMBER, p_branch VARCHAR2 ) AS BEGIN INSERT INTO account_info(name, balance, branch) VALUES(p_name, p_balance, p_branch); END; ``` - 销户: ``` CREATE PROCEDURE delete_account( p_account_no VARCHAR2 ) AS BEGIN DELETE FROM account_info WHERE account_no = p_account_no; DELETE FROM transaction_history WHERE account_no = p_account_no; END; ``` - 查询余额: ``` CREATE PROCEDURE check_balance( p_account_no VARCHAR2, p_balance OUT NUMBER ) AS BEGIN SELECT balance INTO p_balance FROM account_info WHERE account_no = p_account_no; END; ``` - 存款: ``` CREATE PROCEDURE deposit( p_account_no VARCHAR2, p_amount NUMBER ) AS BEGIN UPDATE account_info SET balance = balance + p_amount WHERE account_no = p_account_no; INSERT INTO transaction_history(account_no, transaction_type, amount) VALUES(p_account_no, 'DEPOSIT', p_amount); END; ``` - 取款: ``` CREATE PROCEDURE withdraw( p_account_no VARCHAR2, p_amount NUMBER ) AS BEGIN UPDATE account_info SET balance = balance - p_amount WHERE account_no = p_account_no; INSERT INTO transaction_history(account_no, transaction_type, amount) VALUES(p_account_no, 'WITHDRAW', p_amount); END; ``` - 转账: ``` CREATE PROCEDURE transfer( p_sender_account_no VARCHAR2, p_receiver_account_no VARCHAR2, p_amount NUMBER ) AS BEGIN UPDATE account_info SET balance = balance - p_amount WHERE account_no = p_sender_account_no; UPDATE account_info SET balance = balance + p_amount WHERE account_no = p_receiver_account_no; INSERT INTO transaction_history(account_no, transaction_type, amount) VALUES(p_sender_account_no, 'TRANSFER', -p_amount); INSERT INTO transaction_history(account_no, transaction_type, amount) VALUES(p_receiver_account_no, 'TRANSFER', p_amount); END; ``` - 查询交易记录: ``` CREATE PROCEDURE check_transaction_history( p_account_no VARCHAR2 ) AS BEGIN SELECT * FROM transaction_history WHERE account_no = p_account_no; END; ``` 3. 测试: 我们可以使用以下代码来测试我们的系统: ``` DECLARE v_balance NUMBER; BEGIN -- 创建账户 create_account('Alice', 1000, 'Beijing'); create_account('Bob', 2000, 'Shanghai'); -- 查询余额 check_balance('1001', v_balance); DBMS_OUTPUT.PUT_LINE('Alice balance: ' || v_balance); check_balance('1002', v_balance); DBMS_OUTPUT.PUT_LINE('Bob balance: ' || v_balance); -- 存款 deposit('1001', 500); -- 取款 withdraw('1002', 1000); -- 转账 transfer('1001', '1002', 300); -- 查询交易记录 check_transaction_history('1001'); check_transaction_history('1002'); -- 销户 delete_account('1001'); delete_account('1002'); END; ``` 这只是一个简单的实现,真正的银行管理系统要比这个复杂得多。但这个例子应该可以帮助你了解如何使用Oracle-PLSQL来实现银行管理系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shenghuiping2001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值