嘗試這一個 -
查詢:
DECLARE @temp TABLE
(
product INT
, [order] INT
, ord_qnty INT
)
INSERT @temp(product, [order], ord_qnty)
SELECT 10, 3, 4
SELECT
t.product
, t.[order]
, ord_qnty = 1
FROM @temp t
JOIN [master].dbo.spt_values sv ON t.ord_qnty > sv.number
WHERE sv.[type] = 'p'
SELECT
t.product
, t.[order]
, ord_qnty = 1
FROM @temp t
JOIN (
SELECT number = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM sys.system_parameters p
) sv ON t.ord_qnty >= sv.number
輸出:
product order ord_qnty
----------- ----------- -----------
10 3 1
10 3 1
10 3 1
10 3 1
查詢費用:
對於任何 「數以百萬計值」:
SET NOCOUNT ON;
DECLARE @numbers TABLE (number INT)
DECLARE @temp TABLE
(
product INT
, [order] INT
, ord_qnty INT
)
INSERT @temp(product, [order], ord_qnty)
SELECT 10, 3, 4
DECLARE
@i BIGINT = 1
, @max BIGINT = (
SELECT MAX(ord_qnty)
FROM @temp
)
WHILE (@i <= @max) BEGIN
INSERT INTO @numbers (number)
VALUES (@i), (@i+1), (@i+2), (@i+3), (@i+4), (@i+5), (@i+6), (@i+7), (@i+8), (@i+9)
SELECT @i += 10
END
SELECT
t.product
, t.[order]
, ord_qnty = 1
FROM @temp t
CROSS JOIN (
SELECT *
FROM @numbers
WHERE number < @max + 1
) t2
@Nishad,請問你是什麼不喜歡我的回答? – Devart
THERZ沒什麼,我不喜歡:),有什麼,你問? – Nishad
感謝您的回答。 :) – Devart