我有以下情况。
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的这些值。有没有可能性
答案 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 |
+--------------+----------------------+-----------------+
答案 1 :(得分: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