存储过程与用户定义函数的错误处理
我的 ERP 数据库使用不可为空的日期时间字段。但是,当日期时间不可用时,它会输入 '' 并返回 '1900-01-01 00: 00: 00.000' 作为值。
我想抑制 1900 个日期,同时仅从日期时间字段中删除日期。我创建了以下 UDF 来执行此操作:
CREATE FUNCTION ExtractDate(@DirtyDate DATETIME)
RETURNS VARCHAR(10) AS
BEGIN
DECLARE @CleanDate VARCHAR(10)
SELECT @CleanDate =
CASE
WHEN @DirtyDate = '' THEN ''
ELSE CONVERT(VARCHAR(10), @DirtyDate, 101)
END
RETURN @CleanDate
END
这可行,但我想添加错误处理,以防用户在日期时间字段以外的其他内容上使用它。经过一番谷歌搜索后,我发现 UDF 不可能做到这一点。
但是,如果我将其编写为存储过程,我仍然可以在 select 语句中调用它吗?有人能指出我正确的方向吗?
My ERP database uses non-nullable datetime fields. However, it enters '' for the datetime when one isn't available and returns ‘1900-01-01 00: 00: 00.000’ as the value.
I want to suppress the 1900 dates while stripping the Date only from the Datetime field. I created the following UDF to do that:
CREATE FUNCTION ExtractDate(@DirtyDate DATETIME)
RETURNS VARCHAR(10) AS
BEGIN
DECLARE @CleanDate VARCHAR(10)
SELECT @CleanDate =
CASE
WHEN @DirtyDate = '' THEN ''
ELSE CONVERT(VARCHAR(10), @DirtyDate, 101)
END
RETURN @CleanDate
END
This works, but I wanted to add error handling in case a user used it on something other than a datetime field. After some Googling, I found out that this isn't possible with UDF.
However, if I write this as a stored procedure, would I still be able to call it in a select statement? Can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不能在 select 语句中调用存储过程。
我对您想要包含错误处理的雄心表示赞赏,但您最好的选择是在应用程序端检查这一点 - 不允许人们在非日期字段上使用该函数。
No, you can't call a stored proc in a select statement.
I applaud your ambition in wanting to include error handling, but your best bet is to check for that on the app side - don't allow people to use the function on non-date fields.