MS SQL标量函数,用于格式化基于laguage开关的日期(MS SQL scalar function to format datebased on language switch)
我创建了一个标量函数,它应该根据变量以特定格式吐出日期。 由于某种原因,我在“其他”周围得到一个错误
我究竟做错了什么 ?
CREATE FUNCTION [dbo].[fGetDateformat] (@datum datetime,@CNotation char(2)) /* accepts datum and CNotation USAGE: select fGetDateformat(datum, "EN" or "DE") */ returns varchar(25) AS BEGIN declare @ReturnStr varchar(25) if @Cnotation = 'DE' set language german set @ReturnStr = DATENAME(dd, @datum)+'. '+DATENAME(MM, @datum)+' '+ DATENAME(YEAR, @datum) else set @ReturnStr = DATENAME(mm, @datum)+' '+DATENAME(dd, @datum)+', '+ DATENAME(YEAR, @datum) return @ReturnStr END GOI have created a scalar function that is supposed to spit out a date in a specific format based on a variable. for some reason i get an error around the "else"
what am i doing wrong ?
CREATE FUNCTION [dbo].[fGetDateformat] (@datum datetime,@CNotation char(2)) /* accepts datum and CNotation USAGE: select fGetDateformat(datum, "EN" or "DE") */ returns varchar(25) AS BEGIN declare @ReturnStr varchar(25) if @Cnotation = 'DE' set language german set @ReturnStr = DATENAME(dd, @datum)+'. '+DATENAME(MM, @datum)+' '+ DATENAME(YEAR, @datum) else set @ReturnStr = DATENAME(mm, @datum)+' '+DATENAME(dd, @datum)+', '+ DATENAME(YEAR, @datum) return @ReturnStr END GO最满意答案
如果T-SQL块跨越多行,则必须将其括在begin和end 。 例如,这不起作用:
if 1=1 print 'One is one!' print 'Yay' else print 'Huh?'这会:
if 1=1 begin print 'One is one!' print 'Yay' end else print 'Huh?'请注意print 'Huh?' 仍然没问题,因为它是单行。
If a T-SQL block spans multiple lines, you have to enclose it in begin and end. For example, this won't work:
if 1=1 print 'One is one!' print 'Yay' else print 'Huh?'This will:
if 1=1 begin print 'One is one!' print 'Yay' end else print 'Huh?'Note that print 'Huh?' is still okay, since it is a single line.
更多推荐
发布评论