msSQL Server 數據庫教程存儲過程轉換成MySQL教程數據庫
DELIMITER $$
DROP PROCEDURE IF EXISTS ChangeSequence$$
CREATE PROCEDURE ChangeSequence
(
_sign INT ,-- 0: 上移 1:下移
TableName VARCHAR(50) ,-- 表名
ItemName VARCHAR(50) ,-- 主鍵字段名
ItemID INT , -- 主鍵ID
SortName VARCHAR(50) , -- 排序ID
TypeName VARCHAR(50) , -- 分類字段名
TypeValue VARCHAR(50) -- 分類值
)
BEGIN
DECLARE _SQL VARCHAR(4000);
DECLARE ThisSort INT;
DECLARE PREVID INT;
DECLARE NextID INT;
DECLARE _Count INT;CREATE TEMPORARY TABLE _Tab
(
_ItemID INT,
_Sort INT
);
SET _SQL=CONCAT('INSERT INTO _Tab (_ItemID,_Sort) SELECT ' ,ItemName,SortName,' FROM ',TableName) ;IF (TypeName<>'' AND TypeValue<>'') THEN
SET _SQL=CONCAT(_SQL,' where ',TypeName,'=',TypeValue);
END IF;
SET _SQL = CONCAT(_SQL,' ORDER BY _Sort ASC ') ;
SET @v_sql=_SQL;
PREPARE stmt FROM @v_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET _SQL = '';
SELECT COUNT(*) INTO _Count FROM _Tab ;
SELECT Sort INTO ThisSort FROM _Tab WHERE _ItemID = ItemID;
IF(ThisSort>1) THEN
SELECT _ItemID INTO PREVID FROM _Tab WHERE _Sort=(ThisSort-1);
END IF;
IF(ThisSort<_Count) THEN SELECT _ItemID INTO NextID FROM _Tab WHERE _Sort=(ThisSort+1);END IF;
IF(_Sign=0) THEN
IF(ThisSort>1) THEN
SET _SQL = CONCAT('UPDATE ',TableName,' SET ',SortName,'=','CONVERT((',ThisSort,'-1),varchar(100))',' WHERE ',ItemName,' = ','CONVERT(',ItemID,',varchar(100))' , ';' ,
'UPDATE ',TableName,' SET ',SortName,'=','CONVERT(',ThisSort,',varchar(100))',' WHERE ',ItemName,' = ','CONVERT(',PREVID,',varchar(100))');
END IF;
ELSE
IF(ThisSort<_Count) THEN
SET _SQL =CONCAT( 'UPDATE ',TableName,' SET ',SortName,'=','CONVERT((',ThisSort,'+1),varchar(100))',' WHERE ',ItemName,' = ','CONVERT(',ItemID,',varchar(100))',';',
'UPDATE ',TableName,' SET ',SortName,'=','CONVERT(',ThisSort,',varchar(100))',' WHERE ',ItemName,' = ','CONVERT(',NextID,',varchar(100))');
END IF;
END IF;
SET @v_sql1=_SQL;
PREPARE stmt1 FROM @v_sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$