SQL如下:
SELECT DISTINCT ProjectID INTO #temp1 FROM ProjectSettings
SELECT ROW_NUMBER() OVER(ORDER BY ProjectID) AS Row_num,ProjectID INTO #temp2 FROM #temp1
DECLARE @count INT
DECLARE @i INT
SELECT @count=COUNT(DISTINCT Projectid) FROM ProjectSettings
SET @i=1
WHILE @i<@count
BEGIN
DECLARE @projectid INT
SELECT @projectid=ProjectID FROM #temp2 WHERE Row_num=@i
IF NOT EXISTS(SELECT * FROM ProjectSettings WHERE ProjectID=@projectid AND Name='SDVWorkFlowID')
--插入操作
SET @i=@i+1
END
DROP TABLE #temp1
DROP TABLE #temp2
这个SQL语句中用到了如下几个知识点:
- 临时表
#temp1
和#temp2
都是临时表,以#
开头作为标识,最后记得删除临时表 - 变量的定义
以DECLARE作为声明,以@
开头作为标识,最后再加一个变量的类型 - 循环
WHILE 条件
BEGIN
操作
SET @i=@i+1
END
网友评论