Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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
Firebird sql将插入另一个表中只有一个不同字段的典型记录_Sql_Firebird_Sql Insert - Fatal编程技术网

Firebird sql将插入另一个表中只有一个不同字段的典型记录

Firebird sql将插入另一个表中只有一个不同字段的典型记录,sql,firebird,sql-insert,Sql,Firebird,Sql Insert,我在Firebird 2.5上工作 我有两个表,它们的所有列都是相似的,除了一个有一个主键和一个非空外键字段(a)作为主表外 我知道我可以像这样使用sql来插入两个表中的所有值 insert into table1 select * from table2 where somthing = 'foo' 但是字段(A)如何在同一sql语句中手动插入该值?因为这是唯一需要手动输入的字段 谢谢您可以明确指定源字段和目标字段(您应该这样做;除非您有特定的理由,否则不要使用select*): 我之所以

我在Firebird 2.5上工作 我有两个表,它们的所有列都是相似的,除了一个有一个主键和一个非空外键字段(a)作为主表外

我知道我可以像这样使用sql来插入两个表中的所有值

 insert into table1 select * from table2 where somthing = 'foo'
但是字段(A)如何在同一sql语句中手动插入该值?因为这是唯一需要手动输入的字段


谢谢

您可以明确指定源字段和目标字段(您应该这样做;除非您有特定的理由,否则不要使用
select*
):


我之所以看到这篇文章,是因为我正在寻找一个解决方案来做同样的事情,但没有硬编码字段名,因为字段可能会被添加/删除,并且不想记住更新复制记录过程

在谷歌搜索了一段时间后,我想出了这个解决方案:

   select cast(list(trim(RDB$FIELD_NAME)) as varchar(10000))
      from RDB$RELATION_FIELDS 
      where RDB$RELATION_NAME = 'YOUR_TABLE' 
         and RDB$FIELD_NAME not in ('ID') -- include other fields to NOT copy
   into :FIELD_NAMES;

   NEW_ID = next value for YOUR_TABLE_ID_GENERATOR;

   execute statement '
      insert into YOUR_TABLE (ID,' || FIELD_NAMES || ')
      select ' || cast(:NEW_ID as varchar(20)) || ',' ||
         FIELD_NAMES || '
         from YOUR_TABLE
         where ID = ' || cast(:ID_OF_RECORD_TO_COPY as varchar(20));
希望这能为遇到此问题的其他人节省一些时间

   select cast(list(trim(RDB$FIELD_NAME)) as varchar(10000))
      from RDB$RELATION_FIELDS 
      where RDB$RELATION_NAME = 'YOUR_TABLE' 
         and RDB$FIELD_NAME not in ('ID') -- include other fields to NOT copy
   into :FIELD_NAMES;

   NEW_ID = next value for YOUR_TABLE_ID_GENERATOR;

   execute statement '
      insert into YOUR_TABLE (ID,' || FIELD_NAMES || ')
      select ' || cast(:NEW_ID as varchar(20)) || ',' ||
         FIELD_NAMES || '
         from YOUR_TABLE
         where ID = ' || cast(:ID_OF_RECORD_TO_COPY as varchar(20));