我想在SQL中创建系列数据以用于测试目的。
实施例。
SELECT Entry.* FROM Entry WHERE body LIKE ?
我想在SQL中创建类似上述方式的1000条记录。 谁能建议我解决方案?
谢谢
答案 0 :(得分:2)
如果您正在使用SQL Server,则可以通过recursive
方式执行此操作:
with series as (
select 1 as id
union all
select id+1
from series
where id < 1000
)
select concat('Product', id)
from series s
option (maxrecursion 1000);
答案 1 :(得分:1)
使用ad-hoc计数表
的另一个选项示例强>
Select Top 1000 Product=concat('Product',Row_Number() Over (Order By (Select NULL)))
From master..spt_values n1
<强>返回强>
Product1
Product2
Product3
Product4
...
Product998
Product999
Product1000
答案 2 :(得分:0)
你走了:
DECLARE @temp TABLE(
Products VARCHAR(15)
);
DECLARE @counter INT
SET @counter = 1
WHILE (@counter <= 1000)
BEGIN
INSERT INTO @temp
VALUES('Product'+CONVERT(VARCHAR(5),@counter));
SET @counter = @counter + 1
END
SELECT * FROM @temp;
答案 3 :(得分:0)
这应该让你开始。
DECLARE @table table (Product varchar(200))
DECLARE @cnt INT; SET @cnt = 0
WHILE @cnt <=10
BEGIN
SET @cnt = @cnt + 1
insert into @table
SELECT 'Product' +CAST(@cnt as varchar(150))
END
select * from @table
答案 4 :(得分:0)
基于集合的CTE可以快速生成测试数据。更好的是一个数字(a.k.a.)数字表。
WITH
t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
,t1k AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t10 AS a CROSS JOIN t10 AS b)
SELECT 'Product' + CAST(num AS varchar(10))
FROM t1k;