如何运行多个 SQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6212663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:49:56  来源:igfitidea点击:

How to run multiple SQL queries?

sqlplsql

提问by Vikram Singh

I have a create table query, an update query ,and then drop table query. I need to run these three queries in one shot. What is the best way to do this?

我有一个创建表查询,一个更新查询,然后删除表查询。我需要一次性运行这三个查询。做这个的最好方式是什么?

Example
1st Query: Create table A.
2nd Query: Update value in table A
3rd Query: Drop table A.

Instead of running these three queries one by one, I want to run them in on go either by PLSQL or some other way. Please help.

我不想一个一个地运行这三个查询,而是想通过 PLSQL 或其他方式运行它们。请帮忙。

回答by DaMainBoss

Put the 3 queries following each other separated by a semi-colon:

将 3 个查询用分号隔开:

SELECT * 
FROM table_to_be_selected; 
DROP TABLE the table_to_be_dropped; 
TRUNCATE TABLE table_to_be_truncated;

You can concatenate these queries in a string and execute that string.

您可以将这些查询连接到一个字符串中并执行该字符串。

Another way is this solution.

另一种方法是这种解决方案

回答by Datajam

Simply put three queries one after the other in a .sql file, with semi-colons after each statement, then execute it as a script (either on a SQL*Plus prompt using @scriptname.sqlor in TOAD/SQL Developer [or equivalent] using its script execution function).

简单地将三个查询一个接一个地放在一个 .sql 文件中,每条语句后用分号,然后将其作为脚本执行(在 SQL*Plus 提示上使用@scriptname.sql或在 TOAD/SQL Developer [或等效] 中使用其脚本)执行函数)。

回答by whirlwin

Why not create a PROCEDURE?

为什么不创建一个PROCEDURE

E.g.

例如

CREATE OR REPLACE PROCEDURE foo
IS
BEGIN
  -- The create sentence goes here. For example:
  -- EXECUTE IMMEDIATE
  -- 'CREATE TABLE bar (...)';

  -- The update sentence goes here
  -- EXECUTE IMMEDIATE
  -- 'UPDATE bar SET ...';

  -- The drop/delete sentence goes here.
  -- EXECUTE IMMEDIATE
  -- 'DROP TABLE bar;'
END;

You can test it with:

您可以使用以下方法对其进行测试:

SET serveroutput ON;
exec foo;

The PROCEDUREis stored on the database, so you can use it later on.

PROCEDURE被存储在数据库中,所以你以后可以使用它。

Hope this helps.

希望这可以帮助。