Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
Sql 从cte选择更新_Sql_Firebird_Common Table Expression - Fatal编程技术网

Sql 从cte选择更新

Sql 从cte选择更新,sql,firebird,common-table-expression,Sql,Firebird,Common Table Expression,我需要根据以前的CTE语句更新表tb0。该代码类似于下面的伪代码: with tb1 as(select tb_index from tb0 where tb_index=10), tb2 as (select tb_index,count(orders))total from orders inner join tb1 on tb1.tb_index=orders.tb_index) update tb0 set tb0.status='a' from tb2 inner join tb0

我需要根据以前的CTE语句更新表tb0。该代码类似于下面的伪代码:

with tb1 as(select tb_index from tb0 where tb_index=10),

tb2 as (select tb_index,count(orders))total from orders inner join tb1 on tb1.tb_index=orders.tb_index)

update tb0 set tb0.status='a'
from tb2
inner join tb0.tb_index=tb2.tb_index
where
tb2.total>30
问题是,考虑到内部连接和where子句,上述更新是否有效?

方式:-1

方式:-2


这取决于您使用的数据库管理系统。该代码不符合ANSI SQL。请用正在运行的数据库标记您的问题:mysql、oracle、SQL server。。。?还有,这段代码的目的是什么?嗨,这是firebird。我现在就给它加了标签不,它不会工作,因为Firebird的更新SQL语法不支持FROM,也不支持CTEs Firebird只支持select表达式上的CTEs。你好,马克,你说得对。它不起作用,所以我尝试了另一种方法。TksFirebird的SQL方言在UPDATE中没有FROM子句,SQL标准语法IIRC也没有
WITH TB1 AS(SELECT TB_INDEX FROM TB0 WHERE TB_INDEX=10),
TB2 AS (SELECT TB_INDEX,COUNT(ORDERS))TOTAL FROM ORDERS INNER JOIN TB1 ON TB1.TB_INDEX=ORDERS.TB_INDEX)
UPDATE T SET T.STATUS='A'
FROM TB0 T WHERE EXISTS (SELECT 1 FRM TB2 WHERE T.TB_INDEX=TB2.TB_INDEX
TB2.TOTAL>30)
WITH TB1 AS(SELECT TB_INDEX FROM TB0 WHERE TB_INDEX=10),
TB2 AS (SELECT TB_INDEX,COUNT(ORDERS))TOTAL FROM ORDERS INNER JOIN TB1 ON TB1.TB_INDEX=ORDERS.TB_INDEX)
UPDATE T SET T.STATUS='A'
FROM TB2,TB0 T WHERE T.TB_INDEX=TB2.TB_INDEX
TB2.TOTAL>30