在存储过程中使用 EXECUTE IMMEDIATE 执行 DML 语句
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 needEXECUTE IMMEDIATE
.