我需要创建一个存储过程,它可以检查给定的开始和结束年份之间是否存在年份并返回那些存在的年份。例如,如果我提供 2000 作为开始年份和 2005 结束年份,那么它应该返回表中存在的那些年份。我已经尝试了以下代码,但它不起作用。
ALTER PROCEDURE [dbo].[CheckExistenceforyear]
@StYear int = 0,
@EdYear int = 0,
@SubStationID int = 0,
@YrString varchar(255) = null Output
AS
BEGIN
SET NOCOUNT ON;
Declare @TempValue varchar(4)
if @StYear <= @EdYear
while @StYear <= @EdYear
Begin
if (select Count(Sub.Year) from table sub where Sub.Year=@StYear and Sub.SubStationID = @SubStationID) > 0
Begin
select @TempValue=Sub.Year from table as sub where Sub.Year=@StYear and Sub.SubStationID = @SubStationID
set @YrString = @YrString + @TempValue
End
set @StYear = @StYear +1
End
Else
return -1
select @YrString
END