在存储过程中使用 EXECUTE IMMEDIATE 执行 DML 语句

发布于 2024-11-25 22:33:39 字数 397 浏览 4 评论 0原文

我是 PL SQL 过程的新手,我们在存储过程的执行部分中有这行代码。

我在这里有一个疑问,请告诉我在这里使用EXECUTE IMMEDIATE作为DML语句有什么用?在什么情况下我们应该使用EXECUTE IMMEDIATE

v_update_query2 := 'INSERT INTO '||p_country||'.DETAILS ( ID, STATUS, DEST_SYSTEM, OUT_TIME ) VALUES ('''
    ||v_txn_id ||''','||'''T081'''||','||'''CLEARING'''||', SYSDATE)';



EXECUTE IMMEDIATE v_update_query1 ;

I am new to PL SQL Procedures , we have this line of code inside the Execution section of a Stored Procedure .

I am having a query here , please tell me whats the use of using EXECUTE IMMEDIATE for a DML Statement Here ? and in what cases we should use EXECUTE IMMEDIATE?

v_update_query2 := 'INSERT INTO '||p_country||'.DETAILS ( ID, STATUS, DEST_SYSTEM, OUT_TIME ) VALUES ('''
    ||v_txn_id ||''','||'''T081'''||','||'''CLEARING'''||', SYSDATE)';



EXECUTE IMMEDIATE v_update_query1 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷荒 2024-12-02 22:33:39

EXECUTE IMMEDIATE 是对架构对象(例如表名、列名等)进行变量引用的唯一方法。

它允许您构建任何 字符串,然后执行该字符串: SQL 语句。
如果没有它,过程变量只能用于 sql 中的值,例如 select * from table where column = my_variable

在您的示例中,表名称由 < code>p_country 变量 - 这是一个架构元素,因此您需要 EXECUTE IMMEDIATE

EXECUTE IMMEDIATE is the only way to have variable references to schema objects - eg table names, column names etc.

It allows you to build up any string and then execute that string as an SQL statement.
Without it, procedure variables can only be used for values in sql, eg select * from table where column = my_variable

In your example, the table name is being provided by the p_country variable - that's a schema element, so you need EXECUTE IMMEDIATE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文