Oracle_PL/SQL的基本写法_BEGIN_END块结构及简单的事务实现
虽然之前写了不少Oracle上的SQL语句,但是没有抽出时间对Oracle进行一个系统的学习,实践固然重要,但没有一个理论上的规范学习与
虽然之前写了不少Oracle上的SQL语句,但是没有抽出时间对Oracle进行一个系统的学习,实践固然重要,但没有一个理论上的规范学习与理解,在实践中就不能举一反三,就不能写出高规范高质量的SQL语句。
-- PL/SQL 基本写法
-- 说明:声明、异常处理部分为可选,视具体程序而定
-- 博客记录点滴 转载注明出处
DECLARE -- 声明变量
A INTEGER;-- 只声明
B FLOAT := 0;-- 带赋值的声明
C FLOAT;
BEGIN -- 可执行语句开始
DBMS_OUTPUT.put_line('开始执行可执行语句块![转载注明出处]');
A := 1.5;
DBMS_OUTPUT.put_line('A=' || A);
DBMS_OUTPUT.put_line('B=' || B);
C := A / B; -- 会引发分母为0的异常,下面的两条输出语句将无法执行
DBMS_OUTPUT.put_line('C=' || C);
DBMS_OUTPUT.put_line('可执行语句块执行完毕![转载注明出处]');
EXCEPTION -- 异常处理
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('[PL/SQL 基本写法]中出现异常,错误代码:ORA'||sqlcode);
END; -- 可执行语句结束
/ -- 该符号表示执行这段PL/SQL代码
执行后的输出:
开始执行可执行语句块![转载注明出处]
A=2
B=0
[PL/SQL 基本写法]中出现异常,,错误代码:ORA-1476
我们再看一下如何通过异常处理实现数据库事务:
-- PL/SQL 事务
-- 说明:有多条修改数据的语句执行,如果其中某条出错,之前的更改也不会记入数据库
-- 博客记录点滴 转载注明出处
-- 1.先创建一个测试表
DECLARE
V_SQL_DROP_TABLE VARCHAR2(50) := 'DROP TABLE MY_TEST';
V_SQL_CREATE_TABLE VARCHAR2(100) := 'CREATE TABLE MY_TEST(NOT_NULL VARCHAR2(20) NOT NULL, ONLY_INT INTEGER)';
BEGIN
EXECUTE IMMEDIATE V_SQL_CREATE_TABLE; -- 创建测试表
EXCEPTION
-- 如果表已存在,则会引发异常
WHEN OTHERS THEN
EXECUTE IMMEDIATE V_SQL_DROP_TABLE; -- 先删除
EXECUTE IMMEDIATE V_SQL_CREATE_TABLE; -- 再创建
END;
/
--2.用我们刚创建的测试表进行测试
DECLARE
V_COUNT INTEGER; -- 表中记录的行数
V_INT_VAL MY_TEST.ONLY_INT%TYPE; -- 使用%TYPE关键字参照某表某字段类型声明变量
BEGIN
V_INT_VAL := 123456;
-- 插入一条正确的数据
INSERT INTO MY_TEST VALUES ('TEST_SUCCESS', V_INT_VAL);
-- 查询条数为1条,我们发现插入成功了
SELECT COUNT(*) INTO V_COUNT FROM MY_TEST;
DBMS_OUTPUT.put_line('MY_TEST表中有' || V_COUNT || '条记录');
-- 插入一条错误的数据,因为第二个字段为int型,插入字符数据肯定会出错
INSERT INTO MY_TEST VALUES ('TEST_FAIL', 'ABC');
-- 最后提交更改
COMMIT;
EXCEPTION
-- 异常处理
WHEN OTHERS THEN
ROLLBACK; -- 异常时回滚,这样第一次插入的正确数据也不会保存到数据库
DBMS_OUTPUT.put_line('[PL/SQL 事务]中出现异常,错误代码:ORA' || sqlcode);
-- 我们验证一下表里的数据为0条
SELECT COUNT(*) INTO V_COUNT FROM MY_TEST;
DBMS_OUTPUT.put_line('回滚后,MY_TEST表中有' || V_COUNT || '条记录');
END; -- 可执行语句结束
/ -- 该符号表示执行这段PL/SQL代码
执行后的输出:
MY_TEST表中有1条记录
[PL/SQL 事务]中出现异常,错误代码:ORA-1722
回滚后,MY_TEST表中有0条记录
相关阅读:
Oracle 10g 安装后重启系统,用PLSQL连接报没有监听
ORA-03114 PLSQL过程编译断开连接错误
PLSQL 连接 Oracle简单配置
PLSQL批量Forall操作性能提升详解
使用Oracle SQLDeveloper连接数据库并创建用户
Oracle自带的PL/SQL Developer导入导出数据
在64位Win7系统下安装Oracle 11g和Oracle SQL Developer客户端

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.
