I have a stored procedure which should calculate one's age.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[ageTest]
(
@curDate date
)
AS
BEGIN
SET NOCOUNT ON;
declare @sql nvarchar(max)
declare @params nvarchar (1000)
declare @age date
set @params = N' @curDate date , @age date output'
set @sql = '
declare @dif float
declare @ageRound int
declare @theAge varchar (100)
set @age = ''19890406''
set @theAge =(select (datediff (mm, @age , getdate())))
set @dif = @theAge % 12
set @ageRound = cast (@theAge as float)/12
select @ageRound as Years, @dif as months
'
set @sql = replace (@sql, '19890406', @curDate)
execute sp_executesql @sql, @params, @curDate, @age output
end
execute [dbo].[ageTest] '19511214'
What I want to obtain is two columns:
Years Months
63 10
The problem is it loops. I should probably remove the select from @sql
and put it outside, then I have the declaration problem. Thoughts?
Edit: not a duplicate