将列添加到架构中的多个表
我需要更改模式中的多个表,对于所有以某些内容为前缀的表,例如:
ALTER TABLE "SCHEMA"."TABLE1"
ADD ( "COLUMN1" CHARACTER(4) NOT NULL DEFAULT 'DATA',
"COLUMN2" VARCHAR(16) NOT NULL DEFAULT 'MORE_DATA',
);
我需要它来迭代多个表,例如 SCHEMA.table1、SCHEMA.table2...等等。该架构中有大约 800 个表。
我想知道是否可以使用某种通配符?我正在考虑从系统表创建游标,但确信一定有一种更简单的方法。
I need to alter multiple tables in a schema, for all tables prefixed with something, for example:
ALTER TABLE "SCHEMA"."TABLE1"
ADD ( "COLUMN1" CHARACTER(4) NOT NULL DEFAULT 'DATA',
"COLUMN2" VARCHAR(16) NOT NULL DEFAULT 'MORE_DATA',
);
I need this to iterate over multiple tables, like SCHEMA.table1, SCHEMA.table2... and so on. There is around 800 tables in the schema.
I was wondering if there is some kind of wildcard that I could use? I was looking at creating a cursor from the System tables, but was sure there must be an easier way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这些情况下,如果 DBMS 没有提供简单的方法来执行此操作,我倾向于做的就是简单地编写一个脚本来为我执行此操作。类似于:
这是
bash
类型格式,您需要适应您使用的任何脚本工具。What I tend to do in those cases, if the DBMS doesn't provide an easy way to do it, is to simply write a script that will do it for me. Something like:
That's
bash
type format, you'll need to adapt for whatever scripting tool you use.感谢大家的输入,
在使用 CURSOR 和 while 循环创建了(我认为)奇特的过程来迭代表之后,我决定,因为这是一次从 select 语句为每个表构建 ALTER 查询的过程:
我将生成的查询粘贴到查询窗口中并运行它,结果大约有 1500 个表。我想有时最不优雅的解决方案就足够了:-)
Thanks for everyone's input,
After creating what was (I thought) fancy procedure using a CURSOR and while loop to iterate through the tables, I decided since it is a once off to build an ALTER query for each of the tables from a select statement thus:
I pasted the resultant queries into a query window and ran it, it turned out to be around 1500 tables. I guess sometimes the least elegant solution is good enough :-)
使用未记录的sp_MSforeachTable
。您会在 Google 上找到很多相关帮助。OP 做到了原来没有指定DB2。因此我的回答是多余的。
Use the undocumentedsp_MSforeachTable
. You'll find lots of help on Google for that.OP did not specify DB2 originally. My answer is thus redundant.
下面将向特定数据库(例如,MyDatabase)内具有特定架构(例如,MySchema)的所有表添加一列
The following will add a column to all tables with a specific schema (e.g., MySchema) within a specific database (e.g., MyDatabase)