我有SplitDictionary
功能:
ALTER FUNCTION [dbo].[SplitDictionary]
(
@RowKey NVARCHAR(MAX),
@RowData NVARCHAR(MAX),
@Delimeter NVARCHAR(2)
)
RETURNS @RtnValue TABLE
(
ID INT IDENTITY(1,1),
keyValue nvarchar(max),
Data NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @Iterator INT
SET @Iterator = 1
DECLARE @FoundIndex INT
SET @FoundIndex = CHARINDEX(@Delimeter,@RowData)
declare @keyFoundIndex int
set @keyFoundIndex=charindex(@Delimeter, @RowKey)
WHILE (@FoundIndex>0)
BEGIN
INSERT INTO @RtnValue (keyValue, data)
SELECT KeyValue=LTRIM(RTRIM(SUBSTRING(@RowKey, 1, @FoundIndex - 1))),
Data = LTRIM(RTRIM(SUBSTRING(@RowData, 1, @FoundIndex - 1)))
SET @RowKey=substring(@RowKey, @FoundIndex + datalength(@Delimeter)/2, len(@RowKey))
SET @RowData = SUBSTRING(@RowData,@FoundIndex + DATALENGTH(@Delimeter) / 2,LEN(@RowData))
SET @Iterator = @Iterator + 1
SET @FoundIndex = CHARINDEX(@Delimeter, @RowData)
END
INSERT INTO @RtnValue (keyValue, Data)
SELECT keyValue=ltrim(rtrim(@RowKey)), Data = LTRIM(RTRIM(@RowData))
RETURN
END
当我使用此功能时:
SELECT ID, keyValue, Data from dbo.SplitDictionary('10,1','20,30', ',')
结果是:
1 10 20
2 1 30
这是真的
当我使用此功能时:
SELECT ID, keyValue, Data from dbo.SplitDictionary('1,1','20,30', ',')
结果是:
1 1, 20
2 空 30
这是假的
如何修复存储过程中的第二次调用?