试试这个——
询问:
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