用于解析字符串的存储过程

时间:2012-04-05 18:52:23

标签: sql-server sql-server-2008 sql-server-2005 stored-procedures

我需要编写一个输入为字符串的存储过程。

输入字符串包含变量名称,它们的值由管道分隔符分隔,如下所示:

Name =Praveen | City=Hyderabad | Mobile=48629387429| Role=User| etc

在存储过程中,我声明了@x, @y, @z, @t之类的变量来获取值

@x=Praveen (Name value)
@y=Hyderabad (City Value)
@z=48629387429(Mobile Value)
@t=User(Role Value)

此外,输入字符串可以按任何顺序显示值,如

City=Hyderabad | Mobile=48629387429 | Role=User | Name =Praveen |etc

一旦我将值解析为@x, @y, @z, @t等,我必须在存储过程中使用这些值。

请告诉我如何解析输入字符串以分别获取Name, City, Mobile, Role@x, @y, @z@t的值。

4 个答案:

答案 0 :(得分:4)

一种可能的解决方案是使用XML

DECLARE @text VARCHAR(1000) 
        ,@xml xml

SELECT @text = 'City=Hyderabad | Mobile=48629387429 | Role=User | Name =Praveen'

SELECT @text = REPLACE(@text,'|','"')
    ,@text = REPLACE(@text,'=','="')
    ,@text = '<row ' + @text + '"/>'

SELECT @xml = CAST(@text AS XML)

select 
    line.col.value('@Name[1]', 'varchar(100)') AS Name
    ,line.col.value('@City[1]', 'varchar(100)') AS City
    ,line.col.value('@Mobile[1]', 'varchar(100)') AS Mobile 
    ,line.col.value('@Role[1]', 'varchar(100)') AS Role 
FROM @xml.nodes('/row') AS line(col)

答案 1 :(得分:0)

假设你的输入参数叫做@Text。

DECLARE @Text varchar(255),
    @x varchar(255)

SET @Text = 'Name=Praveen | City=Hyderabad | Mobile=48629387429| Role=User'

-- Added to show how to account for non-trailing |
SET @Text = @Text + ' | ';

SET @x = LTRIM(RTRIM(substring(
         @Text,
         charindex('Name=', @Text) + LEN('Name='),
         charindex(' | ', @Text, charindex('Name=', @Text)) - LEN('Name=')
         )))

SELECT @x

然后只需重复一次@y,@ z,@ t将Name =改为你的休息时间。

答案 2 :(得分:0)

我绝对建议在程序端进行字符串解析而不是数据端。话虽这么说,如果你绝对必须尝试做类似的事情:

DECLARE @String [nvarchar](256) = 'Name=Praveen | City=Hyderabad | Mobile=48629387429 | Role=User |'

DECLARE @name [nvarchar](256) = (SELECT SUBSTRING(@String, CHARINDEX('Name=', @String)+5, CHARINDEX('|', @String)))

DECLARE @city [nvarchar](256) = (SELECT SUBSTRING(@String, CHARINDEX('City=', @String)+5, CHARINDEX('|', @String)))

DECLARE @mobile [nvarchar](256) = (SELECT SUBSTRING(@String, CHARINDEX('Mobile=', @String)+7, CHARINDEX('|', @String)))

DECLARE @role [nvarchar](256) = (SELECT SUBSTRING(@String, CHARINDEX('Role=', @String)+5, CHARINDEX('|', @String)))

SELECT RTRIM(LTRIM(LEFT(@name, CHARINDEX('|', @name)-1))) AS Name,
        RTRIM(LTRIM(LEFT(@city, CHARINDEX('|', @city)-1))) AS City,
        RTRIM(LTRIM(LEFT(@mobile, CHARINDEX('|', @mobile)-1))) AS Mobile,
        RTRIM(LTRIM(LEFT(@role, CHARINDEX('|', @role)-1))) AS Role

返回:

 Name    | City      | Mobile      | Role
________________________________________________
 Praveen | Hyderabad | 48629387429 | User

请注意,初始查询中CHARINDEX添加的长度等于搜索字符串。

“Name =”等于5个字符,所以我们添加5来将索引移过=符号, “Mobile =”等于7,所以我们加7。

同样在最后SELECT查询中,我们从每个CHARINDEX中减去1以删除|符号

来源:

SUBSTRING

CHARINDEX

LEFT

LTRIM

RTRIM

答案 3 :(得分:0)

这是一种使用循环进行字符串操作的有趣方式。请注意我们如何定义@x,@ y等变量以获取特定值。

-- Simulate proc parameter
declare @input nvarchar(max) = 'Name =Praveen | City=Hyderabad | Mobile=48629387429| Role=User'

-- OP's preferred destination vars
declare @x nvarchar(max) = 'Name'
declare @y nvarchar(max) = 'City'
declare @z nvarchar(max) = 'Mobile'
declare @t nvarchar(max) = 'Role'

-- The key/value delimiters we are expecting
declare @recordDelim nchar(1) = '|'
declare @valueDelim nchar(1) = '='

-- Temp storage
declare @inputTable table (
      name nvarchar(128) not null primary key
    , value nvarchar(max) null
)

-- Get all key/value pairs
while ltrim(rtrim(@input)) != '' begin
    insert into @inputTable (name) select ltrim(rtrim(replace(left(@input, isnull(nullif(charindex(@recordDelim, @input), 0), len(@input))), @recordDelim, '')))
    select @input = ltrim(rtrim(right(@input, len(@input) - isnull(nullif(charindex(@recordDelim, @input), 0), len(@input)))))
end

-- Separate keys and values
update @inputTable
set name = ltrim(rtrim(left(name, isnull(nullif(charindex(@valueDelim, name) - 1, 0), len(name)))))
    , value = ltrim(rtrim(right(name, len(name) - isnull(nullif(charindex(@valueDelim, name), 0), len(name)))))

-- Populate the variables
-- If any are null, then this key/value wasn't present
set @x = (select value from @inputTable where name = @x)
set @y = (select value from @inputTable where name = @y)
set @z = (select value from @inputTable where name = @z)
set @t = (select value from @inputTable where name = @t)

另外,从您输入的不规则间距来看,我猜你想要修剪所有内容(这就是为什么这个proc会在整个地方做到这一点)。