从 SQL 存储过程创建/更改
我想从过程中调用 create table/alter table 命令。是否可以?
我的要求是更改所有表中列的数据类型。因此,我只是从 user_tab_cols
获取列名称。现在我想创建一个需要 create 语句的临时表..但我无法在过程中使用它。
有人可以帮我吗?
I want to call create table/ alter table command from a procedure. Is it possible?
My requirement is to change the datatype of a column in all tables. So, I am just getting the column name from user_tab_cols
. Now I want to create a temp table which requires create statement .. but i am unable to use that within a proc.
Can anyone please help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我根据对
USER_TAB_COLUMNS
的引用推测这是 Oracle。ALTER
和CREATE
语句是DDL,我们不能直接在PL/SQL中执行。但是,有几种方法可以解决此限制:EXECUTE IMMEDIATE
和DBMS_UTILITY.EXEC_DDL()
。我将在下面的示例中使用EXECUTE IMMEDIATE
。请注意,通常的限制适用:新数据类型必须与现有数据类型兼容(除非列为空),并且对于某些特定数据类型(如 CLOB),事情会更加棘手。
编辑
我还没有讨论 CREATE TABLE 语句。原理是一样的,只是打字时间长一些。此外,我并不完全清楚它如何适用于您之前更改这些列的数据类型的要求。
I presume from the reference to
USER_TAB_COLUMNS
that this is Oracle.ALTER
andCREATE
statements are DDL, which we cannot execute directly in PL/SQL. However, there are a couple of ways around this restriction:EXECUTE IMMEDIATE
andDBMS_UTILITY.EXEC_DDL()
. I will useEXECUTE IMMEDIATE
in the following example.Note that the usual restrictions apply: the new datatype has to be compatible with the existing datatype (unless the column is empty), and things are trickier with some specilaized datatypes like CLOBs.
edit
I haven't addressed the CREATE TABLE statement. The principle is the same, it is just longer to type out. Besides, I am not entirely clear how it applies to your prior requirement to change the datatype of those columns.
您可以将查询生成为字符串并使用“exec”关键字执行它。
you can generate the query as string and execute it with 'exec' keyword.