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作為聲明,以@
開頭作為標識,最后再加一個變量的類型 - 循環(huán)
WHILE 條件
BEGIN
操作
SET @i=@i+1
END