In Sql Server, I wrote a procedure for getting last 3 years, which includes this year, and it returns only year [ like 2011,2010,2009]. This is my procedure. Is there any optimisation required? Please check the procedure.
create procedure UPS_GetYears
as
begin
declare @date varchar(10)
declare @i int=0
declare @var varchar(60)
declare @y int =-1
select @date = SUBSTRING (CONVERT (varchar,GETDATE(),103),7,4)
create table #Temp (years varchar(10))
while (@i < 3)
begin
insert into #Temp (years) values(@date )
select @date = SUBSTRING (convert(varchar, DATEADD (yyyy,@y,getdate()),103),7,4)
set @i=@i+1
set @y =@y-1
end
select * from #Temp
end
I’d just do
Also why is this a procedure rather than a TVF?