Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如果pgsql表为空,如何在该表中插入多行?_Database_Postgresql_Blackboard - Fatal编程技术网

Database 如果pgsql表为空,如何在该表中插入多行?

Database 如果pgsql表为空,如何在该表中插入多行?,database,postgresql,blackboard,Database,Postgresql,Blackboard,我正在为黑板开发一个构建块,遇到了一个与数据库相关的问题 我试图在pgsql表中插入四行,但前提是该表为空。查询作为模式更新后运行,因此每当我重新安装构建块时都会运行。至关重要的是,我不能简单地放弃现有的价值观和/或替换它们,否则这将是一个简单而有效的解决方案 下面是我现有的查询,它完成了任务,但只针对一行。正如我提到的,我正在尝试插入四行。我不能简单地多次运行insert,因为在第一次运行之后,表将不再是空的 任何帮助都将受到感谢 BEGIN; INSERT INTO my_table

我正在为黑板开发一个构建块,遇到了一个与数据库相关的问题

我试图在pgsql表中插入四行,但前提是该表为空。查询作为模式更新后运行,因此每当我重新安装构建块时都会运行。至关重要的是,我不能简单地放弃现有的价值观和/或替换它们,否则这将是一个简单而有效的解决方案

下面是我现有的查询,它完成了任务,但只针对一行。正如我提到的,我正在尝试插入四行。我不能简单地多次运行insert,因为在第一次运行之后,表将不再是空的

任何帮助都将受到感谢

BEGIN;
    INSERT INTO my_table_name 
    SELECT
        nextval('my_table_name_SEQ'),
        'Some website URL', 
        'Some image URL',
        'Some website name',
        'Y',
        'Y'
    WHERE 
        NOT EXISTS (
            SELECT * FROM my_table_name
        );
    COMMIT;
END;

最好是对行进行计数,因为它可以获得输入行的数量

这应该起作用:

BEGIN;
    INSERT INTO my_table_name 
    SELECT
        nextval('my_table_name_SEQ'),
        'Some website URL', 
        'Some image URL',
        'Some website name',
        'Y',
        'Y'
    WHERE 
        (SELECT COUNT(*) FROM my_table_name)>0
    COMMIT;
END;

插入不会覆盖,所以我不理解你问题的这一部分。 下面是插入多行的两种方法;第二个示例是一条sql语句:

创建表test col1 int, col2 varchar10 ;

insert into test select 1, 'A' ;
insert into test select 2, 'B' ;

insert into test (col1, col2)
values (3, 'C'),
       (4, 'D'),
       (5, 'E') ;


select * from test ;

1   "A"
2   "B"
3   "C"
4   "D"
5   "E"

我设法解决了这个问题。 在帖子中,没有名字的@a_horse_建议使用UNIONALL来解决类似的问题

还要感谢@Dan建议使用COUNT,而不是EXISTS

我的最后一个问题是:

BEGIN;

INSERT INTO my_table (pk1, coll1, coll2, coll3, coll4, coll5)
    SELECT x.pk1, x.coll1, x.coll2, x.coll3, x.coll4, x.coll5
        FROM (
            SELECT 
                nextval('my_table_SEQ') as pk1,
                'Some website URL' as coll1, 
                'Some image URL' as coll2,
                'Some website name' as coll3,
                'Y' as coll4,
                'Y' as coll5
            UNION
            SELECT
                nextval('my_table_SEQ'),
                'Some other website URL', 
                'Some other image URL',
                'Some other website name',
                'Y',
                'N'
            UNION
            SELECT
                nextval('my_table_SEQ'),
                'Some other other website URL', 
                'Some other other image URL',
                'Some other other website name',
                'Y',
                'N'
            UNION
            SELECT
                nextval('my_table_SEQ'),
                'Some other other other website URL', 
                'Some other other other image URL',
                'Some other other other website name',
                'Y',
                'Y'
        ) as x
    WHERE
        (SELECT COUNT(*) FROM my_table) <= 0;

    COMMIT;
END;