【问题标题】:Convert SQL Server stored procedure to MySQL将 SQL Server 存储过程转换为 MySQL
【发布时间】:2011-02-15 08:55:24
【问题描述】:

我需要将以下 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

【问题讨论】:

  • 您需要正确格式化您的问题,以便您的所有 SQL 都显示为代码块。
  • @Graham Clark,现在格式化的代码。感谢您的努力。
  • 它的格式仍然不正确。选择整个代码块并单击代码图标。还有你的问题是什么?你能帮我重写这整段代码吗?如果是这样,我怀疑答案是否定的!
  • 这个问题应该以“太宽泛”而结束。

标签: mysql sql-server stored-procedures


【解决方案1】:

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

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

    例如:

    创建过程 Strproc_example( @strBillingPeriodID VarChar(200), @result int OUT)

    在 MySQL 中是

    创建过程 Strproc_example( v_strBillingPeriodID VarChar(200), OUT v_result int )

  2. 在 SQL Server 中有 As 关键字。在 MySQL 中删除它

  3. 然后在SQL Server的in语句后加分号进行转换 进入 MySQL 语句。

【讨论】:

    【解决方案2】:

    http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html 此链接包含有关在 MySql 中创建存储过程和函数的文档。你那里有很多代码,所以它可能会归结为 1 个中的 6 个和其他的半打。

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 2013-10-10
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多