简体   繁体   English

使用count()从tableA插入tableB到tableB

[英]INSERT INTO from tableA to tableB with count()

Inserting rows from one table to another table, I try to use a count(*) to ensure that the line_no column in table OBJ_LINES is set to 1,2,3,4.. and so on for every line added. 将行从一个表插入到另一个表,我尝试使用count(*)来确保表OBJ_LINES中的line_no列设置为1,2,3,4 ..,依此类推。

INSERT INTO OBJ_LINES(
id,
line_no)
SELECT (1,
(select count(*)+1 FROM OBJ_LINES WHERE id = 1)
FROM TEMPLATE_LINES tmp
WHERE tmp.id = 37;

(syntax Sybase Database) If the TEMPLATE_LINES table holds more than one row, I get a duplicate error as the count() seems to be evaluted only once, and not for every row found in the TEMPLATE_LINES table. (语法Sybase数据库)如果TEMPLATE_LINES表包含多个行,则会收到重复错误,因为count()似乎仅被评估一次,而不是针对TEMPLATE_LINES表中发现的每一行。

How may I write the sql to set 'dynamic' line_no depending on the current number of rows for a given id? 如何根据给定ID的当前行数编写sql以设置“动态” line_no?

INSERT INTO OBJ_LINES(
id,
line_no)
SELECT (1,
number(*)
FROM TEMPLATE_LINES tmp
WHERE tmp.id = 37;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM