insert into PROMOTION_COUNT(MONTH_NUM,PROTO_NUM,SECTION_NUM,PRODUCT_COUNT) select month_num,proto_num,'11',count(product_code) from promotion_product where SUBSTRING(PRODUCT_CLASS,2,2)='11' and checked_flag='0' and PRODUCT_TYPE not LIKE '5%' GROUP BY month_num,proto_num
----
if (select count(*) from promotion_product where SUBSTRING(PRODUCT_CLASS,2,2)='11' and checked_flag='0' and PRODUCT_TYPE not LIKE '5%' GROUP BY month_num,proto_num)>0
begin
---
UPDATE PROMOTION_COUNT SET PRODUCT_TYPE5=(SELECT count(product_code) FROM promotion_product WHERE PROMOTION_COUNT.MONTH_NUM=promotion_product.MONTH_NUM AND PROMOTION_COUNT.PROTO_NUM=promotion_product.PROTO_NUM AND promotion_product.checked_flag='0' AND SUBSTRING(promotion_product.PRODUCT_CLASS,2,2)=PROMOTION_COUNT.SECTION_NUM AND promotion_product.PRODUCT_TYPE LIKE '5%')
----
end
---

解决方案 »

  1.   

    MONTH_NUM,PROTO_NUM,SECTION_NUM不为空;CrazyFor(蚂蚁) :
    if (select count(*) from promotion_product where SUBSTRING(PRODUCT_CLASS,2,2)='11' and checked_flag='0' and PRODUCT_TYPE not LIKE '5%' GROUP BY month_num,proto_num)=0
    这个时候想insert MONTH_NUM,PROTO_NUM,SECTION_NUM,PRODUCT_TYPE5这几个字段,同时让PRODUCT_COUNT的值为0,怎么解决?
      

  2.   

    第一个sql
    IF @@ROWCOUNT <> 0
       第二个 sql
     ELSE
        --多少给点提示信息