我創建已經在表中(CandidatosVestAtual
)候選人的程序(sp_ET1_ClassificacaoPPIInferior
)按分數(Numr_TotalEt1
)排序特定類別(TipoPrograma = 1
):程序在SQL Server 2008中
CREATE TABLE [dbo].[CandidatosVestAtual](
[NUMG_Candidato] [bigint] NOT NULL,
[CODG_Curso] [varchar](4) NOT NULL,
[TipoPrograma] [int] NOT NULL,
[Numr_TotalEt1] [int] NULL,
[Numr_Classificacao_PPIInf_Et1] [int] NULL,
CONSTRAINT [PK_CandidatosVestAtual_1] PRIMARY KEY CLUSTERED ([NUMG_Candidato] ASC)
)
遵循程序的代碼使用:
ALTER PROCEDURE [dbo].[sp_ET1_ClassificacaoPPIInferior] @CODG_Curso varchar(4)
AS
DECLARE @Cnt INT
DECLARE @NUMG_Candidato INT
DECLARE Candidatos_C CURSOR FOR
SELECT NUMG_Candidato
FROM CandidatosVestAtual
WHERE TipoPrograma in (1) AND
Codg_Curso [email protected]_Curso
ORDER BY Numr_TotalEt1 DESC
set @Cnt = 1
OPEN Candidatos_C
FETCH NEXT FROM Candidatos_C INTO @NUMG_Candidato
WHILE (@@FETCH_STATUS = 0)
BEGIN
UPDATE CandidatosVestAtual SET [email protected]
FROM CandidatosVestAtual
WHERE [email protected]_Candidato
SET @Cnt = @Cnt + 1
FETCH NEXT FROM Candidatos_C INTO @NumG_Candidato
END
CLOSE Candidatos_C
DEALLOCATE Candidatos_C
然而,這個程序沒有做任何事情,因爲它代表。當我改變了原來的查詢程序這只是做了一些事情:
SELECT NUMG_Candidato
FROM CandidatosVestAtual
WHERE TipoPrograma in (1)
AND Codg_Curso = @ CODG_Curso
ORDER BY DESC Numr_TotalEt1
通過
SELECT NUMG_Candidato
FROM CandidatosVestAtual
WHERE Codg_Curso = @CODG_Curso
ORDER BY DESC Numr_TotalEt1
我怎樣才能使與原來的查詢程序工作?
您確定該表中的記錄與原始查詢匹配嗎?如果刪除其中的一個條件,然後運行,則應該告訴您問題出在數據上。 –
另外,你應該考慮用一個基於集合的操作替換這個遊標......這很容易用ROW_NUMBER()函數重新實現。 –
當你運行'SELECT NUMG_Candidato FROM CandidatosVestAtual WHERE TipoPrograma in(1)' ? – Beth