用于截断 IBM DB2 中的表的 SQL 查询

人气:369 发布:2022-10-16 标签: syntax truncate db2

问题描述

谁能给我截断 IBM DB2 中表的语法.

Can any one give me the syntax to truncate a table in IBM DB2.

我正在运行以下命令:truncate table tableName immediate;

错误是 DB2

SQLCODE=-104, SQLSTATE=42601, SQLERRMC=table;truncate ;JOIN , DRIVER=3.50.152消息:在truncate"之后发现了一个意外的标记table".预期的标记可能包括:JOIN".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.50.152

SQLCODE=-104, SQLSTATE=42601, SQLERRMC=table;truncate ;JOIN , DRIVER=3.50.152 Message: An unexpected token "table" was found following "truncate ". Expected tokens may include: "JOIN ".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.50.152

语法与 IBM 参考文档中指定的语法相匹配:http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sql_truncate.htm

The syntax matches the one specified in the reference docs of IBM : http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sql_truncate.htm

推荐答案

有一篇很棒的文章关于 截断,这里是 DB2 的要点

There is a great article about truncating, here is the Gist of DB2 stuff

几乎遵循标准.(从 9.7 版开始)DB2 要求在普通的 TRUNCATE TABLE 语句中添加 IMMEDIATE 关键字,例如:

Almost follows the standard.(since version 9.7) DB2 requires that the IMMEDIATE keyword be added the the ordinary TRUNCATE TABLE statement, e.g.:

TRUNCATE TABLE someschema.sometable IMMEDIATE 

TRUNCATE TABLE 必须是事务中的第一条语句.以 TRUNCATE TABLE 开头的事务可能包含其他语句,但如果事务回滚,则不会撤消 TRUNCATE TABLE 操作.DB2s TRUNCATE TABLE 操作有许多可选参数,有关更多信息,请参阅文档;特别是,REUSE STORAGE 参数对于临时 DBA 任务可能很重要.在 DB2 版本中 <9.7,您可能会滥用 IMPORT 声明.不幸的是,您需要知道该命令是从哪个操作系统执行的:

TRUNCATE TABLE must be the first statement in a transaction. A transaction starting with TRUNCATE TABLE may include other statements, but if the transaction is rolled back, the TRUNCATE TABLE operation is not undone. DB2s TRUNCATE TABLE operation has a number of optional arguments, see the documentation for more on this; especially, the REUSE STORAGE argument may be important for ad-hoc DBA tasks. In DB2 versions < 9.7, you may abuse the IMPORT statement. Unfortunately, you need to know which operating system the command is executed from for this to work:

在类 Unix 系统上:IMPORT FROM/dev/null OF DEL REPLACE INTO 表名在 Windows 上:从 NUL OF DEL REPLACE 导入表名IMPORT 不能在所有情况下都被滥用.例如,当使用动态 SQL(来自 Java/.NET/PHP/...——不使用 db2 命令行处理器)时,您需要将 IMPORT 命令包装在对 ADMIN_CMD 的调用中,例如:

On unix-like systems: IMPORT FROM /dev/null OF DEL REPLACE INTO tablename On Windows: IMPORT FROM NUL OF DEL REPLACE INTO tablename IMPORT cannot be abused in all contexts. E.g., when working with dynamic SQL (from Java/.NET/PHP/...—not using the db2 command line processor), you need to wrap the IMPORT command in a call to ADMIN_CMD, e.g.:

CALL ADMIN_CMD('IMPORT FROM /dev/null OF DEL REPLACE INTO tablename')

在涉及其他操作的事务中似乎允许使用 IMPORT,但它意味着立即执行 COMMIT 操作.

IMPORT seems to be allowed in a transaction involving other operations, however it implies an immediate COMMIT operation.

ALTER TABLE 命令也可能被滥用来快速清空表,但它需要更多权限,并且可能会导致前滚恢复出现问题.

The ALTER TABLE command may also be abused to quickly empty a table, but it requires more privileges, and may cause trouble with rollforward recovery.

这是从网站上截取的:http://troels.arvin.dk/db/rdbms/#bulk-truncate_table-db2

435