本文介绍了带有返回值的RETURN语句不能在此上下文中使用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 创建 功能年龄( @ date datetime ) return as begin DECLARE @ tmpdate datetime , @ years int , @ months int , @ days int , @ age varchar ( 12 ) - 声明@date datetime - set @ date = '12 / 29/04' SELECT @ tmpdate = @ date SELECT @ years = DATEDIFF(yy, @ tmpdate ,GETDATE()) - CASE WHEN (月( @ date )> MONTH(GETDATE())) OR (MONTH( @ date )= MONTH(GETDATE()) AND DAY( @ date )> DAY(GETDATE()))那么 1 ELSE 0 END SELECT @tmpdate = DATEADD(yy, @ years , @ tmpdate ) SELECT @ months = DATEDIFF(m, @ tmpdate ,GETDATE()) - CASE WHEN DAY( @ date )> DAY(GETDATE()) THEN 1 ELSE 0 END SELECT @ tmpdate = DATEADD(m, @ months , @tmpdate ) SELECT @ days = DATEDIFF(d, @ tmpdate ,GETDATE()) set @ age = 转换( varchar ( 12 ),(cast( @ years as varchar ( 2 ))+ ' - ' + cast( @ months as varchar ( 2 ))+ ' - ' + cast( @days as varchar ( 4 ))), 11 ) - 选择@age 返回 @ age end 我想创建一个函数,它给出给定日期的输出并将其与今天进行比较给出输出日期。解决方案 你需要改进你的查询。 改变你的返回语句,如return [datatype] i改变它 create function 年龄( @ date datetime ) 返回 datetime - 您的问题在这里 as 开始 DECLARE @tmpdate datetime , @ years int , @ months int , @days int , @ age varchar ( 12 ) - 声明@date datetime - set @ date = '12 / 29/04' SELECT @ tmpdate = @ date SELECT @ years = DATEDIFF (yy, @ tmpdate ,GETDATE()) - CASE WHEN (MONTH( @ date )> MONTH(GETDATE())) OR (MONTH( @ date )= MONTH(GETDATE()) AND DAY( @ date )> DAY(GETDATE()))那么 1 ELSE 0 END SELECT @tmpdate = DATEADD(yy, @ years , @ tmpdate ) SELECT @ months = DATEDIFF(m, @ tmpdate ,GETDATE()) - CASE WHEN DAY( @ date )> DAY(GETDATE()) THEN 1 ELSE 0 END SELECT @ tmpdate = DATEADD(m, @ months , @tmpdate ) SELECT @ days = DATEDIFF(d, @ tmpdate ,GETDATE()) set @ age = 转换( varchar ( 12 ),(cast( @ years as varchar ( 2 ))+ ' - ' + cast( @ months as varchar ( 2 ))+ ' - ' + cast( @days as varchar ( 4 ))), 11 ) - 选择@age 返回 @ age end 您需要声明函数的返回类型: 创建 功能年龄( @date datetime ) RETURNS VARCHA R ( 12 ) as create function Age(@date datetime)returnasbeginDECLARE @tmpdate datetime, @years int, @months int, @days int, @age varchar(12)--declare @date datetime--set@date='12/29/04'SELECT @tmpdate = @dateSELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 ENDSELECT @tmpdate = DATEADD(yy, @years, @tmpdate)SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 ENDSELECT @tmpdate = DATEADD(m, @months, @tmpdate)SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())set @age= Convert(varchar(12),(cast (@years as varchar(2)) + '-'+ cast(@months as varchar(2))+'-'+ cast(@days as varchar(4))),11) --select @agereturn @ageendI would like to create function which gives the output from the given date and compare it with today and give output date. 解决方案 you need to improve your query.change your return statement like returns [datatype]i changed itcreate function Age(@date datetime)returns datetime-- your problem was hereasbeginDECLARE @tmpdate datetime, @years int, @months int, @days int, @age varchar(12)-- declare @date datetime-- set @date='12/29/04'SELECT @tmpdate = @dateSELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) -CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 ENDSELECT @tmpdate = DATEADD(yy, @years, @tmpdate)SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) -CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 ENDSELECT @tmpdate = DATEADD(m, @months, @tmpdate)SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())set @age= Convert(varchar(12),(cast (@years as varchar(2)) + '-'+ cast(@months as varchar(2))+'-'+ cast(@days as varchar(4))),11)-- select @agereturn @ageendYou need to declare the return type of your function:create function Age(@date datetime)RETURNS VARCHAR(12)as 这篇关于带有返回值的RETURN语句不能在此上下文中使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 21:49