0
我有一個父存儲過程和一個嵌套存儲過程。父存儲過程在循環中調用嵌套存儲過程。現在,當有一些條件匹配我提出了錯誤。出現此錯誤時,我想停止所有進程並返回錯誤。錯誤從嵌套的MSSQL過程傳遞給父過程
CREATE Proc [dbo].[Usp_GenSalarySheet](@SalData [HRM].UTDT_SalaryData ReadOnly)
AS
set nocount on
DECLARE @SMT NVARCHAR(MAX)
SET @SMT = 'Create Table ##SalarySheet (StaffID INT,FullName NVARCHAR(1024),PresentDays NVARCHAR(1024),Absent NVARCHAR(1024),Department NVARCHAR(1024),Designation NVARCHAR(1024),'
DECLARE @HopName nvarchar(max)
Declare HOPCursor cursor for select ID from HRM.tbl_HOP
OPEN HOPCursor
FETCH NEXT FROM HOPCursor INTO @HopName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SMT = @SMT + ' [' + @HopName + '] DECIMAL(19,7),'
FETCH NEXT FROM HOPCursor into @HopName
END
SET @SMT = @SMT + '[Total] DECIMAL(19,7))'
CLOSE HOPCursor
DEALLOCATE HOPCursor
print (@smt)
exec (@SMT)
select * into #temp from @SalData
Declare @TopID INT
While (Select Count(*) From #Temp) > 0
Begin
Select Top 1 @TopID = StaffID From #temp
Declare @StaffID INT =(select top 1 StaffID from #temp)
Declare @StaffName NVARCHAR(1024) = (SELECT TOP 1 FullName FROM #temp)
Declare @WorkingDays Int = (SELECT top 1 WorkingDays from #temp)
Declare @Leave INT = (SELECT top 1 [Absent] from #temp)
INSERT INTO ##SalarySheet(StaffID,FullName,[Absent]) values(@StaffID,@StaffName,@Leave)
DECLARE @HOPType INT
DECLARE @Value Decimal(19,7)
DECLARE @CalcVal DECIMAL(19,7) = 0
DECLARE @Formula NVARCHAR(MAX)
DECLARE @Total DECIMAL(19,7)
DECLARE @PayEvery INT
DECLARE @Round Int
Declare HOPList Cursor for SELECT ID,HOPType,Value,Formula,RoundOff,PayEvery FROM HRM.Tbl_HOP order by Schedule
open HOPList
FETCH NEXT FROM HOPList INTO @HopName,@HOPType,@Value,@Formula,@Round,@PayEvery
WHILE @@FETCH_STATUS = 0
BEGIN
if exists(select * from HRM.[Tbl_ContractHOPDetails] where PersonalDetailsID = @StaffID and HOPID = @HopName)
print('select * from HRM.[Tbl_ContractHOPDetails] where PersonalDetailsID = ' + convert(varchar(max), @StaffID) + ' and HOPID =' + convert(varchar(max),@HopName))
begin
if(@HOPType=51)
begin
exec HRM.Usp_GetSalaryValueFromFormula @StaffID,@Formula,@HOPType,@Leave,@WorkingDays,@Value output
set @HOPType= 50
end
if(@HOPType=50)
begin
set @CalcVal = @value
END
IF(@HOPType=38)
BEGIN
SET @CalcVal = @Value - ((@Value/@WorkingDays) * @Leave)
END
if(@PayEvery= 40)
begin
set @CalcVal = ((@CalcVal * @WorkingDays) - (@CalcVal * @Leave))
end
if(@Round = 45)
begin
set @CalcVal = round(@CalcVal,2)
end
else if(@Round = 46)
begin
set @CalcVal = CEILING(@CalcVal)
end
else if(@Round = 47)
begin
set @CalcVal = FLOOR(@CalcVal)
end
set @SMT ='UPDATE ##SalarySheet SET [' + @HopName + '] = ' + cast(@CalcVal as nvarchar(max)) + ' where StaffID = ' + cast(@StaffID as nvarchar(max))
exec (@smt)
end
SET @CalcVal = 0
FETCH NEXT FROM HOPList INTO @HopName,@HOPType,@Value,@Formula,@Round,@PayEvery
END
close HOPList
DEALLOCATE HOPList
set @SMT ='UPDATE ##SalarySheet SET [Total] = ' + cast(@Total as nvarchar(max)) + ' where StaffID = ' + cast(@StaffID as nvarchar(max))
exec (@smt)
Delete #temp Where StaffID = @TopID
end
select * from ##SalarySheet
drop table ##SalarySheet
這是我父存儲Procudere和嵌套過程如下:
CREATE proc [HRM].[Usp_GetSalaryValueFromFormula](@StaffID INT,@val nvarchar(max),@HOPType INT,@Leave INT,@WorkingDays INT, @GetResult Decimal(19,7) output)
as
set nocount on
Declare @Formula Varchar(max)
declare @initial INT =0
declare @final INT =0
Declare @DataVal NVARCHAR(MAX) -- set the value from HOP table
declare @FieldVal nvarchar(max)
declare @cnt int = 0
Declare @Complete Int =CHARINDEX ('[',@val,0)
while (@Complete <> 0)
begin
set @initial = CHARINDEX ('[',@val,0)
set @final = CHARINDEX(']',@val,0)
set @FieldVal = SUBSTRING(@val,@initial,(@[email protected]) + 1)
if len(@FieldVal)<>0
begin
select @HOPType = HOPType, @DataVal= (case when HOPType = 51 then [Formula] else cast([Value] as nvarchar(max)) end) from HRM.Tbl_ContractHOPDetails where PersonalDetailsID = @StaffID and HOPID in(select ID from HRM.tbl_HOP where HOPName = replace(replace(@fieldVal,'[',''),']',''))
if (@DataVal is null or @DataVal ='')
begin
RAISERROR ('Nested HOP is not defined.',11,1)
RETURN
end
print(@DataVal)
if ISNUMERIC(@DataVal)=1
begin
if(@HOPType = 38)
begin
SET @DataVal = cast(@DataVal as decimal(19,7)) - ((cast(@DataVal as decimal(19,7))/@WorkingDays) * @Leave)
end
end
set @val = replace(@val,@fieldVal,@DataVal)
set @fieldVal= ''
set @DataVal = ''
end
set @Complete = CHARINDEX ('[',@val,0)
set @fieldVal =''
set @final =0
set @initial = 0
end
SET @Complete =CHARINDEX ('{',@val,0)
while (@Complete <> 0)
BEGIN
set @initial = CHARINDEX ('{',@val,0)
set @final = CHARINDEX('}',@val,0)
set @FieldVal = SUBSTRING(@val,@initial+1,(@[email protected])-1)
if len(@FieldVal)<>0
begin
set @DataVal = isnumeric((SELECT 0 FROM [HRM].Tbl_StaffTag where CValue = @FieldVal and StaffID = @StaffID))
set @FieldVal = '{' + @FieldVal + '}'
set @val = replace(@val,@fieldVal,@DataVal)
set @fieldVal= ''
set @DataVal = ''
end
set @Complete = CHARINDEX ('{',@val,0)
set @final =0
set @initial = 0
END
DECLARE @RetrunVal DECIMAL(19,7)
declare @ParmDefinition Nvarchar(512) = '@GetVal decimal(19,7) OUTPUT'
Declare @SMT NVARCHAR(MAX) = ' SET @GetVal = ' + @val
EXECUTE sp_executeSQL @Smt, @ParmDefinition, @GetVal [email protected]al OUTPUT
set @GetResult = @RetrunVal
但在目前的情況下它會引發錯誤,並再次從主程序運行循環的下一個步驟。但我想這RAISERROR
之後終止的完整過程請幫我
將您的代碼放入try-catch –