我必须创建一些随机数据,到目前为止,在这个论坛的帮助下,我已经完成了 80%,但我现在(再次)陷入困境
我需要的是我有一个名为 Requested Date 的列,这个日期应该是随机的,但比订单日期晚 1 到 10 天。这可以做到吗?
DECLARE @OrderNumber varchar (30)
DECLARE @OrderDate int
DECLARE @OrderLineNumber varchar(50)
DECLARE @CustomerSkey int
DECLARE @ProductSkey int
DECLARE @OrderMethodSkey int
DECLARE @Quantity int
DECLARE @Cost Decimal(18,3)
DECLARE @RequestedDate int
SET @OrderNumber = 1
SET @OrderDate = 0
SET @OrderLineNumber = 1
SET @CustomerSkey = 1
SET @ProductSkey = 1
SET @OrderMethodSkey = 1
SET @Quantity = 1
SET @Cost = 1
SET @RequestedDate = 0
WHILE @OrderNumber <= 100
WHILE @OrderDate <= 100
WHILE @OrderLineNumber <= 100
WHILE @CustomerSkey <= 100
WHILE @ProductSkey <= 100
WHILE @OrderMethodSkey <= 100
WHILE @Quantity <= 100
WHILE @Cost <= 100
WHILE @RequestedDate <= 100
BEGIN
INSERT INTO Orders
(OrderNumber
, OrderDate
, OrderLineNumber
, CustomerSkey
, ProductSkey
, OrderMethodSkey
, OrderTime
, Quantity
, Cost
, RequestedDate)
SELECT
'ORD' + Right ('000000' + CAST (@OrderNumber AS varchar (30)), 6)
,DATEADD (day, CAST (RAND () * 1500 as int), '2008-1-1')
,(Right ('0' + CAST (@OrderLineNumber AS varchar (30)), 6))
,(99 * RAND()) + 1
,(99 * RAND()) + 1
,(2 * RAND()) + 1
,DATEADD(ms, cast(86400000 * RAND() as int), convert(time, '00:00'))
,(190 * RAND()) + 10
,(40 * RAND()) + 10
,DATEADD (day, CAST (RAND () * 10 as int), @RequestedDate)
SET @OrderNumber = @OrderNumber + 1
SET @OrderDate = @OrderDate + 1
SET @OrderLineNumber = @OrderLineNumber + 1
SET @CustomerSkey = @CustomerSkey + 1
SET @ProductSkey = @ProductSkey + 1
SET @OrderMethodSkey = @OrderMethodSkey + 1
SET @Quantity = @Quantity + 1
SET @Cost = @Cost + 1
SET @RequestedDate = @RequestedDate + 1
END