sqlserver 存储过程当中如何实现增删改查

sqlserver 存储过程当中如何实现增删改查_第1张图片

 

--声明存储过程   新增编辑
ALTER procedure [dbo].[Eng_MyAddOrEdtADPro]
    @My_Cocode int,
    @Type int, --  --1 新增  2 编辑
    @My_KeyId uniqueidentifier,
    @My_PCode int,
    @My_SCode int,
    @My_PName nvarchar(36),
    @My_SName nvarchar(36),
    @My_Order int,
    @My_Apply_UserName nvarchar(36),
    @My_Apply_UserId uniqueidentifier,--申请人的Id
    @My_Error int output,
    @My_ErrorMessage nvarchar(50) output
as
declare @error int;
declare @errormessage nvarchar(50);


set @error=0;
set @errormessage='返回数据成功';


BEGIN     


      DECLARE @My_SKeyId nvarchar(36);
      DECLARE @My_SCode2 int;
      DECLARE @My_PKeyId nvarchar(36);
      DECLARE @My_PCode2 int;

      SELECT @My_SCode2=My_SCode,@My_SKeyId=My_SKeyId FROM MyADs A WHERE A.My_Cocode=@My_Cocode AND (My_SCode=@My_SCode OR A.My_SName=@My_SName) AND My_Status=1;
           
      SELECT @My_PCode2=My_PCode,@My_PKeyId=My_PKeyId FROM MyADs A WHERE A.My_Cocode=@My_Cocode AND (My_PCode=@My_PCode OR A.My_PName=@My_PName) AND My_Status=1;

      IF ISNULL(@My_SCode,'')='' OR  ISNULL(@My_SCode,'')!=@My_SCode2 OR ISNULL(@My_SKeyId,'')='' OR  ISNULL(@My_PCode,'')='' OR ISNULL(@My_PCode,'')!=@My_PCode2 OR ISNULL(@My_PKeyId,'')=''
      BEGIN
          SET @error=1;
          SET @errormessage = '不存在或者不匹配!';
          -- RETURN;
      END

      IF @error=0
      BEGIN

      IF @Type=1
      BEGIN
           INSERT INTO MyADs VALUES (@My_KeyId,@My_Cocode,@My_PKeyId,@My_SKeyId,@My_PCode2,@My_SCode2,@My_PName,@My_SName,1,@My_Order,@My_Apply_UserName,@My_Apply_UserId,GETDATE(),@My_Apply_UserName,@My_Apply_UserId,GETDATE());
      END

      
      IF @Type=2 
      BEGIN
           UPDATE MyADs SET My_SName=@My_SName,@My_PName=My_PName,My_Order=@My_Order,My_UpdateUserName=@My_Apply_UserName,My_UpdateUserId=@My_Apply_UserId,My_UpdateDate=GETDATE()  WHERE  My_KeyId=@My_KeyId AND My_Status=1;
      END

      SET @errormessage =@My_SName+ '更新成功!';

      END

     SET @My_Error=@error;SET @My_ErrorMessage=@errormessage
     SET @My_SKeyId=NULL;
     SET @My_PKeyId=NULL;
     SET @My_SCode2=NULL;
     SET @My_PCode2=NULL;
     SET @error=NULL;
     SET @errormessage=NULL;
END

你可能感兴趣的:(sqlserver,数据库)