这可能是做你想做的事情的一个很好的起点:
SQL小提琴
MS SQL Server 2012 架构设置:
CREATE TABLE Content (
[ContentId] INT NOT NULL,
[Title] NVARCHAR (50) Not NULL,
CONSTRAINT [PK_Content] PRIMARY KEY CLUSTERED ([ContentId] ASC)
);
查询 1:
DECLARE @key VARCHAR(5), @i int
DECLARE @query VARCHAR(120)
SET @i = 1
WHILE @i > 0
BEGIN
SET @key = (SELECT ABS(Checksum(NewID()) % 89999) + 10000)
SET @i = (SELECT count(*) FROM Content WHERE ContentId = @key)
END
SET @query = 'INSERT INTO Content (ContentId,Title) VALUES ('+@key+',''Whatever'+@key+''');'
exec(@query)
结果:
查询 2:
DECLARE @key VARCHAR(5), @i int
DECLARE @query VARCHAR(120)
SET @i = 1
WHILE @i > 0
BEGIN
SET @key = (SELECT ABS(Checksum(NewID()) % 89999) + 10000)
SET @i = (SELECT count(*) FROM Content WHERE ContentId = @key)
END
SET @query = 'INSERT INTO Content (ContentId,Title) VALUES ('+@key+',''Whatever'+@key+''');'
exec(@query)
结果:
查询 3:
select * from Content
结果:
| CONTENTID | TITLE |
|-----------|---------------|
| 22537 | Whatever22537 |
| 66089 | Whatever66089 |