0

谁能帮我把下面的 MS-SQL sp 写到 MySQL sp,

使用这个:

CREATE PROCEDURE sp_InputWork 
    @_DelimitedString nvarchar(MAX) 
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @_DataRow nvarchar(MAX)
    DECLARE @_DescriptionOfWorkDone nvarchar(MAX)
    DECLARE @_TemporaryStorage nvarchar(MAX)
    DECLARE @_QTY int
    DECLARE @_Total int
    DECLARE @_CurrentField int

    WHILE CHARINDEX(';', @_DelimitedString) > 0
    BEGIN
        SET @_DataRow = CAST(SUBSTRING(@_DelimitedString, 0, CHARINDEX(';', @_DelimitedString)) AS nvarchar(MAX))
        SET @_CurrentField = 1
        WHILE CHARINDEX(',', @_DataRow) > 0
            BEGIN
            SET @_TemporaryStorage = CAST(SUBSTRING(@_DataRow, 0, CHARINDEX(',', @_DataRow)) AS nvarchar(MAX))
            IF @_CurrentField = 1
                SET @_QTY = CAST(@_TemporaryStorage AS int)
                    IF @_CurrentField = 2
            SET @_DescriptionOfWorkDone = @_TemporaryStorage
                    IF @_CurrentField = 3
                    SET @_Total = CAST(@_TemporaryStorage AS int)           
            SET @_DataRow = SUBSTRING(@_DataRow, CHARINDEX(',', @_DataRow) + 1, LEN(@_DataRow))
            SET @_CurrentField = @_CurrentField + 1
        END
        INSERT INTO tblWorkDone (QTY, DescriptionOfWorkDone, Total) VALUES (@_QTY, @_DescriptionOfWorkDone, @_Total)
        SET @_DelimitedString = SUBSTRING(@_DelimitedString, CHARINDEX(';', @_DelimitedString) + 1, LEN(@_DelimitedString))
        END 
END 
4

1 回答 1

0

我已将 SP 更改为 Mysql

BEGIN
DECLARE _DataRow VARCHAR(21000);
 DECLARE _DescriptionOfWorkDone VARCHAR(21000);
DECLARE _TemporaryStorage VARCHAR(21000) ;
DECLARE _QTY int;
DECLARE _Total int;
DECLARE _CurrentField int;

WHILE INSTR(_DelimitedString, ';') >0 DO

    SET _DataRow = CAST(SUBSTRING(_DelimitedString, 1, INSTR(_DelimitedString,';' )) AS CHAR);
    SET _CurrentField = 1;
    WHILE INSTR(_DataRow,',' ) > 0 DO         
        SET _TemporaryStorage = CAST(SUBSTRING(_DataRow, 1, INSTR(_DataRow,',' )-1) AS CHAR);
        IF _CurrentField = 1 then
            SET _QTY = CAST(_TemporaryStorage AS UNSIGNED);
                end if;
                      IF _CurrentField = 2 then
        SET _DescriptionOfWorkDone = _TemporaryStorage;
                end if;
                      IF _CurrentField = 3 then
                SET _Total = CAST(_TemporaryStorage AS UNSIGNED)    ;   
                     end if;    

        SET _DataRow = SUBSTRING(_DataRow, INSTR( _DataRow,',') + 1, LENGTH(_DataRow));
        SET _CurrentField = _CurrentField + 1;
         END while;

    INSERT INTO tblWorkDone (QTY, DescriptionOfWorkDone, Total) VALUES (_QTY, _DescriptionOfWorkDone, _Total);
    SET _DelimitedString = SUBSTRING(_DelimitedString, INSTR(_DelimitedString,';' ) + 1, LENGTH(_DelimitedString));

   END WHILE;END

插入的数据

CALL `sp_InputWork`('1,TEST,100,;2,TEST1,200,;3,TEST3,300,;')

成功循环插入Mysql Table

于 2013-06-13T10:10:17.057 回答