I need to convert the following stored procedure of SQL Server to MySQL. I am new to 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
B开发者_JAVA技巧EGIN
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
http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html This link contains the documentation on creating stored procedures and functions in MySql. You have quite a lot of code there so it may come down to 6 of 1 and half dozen of the other.
You can convert it to MySQL by using the following steps.
In SQL Server the parameters of the stored procedure are declared as
For Example:
CREATE PROCEDURE Strproc_example( @strBillingPeriodID VarChar(200), @result int OUT)
In MySQL it is
CREATE PROCEDURE Strproc_example( v_strBillingPeriodID VarChar(200), OUT v_result int )
In SQL server there is the
As
keyword. Remove that in MySQLThen put a semicolon after in statement of SQL Server to convert it into MySQL statements.
精彩评论