0

カウントが 0 の場合にカウント レコードをチェックする必要があるクエリを作成する必要があります。レコードを挿入するか、そうでない場合はレコードを更新します

カーソルを使用して同じことを行うと、正常に機能しますが、カーソルがないと同じことが機能しません

これが私のクエリです(カーソルなし)

---カーソルなし

INSERT INTO [dbo].Products_Del  (product_Id, product_Del_startdate)                 
        SELECT f.product_Id, min(cast(product_startdate as datetime)) AS    orig_order_date FROM   [dbo].Products f
            inner  join [dbo].Products_Del ac on f.product_Id = ac.product_Id
            WHERE  Product_Status = 'ORDERED'
            AND    product_startdate != '.'    
            AND (select COUNT(*) FROM  products f1  
            INNER JOIN dbo.Products_Del ac on f1.product_Id = ac.product_Id 
            where f1.product_Id = f.product_Id) = 0  
            GROUP BY f.product_Id --order by product_Id



-- Update if exists
        ;with cts 
        AS ( 
            SELECT  product_Id , min(cast(product_startdate as datetime)) as orig_date from [dbo].Products f
                WHERE product_Id in (select product_Id from Products_Del)
                and  Product_Status = 'ORDERED'
                AND    product_startdate != '.'    -- ignore rows where date is unknown
                AND (select COUNT(*) FROM  Products f1  
                INNER JOIN dbo.Products_Del ac on f1.Product_id = ac.Product_id 
                where f1.product_Id = f.product_Id) = 1  
                GROUP BY product_Id 
            )
        UPDATE ac
        SET ac.product_Del_startdate = cts.orig_date
        FROM Products_Del ac
        INNER JOIN cts ON ac.product_Id = cts.product_Id 

しかし、これはうまく機能します(カーソルを使用)

DECLARE @v_count        INT
    DECLARE @c_product_id   INT
    DECLARE @c_product_date DATETIME


DECLARE  cursor1 CURSOR FOR
SELECT product_id,
       min(cast(product_startdate as DATETIME)) AS orig_order_date
FROM   [dbo].Products
WHERE  Product_Status = 'ORDERED'
AND    product_startdate != '.'    -- ignore rows where date is unknown
GROUP BY product_id
--order by product_id

OPEN cursor1
FETCH NEXT FROM cursor1 INTO @c_product_id,@c_product_date
WHILE(@@FETCH_STATUS = 0)
BEGIN
    SELECT @v_count = COUNT(*)
             FROM   [dbo].Products_Del
            WHERE  product_Id = @c_product_id
            IF @v_count = 1 
            BEGIN
               -- If so, plug the date into that row.
               UPDATE [dbo].Products_Del
               SET    product_Del_startdate = @c_product_date
               WHERE  product_Id = @c_product_id
            END
            ELSE
            BEGIN
               -- If not, then create a new row in the aircraft_delivery_status table
               IF @v_count = 0 
               BEGIN
                  INSERT INTO [dbo].Products_Del
                         (product_Id, product_Del_startdate)
                  VALUES (@c_product_id, @c_product_date)
               END 
            END 

     FETCH NEXT FROM cursor1 INTO @c_product_id,@c_product_date       

END
CLOSE cursor1
DEALLOCATE cursor1

スキーマとの SQL Fiddle リンク http://sqlfiddle.com/#!6/a7d0d/1

4

1 に答える 1