sqlh2liquibaseh2db

INSERT INTO table SELECT not working with constant values in H2 DB


Following is the H2DB query

 INSERT INTO userpermission (userid, permissionid, createddt, createdby, updateddt, updatedby)
            SELECT u.userid, p.permissionid, now(), ("1"), now(), ("1") FROM user u, permission p
            WHERE u.email = "csr_admin@example.com";

What I am trying to achieve is to insert a record into userpermission for user with given email address in user table, for each permission in permission table.

Following is the error

 Reason: liquibase.exception.DatabaseException: Column "1" not found; SQL statement:
                                    INSERT INTO userpermission (userid, permissionid, createddt, createdby, updateddt, updatedby)
                                                SELECT u.userid, p.permissionid, now(), ("1"), now(), ("1") FROM user u, permission p
                                                WHERE u.email = "csr_admin@example.com" [42122-193] [Failed SQL: INSERT INTO userpermission (userid, permissionid, createddt, createdby, updateddt, updatedby)
                                                SELECT u.userid, p.permissionid, now(), ("1"), now(), ("1") FROM user u, permission p
                                                WHERE u.email = "csr_admin@example.com"]

The same query is working in MySQL.


Solution

  • As mentioned in the comments,

    1. Use single quotes for sting constants.
    2. Do not use braces with constants in select clause.
    3. Check the join condition. Without matching 2 tables based on common column, you are doing a Cartesian product