2017-09-27 13 views
0

次のシナリオがあります。値を挿入する2つのUDT

PFB私のクエリ

DECLARE @termlength AS Prdctlg.ContractTermLengthType; 

DECLARE @PromotionIdsTable AS Prdctlg.PromotionsType 

INSERT INTO @termlength (PromotionName, 
PromotionStartDate, 
PromotionEndDate, 
PromotionDescription, 
TermLengthPrice, 
LengthInMonths) 
    VALUES ('CT1', '02/02/2017', '02/02/2019', 'Desc', '12', '15') 

INSERT INTO @termlength (PromotionName, 
PromotionStartDate, 
PromotionEndDate, 
PromotionDescription, 
TermLengthPrice, 
LengthInMonths) 
    VALUES ('CT12', '02/02/2017', '02/02/2018', 'Desc13', '122', '152') 

INSERT INTO PrdCtlg.Promotions (PromotionName, PromotionStartDate, PromotionEndDate, PromotionTypeId) 
OUTPUT INSERTED.pkPromotionsId INTO @PromotionIdsTable (PromotionsId) 
    SELECT 
    PromotionName, 
    PromotionStartDate, 
    PromotionEndDate, 
    1 
    FROM @termlength 


SELECT 
    * 
FROM @PromotionIdsTable 
SELECT 
    * 
FROM @termlength 

私は@termlength TermLengthIdに@PromotionIdsTable UDTのこれらの値を挿入します。どの働かはあり

ここ
+2

、 –

+0

あなたが意味するか私達にあなたの試みとサンプルデータを表示してください:*同じ* 'PromotionsId 'しかし、異なる'プロモーション記述 '? – Richard

+0

どの行を削除しますか?どの行が正しいかを決定する必要があります –

答えて

1

あなたが行く:

DECLARE @T TABLE (PromotionsId INT, PromotionDescription NVARCHAR(50), TermLengthPrice MONEY); 
/**/ 
INSERT INTO @T VALUES 
(3168, 'Desc', 12), 
(3168, 'Desc 13', 122), 
(3169, 'Desc', 12), 
(3169, 'Desc 13', 122), 

(3170, 'Asc', 12), 
(3170, 'Asc 13', 122), 
(3171, 'Asc', 12), 
(3171, 'Asc 13', 122); 

WITH C1 AS(
SELECT *, ROW_NUMBER() OVER (PARTITION BY PromotionDescription ORDER BY PromotionDescription) AS RN 
FROM @T 
    ) 
    , 
    C2 AS 
    (
     SELECT *, ROW_NUMBER() OVER (PARTITION BY PromotionDescription ORDER BY PromotionDescription) AS RN 
     FROM @T 
     ) 
     , FinalTable AS(
     SELECT * 
     FROM C1 
     WHERE RN = 1 
     UNION ALL 
     SELECT * 
     FROM C2 
     WHERE RN=1) 
     SELECT DISTINCT PromotionsId, PromotionDescription, TermLengthPrice 
     FROM FinalTable; 

結果:

+==============+======================+=================+ 
| PromotionsId | PromotionDescription | TermLengthPrice | 
+==============+======================+=================+ 
|   3168 | Desc     |   12,0000 | 
+--------------+----------------------+-----------------+ 
|   3169 | Desc 13    |  122,0000 | 
+--------------+----------------------+-----------------+ 
|   3170 | Asc     |   12,0000 | 
+--------------+----------------------+-----------------+ 
|   3171 | Asc 13    |  122,0000 | 
+--------------+----------------------+-----------------+ 
+0

これは、定数4行の結果です。私は6/8の結果を持っていない場合は動作しません – Sana

+0

しかし、そのおかげでどこかに私を案内します – Sana

+0

@サナ私の更新をより多くの行でご覧ください。 – Sami

0
INSERT INTO PrdCtlg.Promotions (PromotionName, PromotionStartDate, PromotionEndDate, PromotionTypeId) 
SELECT PromotionName,PromotionStartDate,PromotionEndDate,PromotionDescription,TermLengthPrice,LengthInMonths 
FROM @PromotionIdsTable UNION ALL 
SELECT PromotionName,PromotionStartDate,PromotionEndDate,PromotionDescription,TermLengthPrice,LengthInMonths 
FROM @termlength 
関連する問題