MS SQL标量函数,用于格式化基于laguage开关的日期(MS SQL scalar function to format datebased on language switch)

编程入门 行业动态 更新时间:2024-10-25 14:35:25
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 GO

I 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.

更多推荐

本文发布于:2023-08-05 18:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标量   函数   日期   laguage   MS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!