Codeigniter与存储过程

时间:2016-03-15 07:04:22

标签: sql-server-2008 codeigniter stored-procedures

我想用存储过程保存。请先检查我的脚本

`USE [Payroll]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ResignSave]    
(    
 @ResignCode varchar(50),    
 @Nip Varchar(50),
 @Keterangan Varchar(50),
 @ResignDate Varchar(50),
 @CreatedBy varchar(50),
 @CreatedDate date,
 @msg varchar(25) = NULL

)    
AS 
BEGIN
Begin Try


set @ResignCode = (select case 
    when right(max(ResignCode),7) is null then 'RC0000001' 
        else ('RC' + RIGHT('0000000' + cast(right(max(ResignCode),7) + 1 as nvarchar),7)) 
 end ResignCode from Resign)        

    INSERT INTO Resign(ResignCode,Nip, Keterangan, ResignDate, CreatedBy,CreatedDate)
            VALUES(@ResignCode,@Nip,@Keterangan, @ResignDate,@CreatedBy,GETDATE())  
end try

Begin Catch           
  Select ERROR_NUMBER() as ErrorNumber, ERROR_MESSAGE() as ErrorMessage               
 End Catch                                  
End   
`

我的存储过程正常。我的问题是,无论如何检查输入是否存在于我的表中然后将其设置为我的@msg

1 个答案:

答案 0 :(得分:1)

您可能正在寻找以下代码。

ALTER Procedure [dbo].[ResignSave]    
(    
 @ResignCode varchar(50),    
 @Nip Varchar(50),
 @Keterangan Varchar(50),
 @ResignDate Varchar(50),
 @CreatedBy varchar(50),
 @CreatedDate date,
 @msg varchar(25) = NULL

)    
AS 
BEGIN
  Begin Try

    if exists (select * from Resign where ResignCode = @ResignCode and Nip = @Nip and ResignDate = @ResignDate) then
    BEGIN
        set @msg = 'Record already exists'
        select @msg AS ProcResult
    END

    set @ResignCode = (select case 
        when right(max(ResignCode),7) is null then 'RC0000001' 
        else ('RC' + RIGHT('0000000' + cast(right(max(ResignCode),7) + 1 as nvarchar),7)) 
    end ResignCode from Resign)        

    INSERT INTO Resign(ResignCode,Nip, Keterangan, ResignDate, CreatedBy,CreatedDate)
            VALUES(@ResignCode,@Nip,@Keterangan, @ResignDate,@CreatedBy,GETDATE())  

    SELECT 'Insert Successful' AS ProcResult
  end try

  Begin Catch           
    Select 'Insert Failed:' + cast(ERROR_NUMBER() as varchar) + ERROR_MESSAGE() AS ProcResult
  End Catch                                  
End

如果您正在寻找这个,那么您就不需要@msg参数了。 juts执行过程并获取返回的结果集