将SQL Server存储过程转换为MySQL

时间:2010-05-04 08:38:32

标签: mysql sql-server stored-procedures

我需要将以下SQL Server存储过程转换为MySQL。我是MySQL的新手。

    CREATE PROC InsertGenerator
(@tableName varchar(100)) as
--Declare a cursor to retrieve column specific information 
--for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR 
SELECT column_name,data_type FROM information_schema.columns 
WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(3000) 

--for storing the first half 
--of INSERT statement

DECLARE @stringData nvarchar(3000) 

--for storing the data 
--(VALUES) related statement

DECLARE @dataType nvarchar(1000) --data types returned 

--for respective columns

SET @string='INSERT '+@tableName+'('
SET @stringData=''

DECLARE @colName nvarchar(50)

FETCH NEXT FROM cursCol INTO @colName,@dataType

IF @@fetch_status<>0
    begin
    print 'Table '+@tableName+' not found, processing skipped.'
    close curscol
    deallocate curscol
    return
END
WHILE @@FETCH_STATUS=0
BEGIN
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
    SET @stringData=@stringData+'''''''''+
            isnull('+@colName+','''')+'''''',''+'
END
ELSE

if @dataType in ('text','ntext')

 --if the datatype 
 --is text or something else 

BEGIN
    SET @stringData=@stringData+'''''''''+
          isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
END

ELSE
IF @dataType = 'money' --because money doesn't get converted 

--from varchar implicitly

BEGIN

    SET @stringData=@stringData+'''convert(money,''''''+
        isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'

END
ELSE 
IF @dataType='datetime'
BEGIN
    SET @stringData=@stringData+'''convert(datetime,''''''+
        isnull(cast('+@colName+' as varchar(200)),''0'')+''''''),''+'
END
ELSE 
IF @dataType='image' 
BEGIN

    SET @stringData=@stringData+'''''''''+
       isnull(cast(convert(varbinary,'+@colName+') 
       as varchar(6)),''0'')+'''''',''+'
END
ELSE 

--presuming the data type is int,bit,numeric,decimal 

BEGIN

    SET @stringData=@stringData+'''''''''+
          isnull(cast('+@colName+' as varchar(200)),''0'')+'''''',''+'
END
SET @string=@string+@colName+','
FETCH NEXT FROM cursCol INTO @colName,@dataType
END

2 个答案:

答案 0 :(得分:2)

http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html 此链接包含有关在MySql中创建存储过程和函数的文档。你有相当多的代码,所以它可能会降到1和6的另外6个。

答案 1 :(得分:2)

您可以使用以下步骤将其转换为MySQL。

  1. 在SQL Server中,存储过程的参数声明为

    例如:

    CREATE PROCEDURE Strproc_example(     @strBillingPeriodID VarChar(200),     @result int OUT)

    在MySQL中它是

    CREATE PROCEDURE Strproc_example(     v_strBillingPeriodID VarChar(200),         OUT v_result int)

  2. 在SQL Server中有As个关键字。在MySQL

  3. 中删除它
  4. 然后在SQL Server语句后面加一个分号进行转换 进入MySQL语句。