赞
踩
创建表:
DROP TABLE IF EXISTS `bad_position`;
CREATE TABLE `bad_position` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '广告位ID',
`name` varchar(30) NOT NULL COMMENT '广告位名称',
`width` varchar(255) NOT NULL COMMENT '广告位宽度,px或者%',
`height` varchar(255) NOT NULL COMMENT '广告位高度,px或者%',
`fashion` tinyint(1) NOT NULL COMMENT '1:轮显;2:随即',
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1:开启; 0: 关闭',
`_hash` int(11) unsigned NOT NULL COMMENT '预留散列字段',
PRIMARY KEY (`id`,`_hash`),
KEY `name` (`name`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='广告位记录表';
创建存储过程:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `test`.`p_bad_position`( IN id1 INT,
IN name1 VARCHAR (30),
IN width1 VARCHAR (255),
IN height1 VARCHAR (255),
IN fashion1 TINYINT (1),
IN status1 TINYINT (1),
IN _hash1 INT /*需要输入的参数*/
)
BEGIN
/*declare声明和表字段相对应的变量*/
DECLARE id2 INT;
DECLARE name2 VARCHAR (30);
DECLARE width2 VARCHAR (255);
DECLARE height2 VARCHAR (255);
DECLARE fashion2 TINYINT (1);
DECLARE status2 TINYINT (1);
DECLARE _hash2 INT;
IF (id1 != id) THEN /*如果输入的id和表中的id 不相等执行插入操作,否则执行更新操作*/
INSERT INTO `bad_position` (`name`,`width`,`height`,`fashion`,`status`,`_hash`) VALUES(name1,width1,height1,fashion1,status1,_hash1) ;
ELSE
SELECT `id`,`name`,`width`,`height`,`fashion`,`status`,`_hash` INTO id2,name2,width2,height2,fashion2,status2,_hash2 FROM `bad_position` ;/*将表中的数据和声明的变量绑定*/
IF(name2 != name1) /*对要修改的字段进行赋值,把参数值赋给变量*/
THEN SET name2 = name1 ;
END IF ;
IF(width2 != width1)
THEN SET width2 = width1 ;
END IF ;
IF(height2 != height1)
THEN SET height2 = height1 ;
END IF ;
IF(fashion2 != fashion1)
THEN SET fashion2 = fashion1 ;
END IF ;
IF(status2 != status1)
THEN SET status2 = status1 ;
END IF ;
IF(_hash2 != _hash1)
THEN SET _hash2 = _hash1 ;
END IF ;
UPDATE `bad_position` SET `id`=id2,`name`=name2,`width`=width2,`height`=height2,`fashion`=fashion2,`status`=status2,`_hash`=_hash2 WHERE `id`= id1 ;/*修改表中值,把变量值赋给字段*/
END IF ;
END $$
DELIMITER ;
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。