如何使用动态 SQL 声明从表名派生的列名?
如果我想做这样的事情,
CREATE PROCEDURE A(tab IN VARCHAR2) IS
tab.col_name <column> --static declaration (column name always remains the same)
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO ' || tab(col_name) || 'VALUES(123)';
END A;
怎样才能我在上述情况下使用动态 SQL?
Building on Tony's answer on this question:
If I want to do something like this,
CREATE PROCEDURE A(tab IN VARCHAR2) IS
tab.col_name <column> --static declaration (column name always remains the same)
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO ' || tab(col_name) || 'VALUES(123)';
END A;
How can I use Dynamic SQL in the above case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此示例传递一个表名和一个列名:
您需要意识到 EXECUTE IMMEDIATE 之后的所有内容都必须是包含一些有效 SQL 的字符串。验证这一点的一个好方法是将其设置在变量中并将其打印到屏幕上:
然后,这应该在 SQL Plus 中显示如下内容:
(前提是服务器输出已打开)。
编辑:由于您希望列名是始终具有相同值的局部变量,因此可以这样做:
This example passes in a table name and a column name:
You need to realise that everything after EXECUTE IMMEDIATE must be a string that contains some valid SQL. A good way to verify this is to set it up in a variable and print it to the screen:
This should then display something like the following in SQL Plus:
(provided server output is turned on).
EDIT: Since you want the column name to be a local variable that always has the same value, this could be done as: