日期:2014-05-16  浏览次数:20744 次

储存过程,判断字段。
SQL code

CREATE PROCEDURE `Up_MyConcern_Ins`(
 IN  Id_Int ,
 IN  StarConcern_VarC varchar(800), 
 IN  TI_Id_Int,  
 IN  ChannelNo_Int,  
 IN  UserName_VarC varchar(800) ,   
 

 OUT ReturnValues_Int INT
)
PROC:
BEGIN

DECLARE IsExists_Int INT;
DECLARE Id_Int  INT;
 

DECLARE EXIT HANDLER FOR SQLSTATE'23000' 
BEGIN
    SET ReturnValues_Int = 1;
    ROLLBACK;
END;

SET AUTOCOMMIT = 0;
START TRANSACTION;

SET ReturnValues_Int = 0;

SELECT COUNT(1) INTO IsExists_Int FROM MyConcern WHERE MC_StarConCern_VarC = StarConCern_VarC;


IF IsExists_Int = 0 THEN 
    SET ReturnValues_Int = 2;
    ROLLBACK;
    LEAVE PROC;
END IF;   




INSERT INTO MyConcern(MC_Id_Int,MC_TI_Id_Int,MC_ChannelNo,MC_UserId_Int,MC_UserName_VarC,MC_StarConcern_VarC)
       VALUES(Id_Int,FN_Id_Int,ChannelNo_Int,UserId_Int,UserName_VarC,StarConcern_VarC);





表MyConcern的插入存储过程,插入前要对TI_Id_Int(在另外一张表里存着) 字段判断,存在可以插入,不存着返回。
怎么修改插入存储过程?

------解决方案--------------------
自己动手做一下吧,有问题再问
------解决方案--------------------
楼主碰到的问题是什么? 建议把错误信息一同贴出以供分析。
------解决方案--------------------
SELECT COUNT(1) INTO IsExists_Int FROM MyConcern WHERE MC_StarConCern_VarC = StarConCern_VarC;


IF IsExists_Int = 0 THEN 
SET ReturnValues_Int = 2;
ROLLBACK;
LEAVE PROC;
END IF;


同样方式对 TI_Id_Int 进行判断。只是表改变为存 TI_Id_Int 字段的表。