Domanda

I am trying to insert a row from table1 in database A into table2 in database B all in firebird and doesn't look like standard way is working.

insert * from A.table1 into B.table2 where <condition>

thanks

È stato utile?

Soluzione

Fully qualified names

The term Fully Qualified Name seems to be a SQL Server specific term for cross database (or catalog) references. In Firebird, databases are independent and isolated: they don't know about the existence of other databases. It is therefor not possible to refer to tables in other databases directly.

With the EXECUTE STATEMENT extension ON EXTERNAL it is possible to access another database from within a block of PSQL (a stored procedure or EXECUTE BLOCK), but it is not as straight forward as a INSERT INTO ... SELECT.

For example (note: I didn't test it, so it might have some syntax errors):

EXECUTE BLOCK
   DECLARE varColumn1 VARCHAR(10);
   DECLARE varColumn2 VARCHAR(10);
BEGIN
   FOR EXECUTE 'SELECT column1, column2 FROM tableA'
       ON EXTERNAL 'localhost:/path/to/dbA' 
       AS USER 'userDBa' PASSWORD 'pwdUserDBa'
       INTO :varColumn1, :varColumn2
   BEGIN
       INSERT INTO tableB(column1, column2) VALUES (:varColumn1, :varColumn2);
   END
END

Original answer

The syntax you quote in your question is not 'standard'. The SQL:2011 standard defines an INSERT as:

<insert statement> ::=
INSERT INTO <insertion target> <insert columns and source>

<insertion target> ::=
<table name>

<insert columns and source> ::=
<from subquery>
| <from constructor>
| <from default>

<from subquery> ::=
[ <left paren> <insert column list> <right paren> ]
[ <override clause> ]
<query expression>

<from constructor> ::=
[ <left paren> <insert column list> <right paren> ]
[ <override clause> ]
<contextually typed table value constructor>

<override clause> ::=
OVERRIDING USER VALUE
| OVERRIDING SYSTEM VALUE

<from default> ::=
DEFAULT VALUES

<insert column list> ::=
<column name list>

In other words, an INSERT from another table would be:

INSERT INTO tableB (column1, column2, ...) SELECT column1, column2, ... FROM tableA

Technically you could leave the explicit column definitions off, but it is usually better to be explicit.

This is also the syntax supported by Firebird (although Firebird does not support all the options listed in the SQL:2011 standard), see the Interbase 6.0 Language Reference (+ Firebird 2.5 Language Reference Update), both available from http://www.firebirdsql.org/en/reference-manuals/ :

INSERT [TRANSACTION transaction] INTO <object> [(col [, col …])]
{VALUES (<val> [, <val> …]) | <select_expr>};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top