解决MySQL存储过程中的数组问题

时间:2018-05-19 07:59:40

标签: mysql stored-procedures

我正在尝试将字符串数组(以逗号分隔的字符串)传递给存储过程。我的要求是迭代,读取和读取。最后插入String array ..

中的值

我试过这个,如下,

exec sp_executesql N'SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Alarm] AS [Extent1]
        WHERE ([Extent1].[AssetID] = @p__linq__0) AND ([Extent1].[EventCode] IN (1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166)) AND ([Extent1].[EventCode] IS NOT NULL) AND (((0 = [Extent1].[EventType]) AND (0 = [Extent1].[AlarmStatus])) OR ((0 <> [Extent1].[EventType]) AND (6 <> [Extent1].[AlarmStatus]))) AND (((0 = [Extent1].[EventType]) AND (0 = [Extent1].[AlarmStatus])) OR ((1 = [Extent1].[EventType]) AND ([Extent1].[AcknowledgeDate] IS NULL))) AND (11 <> [Extent1].[AlarmStatus]) AND (12 <> [Extent1].[AlarmStatus]) AND ([Extent1].[SourceID] = @p__linq__1) AND (([Extent1].[SiteID] = @p__linq__2) OR (([Extent1].[SiteID] IS NULL) AND (@p__linq__2 IS NULL)))
    )  AS [GroupBy1]',N'@p__linq__0 uniqueidentifier,@p__linq__1 bigint,@p__linq__2 uniqueidentifier',@p__linq__0='B0524A60-F980-4B82-A799-E788A9A4D04B',@p__linq__1=594520350,@p__linq__2='B02A51FE-2248-E611-A64E-782BCB72ACED'

例如: 输入:

DELIMITER $$
CREATE PROCEDURE SP_test(IN ArrayString VARCHAR(100))
BEGIN
SET ArrayIndex = 0;

loop: LOOP    
  IF (ArrayIndex < ArrayString.length) THEN
    INSERT INTO temp SELECT parseString(ArrayString,ArrayIndex);     
  ELSE 
    LEAVE loop;  
  END IF; 
SET ArrayIndex = ArrayIndex + 1;
END LOOP loop;

END$$
DELIMITER ;

SET ArrayString = 'one,two,three,four'; 应返回值4&amp;循环应迭代4次

第一次迭代:

ArrayString.length

应该返回值&#39;一个&#39; &安培;插入

第二次迭代:

ArrayIndex = 0;
parseString(ArrayString,ArrayIndex)

应该返回值&#39;两个&#39; &安培;插入

第3次迭代:

ArrayIndex = 1;
parseString(ArrayString,ArrayIndex)

应该返回值&#39;三&#39; &安培;插入

第4次迭代:

ArrayIndex = 3;
parseString(ArrayString,ArrayIndex)

应返回值&#39;四&#39; &安培;插入

如何编写ArrayString.length和amp;的代码parseString(ArrayString,ArrayIndex)?

请帮助......

0 个答案:

没有答案