我继承了SQL Server的以下代码:
SET NOCOUNT ON
DECLARE
@PERSON_ID AS INT,
@COMMS_DATE AS DATE,
@PERSON_ID_HOLD AS INT,
@COMMS_DATE_HOLD AS DATE,
@CAMPAIGN_ID AS INT,
@CAMPAIGN_ID_OLD AS INT,
@SQL AS VARCHAR(200)
SELECT @PERSON_ID_HOLD = 0
SELECT @COMMS_DATE_HOLD = null
SELECT @CAMPAIGN_ID = 0
SELECT @CAMPAIGN_ID_OLD = 0
DECLARE COMMS_CURSOR CURSOR FOR
SELECT PERSONID, ReportDate FROM ##Motability_Comms_Combined order by PersonID, ReportDate
OPEN COMMS_CURSOR
FETCH NEXT FROM COMMS_CURSOR
INTO @PERSON_ID, @COMMS_DATE
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@PERSON_ID = @PERSON_ID_HOLD) and (DATEDIFF(day,@COMMS_DATE_HOLD,@COMMS_DATE) > 90)
BEGIN
IF @CAMPAIGN_ID = 0
begin
SET @PERSON_ID_HOLD = @PERSON_ID
SET @COMMS_DATE_HOLD = @COMMS_DATE
end
ELSE
begin
SET @CAMPAIGN_ID_OLD = @CAMPAIGN_ID
SET @CAMPAIGN_ID = @CAMPAIGN_ID + 1
END
end
ELSE
IF (@PERSON_ID <> @PERSON_ID_HOLD)-- or (@COMMS_DATE <> @COMMS_DATE_HOLD)
BEGIN
SET @CAMPAIGN_ID = 1
SET @CAMPAIGN_ID_OLD = @CAMPAIGN_ID
END
IF (@CAMPAIGN_ID > 1)
BEGIN
UPDATE ##Motability_Comms_Combined SET Derived_Campaign_ID = @CAMPAIGN_ID WHERE personid = @PERSON_ID AND REPORTDATE = @COMMS_DATE --WHERE CURRENT OF COMMS_CURSOR
END
SET @PERSON_ID_HOLD = @PERSON_ID
SET @COMMS_DATE_HOLD = @COMMS_DATE
FETCH NEXT FROM COMMS_CURSOR INTO @PERSON_ID, @COMMS_DATE
END
CLOSE COMMS_Cursor
DEALLOCATE COMMS_Cursor
UPDATE ##Motability_Comms_Combined SET Derived_Campaign_ID = 1 WHERE Derived_Campaign_ID is null
SET NOCOUNT OFF
任何人都可以告诉我这种SQL的目的是什么,例如nocount,声明,游标,等等?是否有另一种方式来做选择呢?
感谢您提前提供任何帮助。
答案 0 :(得分:0)
CURSORS用于以过程方式执行操作,而不是通常的基于集合的方式(例如,如果需要循环执行每行执行存储过程的某些数据)。他们资源密集,通常不赞成。看来这可以在没有光标的情况下重写。
&#34; ## Motability_Comms_Combined是一个全局临时表,因此也可能被其他代码引用(在不同的SQL连接中)