Hi there, I have recently started a new job. They are using SQL server 2008. When looking at stored procedures. I have noticed that they are declaring variables, then declaring those variables again as a local variable. This is to help on the indexing and speed of the stored procedures. From what I have been told. An example is in the code attached. I was curious to know why they might do this ?
I want to make changes but I am wanting to understand their reasons as to why the stored procedures were written this way. If any one can help or show some links to what the benefits of @ local Variable? I would be most grateful.
ALTER PROCEDURE [dbo].[CVDHA] -- Add the parameters for the stored procedure here @YEAR nvarchar (4) , @MONTH nvarchar (2),@DHB nvarchar (60), @PHO nvarchar (60), @Prac nvarchar (100), @User nvarchar (60) , @Where nvarchar (15) with recompile AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @To_LOCAL datetime, @YEAR_LOCAL nvarchar (4) , @MONTH_LOCAL nvarchar (2), @DHB_LOCAL nvarchar (60), @PHO_LOCAL nvarchar (60), @Prac_LOCAL nvarchar (100), @User_LOCAL nvarchar (60), @Where_LOCAL nvarchar(15), @SqlStatment nvarchar(max), @scope nvarchar(10), @tablename nvarchar(max) SET @YEAR_LOCAL = @YEAR SET @MONTH_LOCAL = @MONTH SET @DHB_LOCAL = @DHB SET @PHO_LOCAL = @PHO SET @Prac_LOCAL = @Prac SET @To_LOCAL=DateAdd(Second, -1, dateadd(qq, DateDiff(qq, -1, convert(datetime,(@YEAR_LOCAL+ '-'+ @MONTH_LOCAL + '-01'),102)), 0)) SET @User_LOCAL = @User SET @Where_LOCAL = @Where