Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在SQL中创建一个函数,但是我遇到了很多错误_Sql_Sql Server - Fatal编程技术网

我想在SQL中创建一个函数,但是我遇到了很多错误

我想在SQL中创建一个函数,但是我遇到了很多错误,sql,sql-server,Sql,Sql Server,我想在Management Studio中的SQL Server中为以下对象创建函数: Input: 7589586586 Output: (758) 958-6586 Input: 758ABC6586 Output: (758) 222-6586 Input: 758ABC65 Output: Invalid Formats (like mobile keypad) 这是我的SQL代码,其中有很多错误: CREATE FUNCTION fn_bhagyas

我想在Management Studio中的SQL Server中为以下对象创建函数:

Input: 7589586586     Output:  (758) 958-6586
Input: 758ABC6586     Output: (758) 222-6586  
Input: 758ABC65       Output: Invalid Formats (like mobile keypad)
这是我的SQL代码,其中有很多错误:

CREATE FUNCTION fn_bhagyashreed_phonenumber
    (@input VARCHAR(20))
RETURNS VARCHAR(50)
BEGIN
    DECLARE @compare VARCHAR(30) = '';            
    DECLARE @cnt INT = 1;
    DECLARE @varout VARCHAR(30) = '';
    DECLARE @val VARCHAR(30) = '';
    DECLARE @Phoutput VARCHAR(50) = '';
    DECLARE @var INT;

    SET @var = LEN(@input);

    IF @var <> 10 OR @input NOT REGEXP '^[[:alnum:]]+$' THEN
        SET @Phoutput = 'Invalid Format';
    ELSE
        WHILE @cnt <= 10 
        BEGIN
            SET @compare = SUBSTRING(@input, @cnt, 1);

            IF @compare IN('a','b','c','2') 
            BEGIN
                SET @val=2;
            ELSE IF @compare IN('d', 'e', 'f', '3') 
            BEGIN
                SET @val=3;
            ELSE IF @compare IN('g', 'h', 'i', '4') 
            BEGIN
                SET @val = 4;
            ELSE IF @compare IN('j', 'k', 'l', '5') 
            BEGIN
                SET @val = 5;
            ELSE IF @compare IN('m', 'n', 'o', '6') 
            BEGIN
                SET @val = 6;
            ELSE IF @compare IN('p', 'q', 'r', 's', '7') 
            BEGIN
                SET @val = 7;
            ELSE IF @compare IN('t', 'u', 'v', '8') 
            BEGIN
                SET @val = 8;
            ELSE IF @compare IN('w', 'x', 'y', 'z', '9') 
            BEGIN
                SET @val = 9;
            ELSE IF @compare = '1' 
            BEGIN
                SET @val = 1;
            ELSE IF @compare = '0' 
            BEGIN
                SET @val = 0;
        END 

        SET @varout = CONCAT(@varout,@val);
        SET @cnt = @cnt + 1;
    END;

    SET @Phoutput = CONCAT('(',SUBSTRING(@varout,1,3),')',' ',SUBSTRING(@varout,4,3),'-',SUBSTRING(@varout,7,4));
END; IF;
RETURN Phoutput;
END$$
以下是错误:

Msg 102,15级,状态1,程序fn_bhagyashreed_电话号码,第13行 “REGEXP”附近的语法不正确

Msg 156,15级,状态1,程序fn_bhagyashreed_电话号码,第15行 关键字“ELSE”附近的语法不正确

Msg 156,15级,状态1,程序fn_bhagyashreed_电话号码,第20行 关键字“ELSE”附近的语法不正确。


这里面有多个语法错误,我不单独讨论它们

每个IF必须有一个开始和结束

见示例D


END$$应该是END

您的整个方法与sql思维相去甚远。您可以按过程执行此操作,但SQL(如果可能的话)最好使用基于集合的方法

试试这个完全即席的SQL

结果

ID  PhoneNumber     Validated
1   (758) 958-6586  (758) 958-6586
2   (758) 222-6586  (758) 222-6586
3   (758) 222-65XX  Invalid Format
简短解释 首先,我创建一个表变量来模拟测试场景。第一个CTE tenDigits将创建一个从1到10的派生表。第二个CTE将一个接一个地返回带有电话号码数字的派生表,并在同一个go中进行必要的替换。第三个CTE将把单个数字重新连接到一段文本。材料用于将和-推到正确的位置。最后,“X”的存在表示一个无效数字

您可以将第一个CTE放大到10位以上,如果Nr超过10,则添加和X,以消除与长数字相关的错误。

这应该有效

CREATE FUNCTION fn_bhagyashreed_phonenumber(@input VARCHAR(20))
RETURNS VARCHAR(50)
  BEGIN
    DECLARE @compare VARCHAR(30) = ''
    DECLARE @cnt INT = 1
    DECLARE @varout VARCHAR(30) = ''
    DECLARE @val VARCHAR(30) = ''
    DECLARE @Phoutput VARCHAR(50) = ''
    DECLARE @var INT

    SET @var = LEN(@input)

    IF @var <> 10
        OR @input LIKE '%[^a-z0-9-'']%'
      SET @Phoutput = 'Invalid Format'
    ELSE
      BEGIN
        WHILE @cnt <= 10
          BEGIN
            SET @compare = SUBSTRING(@input, @cnt, 1)
            SET @val=CASE
                       WHEN @compare IN ('a', 'b', 'c', '2') THEN 2
                       WHEN @compare IN ('d', 'e', 'f', '3') THEN 3
                       WHEN @compare IN ('g', 'h', 'i', '4') THEN 4
                       WHEN @compare IN ('j', 'k', 'l', '5') THEN 5
                       WHEN @compare IN ('m', 'n', 'o', '6') THEN 6
                       WHEN @compare IN ('p', 'q', 'r', 's', '7') THEN 7
                       WHEN @compare IN ('t', 'u', 'v', '8') THEN 8
                       WHEN @compare IN ('w', 'x', 'y', 'z', '9') THEN 9
                       WHEN @compare IN ('1') THEN 1
                       WHEN @compare IN ('0') THEN 0
                       ELSE 0
                     END
            SET @varout = CONCAT(@varout, @val)
            SET @cnt = @cnt + 1
          END
      END

    IF @Phoutput <> 'Invalid Format'
      BEGIN
        SET @Phoutput = CONCAT('(', SUBSTRING(@varout, 1, 3), ')', ' ', SUBSTRING(@varout, 4, 3), '-', SUBSTRING(@varout, 7, 4))
      END

    RETURN @Phoutput
  END 

我只是想与众不同。 也可以尝试使用其他示例数据

DECLARE @tbl TABLE(ID INT IDENTITY,phoneString VARCHAR(100));
INSERT INTO @tbl VALUES('7589586586'),('758ABC6586'),('758ABC65')

;With CTE as
(
select id,replace(phoneString,char(97),'2') phoneString,97 rn 
from @tbl where phoneString like '%[Aa-Zz]%' and len(phoneString)=10

union all

select id, replace(phoneString,char(rn)
,case when rn BETWEEN 97 and 99 then '2' 
when rn BETWEEN 100 and 102 then '3' 
when rn BETWEEN 103 and 105 then '4' 
when rn BETWEEN 106 and 108 then '5' 
when rn BETWEEN 109 and 111 then '6' 
when rn BETWEEN 112 and 114 then '7' 
when rn BETWEEN 115 and 117 then '8' 
when rn BETWEEN 119 and 122 then '9' 
else 'X' end),rn+1 from cte c
where rn<122 and phoneString like '%[Aa-Zz]%'

)
,CTE1 as
(
select  *,ROW_NUMBER()over( partition by id order by rn desc) rn1 
from cte    
)

select id,phonestring,'('+SUBSTRING(phonestring,1,3)+' ) '+ 
SUBSTRING(phonestring,4,3)+'-'+SUBSTRING(phonestring,8,3)
 Validated from cte1
where rn1=1
union ALL
select id,phonestring,'('+SUBSTRING(phonestring,1,3)+' ) '+ 
SUBSTRING(phonestring,4,3)+'-'+SUBSTRING(phonestring,8,3)
 Validated from @tbl
where isnumeric(phoneString)=1 and len(phoneString)=10
union ALL
select id,phonestring,'invalid format' from @tbl 
where len(phonestring)<10

本地化电话号码输出应该留给GUI层。您可以使用类似的示例,而不是使用正则表达式:IF@var 10或@input不象“^[[:alnum:]+$”那么。谢谢。这很有帮助。但是concat呢?我们可以在sql中使用它吗?选择@@version?请看一看仍然出现这些错误的示例:Msg 102,级别15,状态1,过程fn_phonenumber,第13行“REGEXP”附近的语法不正确。Msg 156,第15级,状态1,程序fn_phonenumber,第17行关键字“ELSE”附近语法不正确。Msg 156,15级,状态1,程序fn_phonenumber,第42行关键字“SET”附近语法不正确。Msg 195,级别15,状态10,程序fn_phonenumber,第42行“CONCAT”不是可识别的内置函数名。Msg 102,15级,状态1,程序fn_phonenumber,第44行“;”附近语法不正确。最多有4个错误。请提供帮助:Msg 156,级别15,状态1,过程fn_phonenumber,第13行关键字“THEN”附近语法不正确。Msg 156,15级,状态1,程序fn_phonenumber,第16行关键字“ELSE”附近语法不正确。Msg 195,级别15,状态10,程序fn_phonenumber,第42行“CONCAT”不是可识别的内置函数名。Msg 195,级别15,状态10,程序fn_phonenumber,第45行'CONCAT'不是公认的内置函数名。方法不错。非常感谢,先生。
CREATE FUNCTION fn_bhagyashreed_phonenumber(@input VARCHAR(20))
RETURNS VARCHAR(50)
  BEGIN
    DECLARE @compare VARCHAR(30) = ''
    DECLARE @cnt INT = 1
    DECLARE @varout VARCHAR(30) = ''
    DECLARE @val VARCHAR(30) = ''
    DECLARE @Phoutput VARCHAR(50) = ''
    DECLARE @var INT

    SET @var = LEN(@input)

    IF @var <> 10
        OR @input LIKE '%[^a-z0-9-'']%'
      SET @Phoutput = 'Invalid Format'
    ELSE
      BEGIN
        WHILE @cnt <= 10
          BEGIN
            SET @compare = SUBSTRING(@input, @cnt, 1)
            SET @val=CASE
                       WHEN @compare IN ('a', 'b', 'c', '2') THEN 2
                       WHEN @compare IN ('d', 'e', 'f', '3') THEN 3
                       WHEN @compare IN ('g', 'h', 'i', '4') THEN 4
                       WHEN @compare IN ('j', 'k', 'l', '5') THEN 5
                       WHEN @compare IN ('m', 'n', 'o', '6') THEN 6
                       WHEN @compare IN ('p', 'q', 'r', 's', '7') THEN 7
                       WHEN @compare IN ('t', 'u', 'v', '8') THEN 8
                       WHEN @compare IN ('w', 'x', 'y', 'z', '9') THEN 9
                       WHEN @compare IN ('1') THEN 1
                       WHEN @compare IN ('0') THEN 0
                       ELSE 0
                     END
            SET @varout = CONCAT(@varout, @val)
            SET @cnt = @cnt + 1
          END
      END

    IF @Phoutput <> 'Invalid Format'
      BEGIN
        SET @Phoutput = CONCAT('(', SUBSTRING(@varout, 1, 3), ')', ' ', SUBSTRING(@varout, 4, 3), '-', SUBSTRING(@varout, 7, 4))
      END

    RETURN @Phoutput
  END 
DECLARE @tbl TABLE(ID INT IDENTITY,phoneString VARCHAR(100));
INSERT INTO @tbl VALUES('7589586586'),('758ABC6586'),('758ABC65')

;With CTE as
(
select id,replace(phoneString,char(97),'2') phoneString,97 rn 
from @tbl where phoneString like '%[Aa-Zz]%' and len(phoneString)=10

union all

select id, replace(phoneString,char(rn)
,case when rn BETWEEN 97 and 99 then '2' 
when rn BETWEEN 100 and 102 then '3' 
when rn BETWEEN 103 and 105 then '4' 
when rn BETWEEN 106 and 108 then '5' 
when rn BETWEEN 109 and 111 then '6' 
when rn BETWEEN 112 and 114 then '7' 
when rn BETWEEN 115 and 117 then '8' 
when rn BETWEEN 119 and 122 then '9' 
else 'X' end),rn+1 from cte c
where rn<122 and phoneString like '%[Aa-Zz]%'

)
,CTE1 as
(
select  *,ROW_NUMBER()over( partition by id order by rn desc) rn1 
from cte    
)

select id,phonestring,'('+SUBSTRING(phonestring,1,3)+' ) '+ 
SUBSTRING(phonestring,4,3)+'-'+SUBSTRING(phonestring,8,3)
 Validated from cte1
where rn1=1
union ALL
select id,phonestring,'('+SUBSTRING(phonestring,1,3)+' ) '+ 
SUBSTRING(phonestring,4,3)+'-'+SUBSTRING(phonestring,8,3)
 Validated from @tbl
where isnumeric(phoneString)=1 and len(phoneString)=10
union ALL
select id,phonestring,'invalid format' from @tbl 
where len(phonestring)<10