简体   繁体   English

基本SQL UDF创建

[英]Basic SQL UDF creation

After reading far too much on the subject, I'm clueless how to write a UDF, and where to save it. 在阅读了过多的主题之后,我不知道如何编写UDF以及将其保存在何处。

Instead of writing the same nested REPLACE() numerous times in several other scripts, I would like to be able to call it as I need, something like: 与其在多个其他脚本中多次编写同一个嵌套的REPLACE(),不如希望我可以调用它,例如:

Targ.Name =  dbo.fn_add_sym( isnull(Targ.Name,  Src.Name) )

I have the following function, but I haven't been able to test it, since I don't know where to put it. 我具有以下功能,但由于不知道放置位置,因此无法对其进行测试。

CREATE FUNCTION dbo.fn_add_sym( @string NVARCHAR(max) )
RETURNS NVARCHAR(max)
WITH SCHEMABINDING
AS
begin
return 
  @string = REPLACE(
              REPLACE(
                REPLACE(
                  REPLACE(
                    REPLACE(
                      REPLACE(@string, N'%2b', N'+')
                    , N'%2d', N'-')
                  , N'%3d', N'=')
                , N'%22', N'"')
              , N'%5f', N'_')
            ,'"', N'"')

end

Here is a SQL Fiddle that shows the function. 是显示该功能的SQL Fiddle。

The following is some code to test it: 以下是一些测试代码:

select dbo.fn_add_sym('abc%5fdef')

The only syntax error you had was return @string = . 您遇到的唯一语法错误是return @string = The @string = is unnecessarily. @string =不必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM