2013-06-12 404 views
0

MS-SQL任何人可以幫我寫了下面的MS-SQL SP到MySQL SP,存儲過程到MySQL

使用這樣的:

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 
+3

你得到什麼錯誤,你有什麼嘗試? –

+0

@RaviSingh我正在做的是將一個字符串傳遞給SP(a,test,100.00,1; b,test1,150.00,c;),即由昏迷分隔的值和由分號分隔的值設置,我想將SP循環通過值集,然後通過字段然後插入到表中。我沒有在Mysql中試過這個。我從一個網站獲得這個sp [link] http://www.vbforums.com/showthread.php?516496-Inserting-datagrid-view-data-to-sql-database [/ link] –

+0

這應該在mysql中工作更改:1)將'charindex'更改爲'instr()'。 2)使用「IF..THEN」而不是「IF」。 –

回答

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表

+0

對不起,如果我讀錯了,但這不是一個解決方案嗎? (假設你不認爲「所有字段爲空」是正確的)。如果是這樣:請不要將它作爲回答發佈,但更新您的問題:這不是你典型的論壇,這是一個問答網站,'你的回答'框是真正的答案:) – Nanne

+0

@Nanne最後我得到了它的工作和發佈正確的答案,我的道歉更新我的問題作爲答案 –