对于MSSQL 2014,我需要帮助将500万个随机行添加到表中,其中varchar(25)pk列,第二个BIGINT &第三个nvarchar(256)。我如何编写脚本?向表中添加唯一的随机500万行
[所以,我想到了首先将值填充到主键列和BIGINT列中。我每次运行该脚本,我得到的算术溢出错误数据类型转换期间]
Create table Alok(Col1 varchar(25) NOT NULL PRIMARY KEY, Col2 BIGINT NOT NULL, Col3 nvarchar(256))
--At first, just trying to insert values for PKColumn and BIGINT Column as couldn't script for all three columns at once.
DECLARE @RowCount INT
DECLARE @RowString VARCHAR(25)
DECLARE @Random INT
DECLARE @Upper INT
DECLARE @Lower INT
DECLARE @InsertCol2 BIGINT
--To configure the items that are set once per run.
SET @Lower = 0
SET @Upper = 9223372036854775807
SET @RowCount = 0
--Setting up the RowCount
WHILE @RowCount < 5000000
BEGIN
SET @RowString = CAST(@RowCount AS VARCHAR(25))
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SET @InsertCol2 = ROUND(((@Lower + 9000000000) * RAND()), 15)
INSERT INTO Alok
(Col1
,Col2)
VALUES
(REPLICATE('0', 25 - DATALENGTH(@RowString)) + @RowString
, @InsertCol2)
SET @RowCount = @RowCount + 1
END
请包括脚本 –
即使你得到这个工作,它需要几天才能完成。 – ATC
是的!这一直保持执行2个多小时,然后我停止了它,并试图通过将RowCount限制为100来检查它是否能够正常工作。然后,我收到了数据类型转换的错误。 – Alok