开发者

Problem with Executing Mysql stored procedure

开发者 https://www.devze.com 2022-12-29 18:44 出处:网络
The stored procedure builds without any problem. The purpose of this is to take backup of selected tables to a script file. when the SP returns a value {Insert statements}.

The stored procedure builds without any problem. The purpose of this is to take backup of selected tables to a script file. when the SP returns a value {Insert statements}.

I am using the below MySql stored procedure, created by SQLWAYS [Tool to convert MsSql to MySql]. The actual MsSql SP is from http://www.codeproject.com/KB/database/InsertGeneratorPack.aspx

When i execute the SP in MySql Query Browser, It says "Unknown column 'tbl_users' in 'field list'"

What would be the problem ? Because there was no error when i build-ed this Converted MySql SP.

Help..

  DELIMITER $$

DROP PROCEDURE IF EXISTS `demo`.`InsertGenerator` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertGenerator`(v_tableName VARCHAR(100))
SWL_return:
BEGIN
-- SQLWAYS_EVAL# to retrieve column specific information

-- SQLWAYS_EVAL# table

   DECLARE v_string NATIONAL VARCHAR(3000); -- SQLWAYS_EVAL# first half 

-- SQLWAYS_EVAL# tement

   DECLARE v_stringData NATIONAL VARCHAR(3000); -- SQLWAYS_EVAL# data 

-- SQLWAYS_EVAL# statement

   DECLARE v_dataType NATIONAL VARCHAR(1000); -- SQLWAYS_EVAL# 

-- SQLWAYS_EVAL# columns

   DECLARE v_colName NATIONAL VARCHAR(50);
   DECLARE NO_DATA INT DEFAULT 0;
   DECLARE cursCol CURSOR  FOR 
   SELECT column_name,data_type FROM `columns` 
   WH开发者_开发问答ERE table_name = v_tableName;
   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
   BEGIN
      SET NO_DATA = -2;
   END;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET NO_DATA = -1;
   OPEN cursCol;
   SET v_string = CONCAT('INSERT ',v_tableName,'(');
   SET v_stringData = '';

   SET NO_DATA = 0;
   FETCH cursCol INTO v_colName,v_dataType;

   IF NO_DATA <> 0 then

      -- NOT SUPPORTED print CONCAT('Table ',@tableName, ' not found, processing skipped.')
close cursCol;

      LEAVE SWL_return;
   end if;

   WHILE NO_DATA = 0 DO
      IF v_dataType in('varchar','char','nchar','nvarchar') then

         SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# ll(',v_colName,'SQLWAYS_EVAL# ''+');
      ELSE
         if v_dataType in('text','ntext') then -- SQLWAYS_EVAL#

-- SQLWAYS_EVAL# else 


            SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# ll(cast(',v_colName,'SQLWAYS_EVAL# 00)),'''')+'''''',''+');
         ELSE
            IF v_dataType = 'money' then -- SQLWAYS_EVAL# doesn't get converted 

-- SQLWAYS_EVAL# implicitly


               SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# y,''''''+
isnull(cast(',v_colName,'SQLWAYS_EVAL# 0)),''0.0000'')+''''''),''+');
            ELSE
               IF v_dataType = 'datetime' then

                  SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# time,''''''+
isnull(cast(',v_colName,
                  'SQLWAYS_EVAL# 0)),''0'')+''''''),''+');
               ELSE
                  IF v_dataType = 'image' then

                     SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# ll(cast(convert(varbinary,',v_colName,
                     'SQLWAYS_EVAL# 6)),''0'')+'''''',''+');
                  ELSE
                     SET v_stringData = CONCAT(v_stringData,'SQLWAYS_EVAL# ll(cast(',v_colName,'SQLWAYS_EVAL# 0)),''0'')+'''''',''+');
                  end if;
               end if;
            end if;
         end if;
      end if;
      SET v_string = CONCAT(v_string,v_colName,',');
      SET NO_DATA = 0;
      FETCH cursCol INTO v_colName,v_dataType;
   END WHILE;
END $$

DELIMITER ;


DELIMITER $$

DROP PROCEDURE IF EXISTS `sample`.`InsGen` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsGen`(
in_db varchar(20),
in_table varchar(20),
in_ColumnName varchar(20),
in_ColumnValue varchar(20)
)
BEGIN

declare Whrs varchar(500);
declare Sels varchar(500);
declare Inserts varchar(200);
declare tablename varchar(20);
declare ColName varchar(20);


set tablename=in_table;


# Comma separated column names - used for Select
select group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')'))
INTO @Sels from information_schema.columns where table_schema=in_db and table_name=tablename;


# Comma separated column names - used for Group By
select group_concat('`',column_name,'`')
INTO @Whrs from information_schema.columns where table_schema=in_db and table_name=tablename;


#Main Select Statement for fetching comma separated table values

 set @Inserts=concat("select concat('insert IGNORE into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');')
 as MyColumn from ", in_db,".",tablename, " where ", in_ColumnName, " = " , in_ColumnValue, " group by ",@Whrs, ";");

 PREPARE Inserts FROM @Inserts;

EXECUTE Inserts;                    

END $$

DELIMITER ;
0

精彩评论

暂无评论...
验证码 换一张
取 消