开发者

PostgreSQL - how to execute "free-standing" code?

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜