Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

A severe error occurred on the current command. The results, if any, should be discarded. - SQL 2012, executing Table-Valued Function

$
0
0

Hello,

I'm completely stuck on this error. I run the contents of a function(with param values plugged in) and it returns a collection of rows, yet when I select from the function I get the following error:

"

Msg 0, Level 11, State 0, Line 0

A severe error occurred on the current command.  The results, if any, should be discarded.

Msg 0, Level 20, State 0, Line 0

A severe error occurred on the current command.  The results, if any, should be discarded.

".

Here is the complete function:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_function1_DBD]

 @param1   int = null,
 @param2   int,
 @param3   int = null,
 @param4  int = null,
 @param5  int,
 @param6 int = null,
 @param7   DateTime,
 @param8   DateTime,
 @param9   varchar(20) 
)
RETURNS TABLE
AS
RETURN
(

WITH CTE1 as
(
 SELECT
  CTE1.[Date]
  , CTE1.[PolicyID]
  , CTE1.Value as ReadingsValue
  , CTE1.PolicyCalculationType
  , Table1.ActualThresholdValue
  , lower(Table3.Name) as OutputType
  , ISNULL( Table1.Multiplier, isnull(Table2.Multiplier, 0) ) AS Multiplier
  , CASE WHEN Table1.TariffPlanID is not null THEN isnull(Table2.AboveRate, 0) ELSE isnull(Table1.AboveRate,0) END as AboveRate
  , CASE WHEN Table1.TariffPlanID is not null THEN isnull(Table2.BelowRate,0) ELSE isnull(Table1.BelowRate,0) END as BelowRate
  , CASE WHEN Table1.TariffPlanID is not null THEN isnull(Table2.ThresholdValue,0) ELSE isnull(Table1.ThresholdValue,0) END as Threshold
  , Table1.Constant
 FROM [dbo].[fn_function2]( @param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param9) CTE1
 left outer join Policies P ON CTE1.PolicyID = P.PolicyID
 left outer join Table1 Table1 ON
  Table1.PolicyID = CTE1.PolicyID and CTE1.[Date] >= Table1.DateFrom and ( Table1.DateTo is null or CTE1.[Date] < Table1.DateTo )
 left outer join Table3 Table3 ON Table1.PolicyOutputTypeID = Table3.PolicyOutputTypeID
 left outer join Table4 Table4 on Table1.TariffPlanID = Table4.TariffPlanID
 LEFT OUTER JOIN Table2 
  ON Table4.TariffPlanID = Table2.TariffPlanID AND CTE1.[Date] >= Table2.DateFrom and ( Table2.DateTo is null or CTE1.[Date] < Table2.DateTo )
 LEFT OUTER JOIN Table4 
  ON P.PolicyOutputUnitID = Table4.ReadingTypeID
 LEFT OUTER JOIN Table5 ON P.IntervalID = Table5.IntervalID
), CTE2 as
(
 SELECT CTE1.[Date]
   , CTE1.PolicyID
   , CTE1.ReadingsValue
   , CTE1.PolicyCalculationType
   , [dbo].[fn_Function2]( CTE1.ReadingsValue, CTE1.Threshold, CTE1.AboveRate, CTE1.BelowRate, CTE1.Multiplier, CTE1.Constant, CTE1.OutputType ) as PolicyValue
 FROM GroupedData CTE1
)
SELECT CASE WHEN 'hour' = 'hour' THEN DATEADD( hh, DATEPATable4( hh, CTE2.[Date]), CTE2.[Date] )
   WHEN 'hour' = 'day' THEN CTE2.[Date]
   WHEN 'hour' = 'week' THEN dateadd( dd, 1 - datepart( dw, CTE2.[Date]), CTE2.[Date])
   WHEN 'hour' = 'month' THEN dateadd( dd, 1 - datepart( dd, CTE2.[Date]), CTE2.[Date])
   WHEN 'hour' = 'year' THEN dateadd( dd, 1 - datepart( dy, CTE2.[Date]), CTE2.[Date])
   END as [Date]
 , CTE2.PolicyID
 , CASE WHEN CTE2.PolicyCalculationType = 'value' THEN SUM(CTE2.ReadingsValue)
  WHEN CTE2.PolicyCalculationType = 'max' THEN MAX( CTE2.ReadingsValue )
  WHEN CTE2.PolicyCalculationType = 'min' THEN MIN( CTE2.ReadingsValue )
  WHEN CTE2.PolicyCalculationType = 'avg' THEN AVG( CTE2.ReadingsValue )
  END as ReadingsValue
 , SUM( CTE2.PolicyValue ) as PolicyValue
FROm CTE2 CTE2
group by [Date], CTE2.PolicyCalculationType, CTE2.PolicyID

)

 


GO

=------------------------------------

If you could point me in the right direction here I would be grateful.

Thanks,

Chris


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>