2017-08-01 1 views
-1

私は非常に複雑な質問があります。誰も私に次のシナリオのための指示を与えることができますテーブルの行を別のテーブルのフィールドに従って挿入または削除する

私は2つのテーブルを持っています。 tblone、tbltwo。 tbloneのQuantity値に従って、tbltwoのレコードを挿入または削除したいとします。たとえば、tbl.Quantityが4で、2行がtbltwoにある場合は、2行追加する必要があります。 tbl.Quantityが1で、tbltwoが3行の場合、2行を削除します。私は本当に混乱しています。どんな助けもありがとうございます。私はカーソルで試しましたが、成功しませんでした。

SELECT Quantity 
FROM tblOrder 
WHERE visitid = 123123 

結果は4

​​

結果2

は、だから私はVISITID = 123123

tblShipment

VisitID|Quantity|Type 
12313  4  cotton 

ためtblTwoから2つの行を追加したいですtblProducts

ProductID|type |method| 
2222  cotton first 
2223  cotton first 

期待される結果:

ProductID|type |method| 
2222  cotton first 
2223  cotton first 
2224  cotton first 
2225  cotton first 

私はこのメイクセンスを願って、私は

+1

データの表示と削除の基準が役立ちます。私は願います。また、あなたが試したことを示してください。 –

+1

したがって、 'tblone'の' Quantity'は、 'tbltwo'にいくつの行を入れるべきかを決定します。あれは正しいですか? –

+0

['MERGE'](https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql)は、[SAK](http://www.sakwiki.com)です。/tiki-index.php)を使用します。これはTSQLの正規表現でもあります:今は2つの問題があります。 – HABO

答えて

0

は、以下のコードを試してみて、あなたが直面しているなら、私に知らせ数量値に応じて行を追加または削除します任意の問題があります。

begin 

    DECLARE @visitid INTEGER; 
    DECLARE @quantity INTEGER; 
    DECLARE @type VARCHAR(100); 
    DECLARE @productid INTEGER; 
    DECLARE @method VARCHAR(100); 
    DECLARE @diff NUMERIC; 
    DECLARE @CNT INTEGER; 

with temp as 
(
    select type,count(1) as cnt 
    from tblproducts 
    group by type 
) 
select s.visitid,s.quantity,s.type,p.productid,p.method,temp.cnt as cnt 
into #tempor 
from tblshipment s join tblproducts p 
on s.type=p.type 
inner join temp on 
s.type=temp.type 
and s.quantity <> temp.cnt 

declare productcursor cursor for select 
visitid,quantity,type,productid,method,cnt 
from #tempor 

open productcursor 

fetch next from productcursor into 
@visitid,@quantity,@type,@productid,@method,@cnt 

while (@@FETCH_STATUS <> -1) 
BEGIN 
    declare @i integer; 
    set @i=1; 
if (@[email protected]) > 0 
    BEGIN 
     while (@i < (@[email protected])) 
     BEGIN 
      insert into tblproducts SELECT max(productid) + 1 AS 
      PRODCUTID,type AS TYPE,method AS METHOD 
      from tblproducts t 
      where [email protected] and [email protected] group by type,method 

      SET @i = @i+1; 
     END  
     fetch next from productcursor into 
    @visitid,@quantity,@type,@productid,@method,@cnt ; 
    END 
    ELSE IF (@[email protected]) <0 
    BEGIN 
     while (@i <= (@[email protected])) 
     BEGIN    
      select *, 
      row_number() over(partition by type,method order by productid 
      desc) as rn 
      into #del 
      from tblproducts  

      delete from tblproducts 
      where productid in (select productid from #del where rn <=(@cnt- 
      @quantity)) 
      and [email protected] and [email protected] 
      SET @i = @i+1; 
     END 
     fetch next from productcursor into 
     @visitid,@quantity,@type,@productid,@method,@cnt ; 
    END 
    END 

    close productcursor 
    deallocate productcursor ; 
END 
+0

ProductidがID列の場合は、挿入からproductidを削除して変更を反映させてください – Aparna

関連する問題