我正在尝试更新表格,特别是电话号码。
我制作了两个程序(也转换为函数):
CreateRandomMobileNumber
ALTER procedure [dbo].[GetRandomMobileNumber](
@min AS BIGINT,
@max AS BIGINT,
@mobile_num AS VARCHAR(20)
) AS BEGIN
SELECT @min = 1, @max = 99999999
SELECT @mobile_num = CAST(CAST(((@max + 1) - @min) * Rand() + @min as bigint) as varchar(15))
SELECT @mobile_num = '04' + ' ' + SUBSTRING(@mobile_num, 1, 4) + ' ' + RIGHT(@mobile_num,4)
SELECT @mobile_num
END
CreateRandomPhoneNumber
ALTER procedure [dbo].[GetRandomPhoneNumber](
@min AS BIGINT,
@max AS BIGINT,
@phone_num AS VARCHAR(20)
)
AS
BEGIN
SELECT @min = 1, @max = 99999999
Select @phone_num = CAST(CAST(((@max + 1) - @min) * Rand() + @min as bigint) as varchar(15))
SELECT @phone_num =
CASE
WHEN LEFT(@phone_num, 1) BETWEEN 1 AND 6
THEN '02' + ' ' + SUBSTRING(@phone_num, 1, 4) + ' ' + RIGHT(@phone_num,4)
WHEN LEFT(@phone_num, 1) BETWEEN 7 AND 8
THEN '03' + ' ' + SUBSTRING(@phone_num, 1, 4) + ' ' + RIGHT(@phone_num,4)
ELSE '07' + ' ' + SUBSTRING(@phone_num, 1, 4) + ' ' + RIGHT(@phone_num,4)
END
SELECT @phone_num
END
我想要做的是能够在更新时调用这些,这是插入语句:
; WITH CTE AS
(
SELECT *, rn = ROW_NUMBER() OVER ( ORDER BY newid() )
FROM anon_PersonChangeData
)
UPDATE c1
SET c1.personNewGender = c2.personCurrentGender
, c1.personNewFirstName = c2.personCurrentFirstName
, c1.personNewSurname = c3.personCurrentSurname
, c1.personNewHomeEmail = CASE
WHEN c1.personStatus IN ('both', 'candidate') AND c1.personCurrentFirstName LIKE '%p%'
THEN c2.personCurrentFirstName + '.' + c3.personCurrentSurname + '@yoohoo.com'
WHEN c1.personStatus IN ('both', 'candidate') AND c1.personCurrentFirstName LIKE '%e%'
THEN c2.personCurrentFirstName + '.' + c3.personCurrentSurname + '@hatmail.com'
ELSE c2.personCurrentFirstName + '.' + c3.personCurrentSurname + '@gmoil.com'
END
, c1.personNewWorkEmail = NULL
, c1.personNewHomePhone = dbo.GetRandomPhoneNumber
, c1.personNewWorkPhone = NULL
, c1.personNewMobilePhone = dbo.GetRandomMobileNumber
FROM CTE c1
INNER JOIN CTE c2 ON c1.rn = c2.rn
INNER JOIN CTE c3 ON c2.rn = c3.rn
我尝试了一些不同的东西,我的功能版本一直给我以下错误:
在函数中无效使用副作用运算符'rand'。
非常感谢任何帮助。
答案 0 :(得分:4)
这是一种简单的方法,使用"内联计数表"设置要生成的数量和 ABS(CHECKSUM(NEWID()))%N 以生成随机数。在这种情况下,我设置1000000000和9999999999之间的界限(因为我们想要10位数...
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b), -- 100 rows
cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b), -- 10,000 rows
cte_Tally (n) AS (
SELECT TOP 20 -- set the number of row you wanr here...
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM
cte_n3 a CROSS JOIN cte_n3 b -- 100,000,000 rows
)
SELECT
PhoneFormatted = '(' + STUFF(STUFF(pn.PhoneNumber, 7, 0, '-'), 4, 0, ') ')
FROM
cte_Tally
CROSS APPLY ( VALUES (CAST(ABS(CHECKSUM(NEWID())) % 9999999999 + 1000000001 AS VARCHAR(14))) ) pn (PhoneNumber);
结果......
PhoneFormatted
-------------------
(165) 103-7444
(288) 558-8646
(259) 635-8073
(277) 171-6645
(104) 265-1071
(119) 838-5696
(111) 378-0002
(202) 490-0627
(208) 400-6605
(101) 681-2601
(239) 842-7079
(257) 067-0305
(227) 761-1426
(171) 330-1550
(312) 728-6722
(108) 526-2654
(202) 573-9529
(205) 334-9711
(112) 345-8447
(202) 587-9164
最好的部分......它的速度很快......以下是将1M数字放入临时表的统计数据IO,时间。
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 7 ms.
SQL Server Execution Times:
CPU time = 1392 ms, elapsed time = 1224 ms.
(1000000 rows affected)
答案 1 :(得分:0)
如果您想要一支班轮,可以使用以下方法:
SELECT TOP 500 PhoneNumber = FORMAT(ROUND(((9999999999 - 1111111111)*RAND(CAST(DATEDIFF(s,'1970-01-01 12:00:00',GETDATE()) As BIGINT)+(ROW_NUMBER() OVER(ORDER BY (SELECT(NULL)))) ) + 1111111111),0),'(###) ###-####')
FROM anytable;