PostgreSQL - 如何执行“独立式”代码?
我习惯了 SQL Server 中的查询编辑器,可以在其中自由编写和查询。执行 T-SQL。如何让以下代码在 PostgreSQL 中执行,而无需从中创建函数?
DECLARE
l integer = 1;
BEGIN
CREATE TABLE product_i (id bigint, key integer, value integer);
CREATE INDEX ix_product_i_size ON product_i(value) WHERE key = 1;
CREATE INDEX ix_product_i_mass ON product_i(value) WHERE key = 2;
LOOP
BEGIN;
INSERT
INTO product_i (id, key, value)
SELECT id, 1, CEILING(10 + RANDOM() * 90)
FROM generate_series(l, 1000) id;
INSERT
INTO product_i (id, key, value)
SELECT id, 2, CEILING(10 + RANDOM() * 90)
FROM generate_series(l, 1000) id;
COMMIT;
l := l + 1000;
EXIT WHEN l > 5000;
END LOOP;
END
I am used to the Query Editor in SQL Server where one can freely write & execute T-SQL. How do I get the following code to execute in PostgreSQL without having to create a function out of it?
DECLARE
l integer = 1;
BEGIN
CREATE TABLE product_i (id bigint, key integer, value integer);
CREATE INDEX ix_product_i_size ON product_i(value) WHERE key = 1;
CREATE INDEX ix_product_i_mass ON product_i(value) WHERE key = 2;
LOOP
BEGIN;
INSERT
INTO product_i (id, key, value)
SELECT id, 1, CEILING(10 + RANDOM() * 90)
FROM generate_series(l, 1000) id;
INSERT
INTO product_i (id, key, value)
SELECT id, 2, CEILING(10 + RANDOM() * 90)
FROM generate_series(l, 1000) id;
COMMIT;
l := l + 1000;
EXIT WHEN l > 5000;
END LOOP;
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您至少使用 PostgreSQL 9.0,则可以将其包含在 DO 块中:
http://www.postgresql.org/docs/9.0/static/sql-do.html
If you are using at least PostgreSQL 9.0, you can include this in a DO block:
http://www.postgresql.org/docs/9.0/static/sql-do.html