views:

138

answers:

5

I have need of some logic in a stored procedure. All the stored procedure does it perform a couple of logic rules and then returns a true or false depending on the result.

The pseudo SQL code:

CREATE TABLE #PV ([Date] DATETIME, Dis FLOAT, Del Float, Sold Float)
INSERT #PV exec GetPVSummaryReport @ID, @PID, @From, @To
SELECT AVG(Dis) / 8 AS DisAvg, AVG(Del) AS DelAvg FROM #PV
IF DisAvg > 20 -- this is the bit I am having problems grokking
    RETURN TRUE
ELSE
    -- do longer calculation

How do you do this sort of logic?

Notes about the code: The table #PV has 4 fields - those provided (Date, Dis, Del and Sold).

+2  A: 
declare @DisAvg float
declare @DelAvg float

-- Instantiate #PV etc

select
  @DisAvg = avg(Dis) / 8,
  @DelAvg = avg(Del)
from
  #PV

if @DisAvg > 20
  return
else
  -- Do something else
Luke Bennett
The RETURN statement should have a value to indicate the condition the procedure was in upon termination.
NYSystemsAnalyst
The question applied to expressions, not return values. I deliberately chose not to return anything in particular.
Luke Bennett
Fair enough. +1 for simplest solution that seems to be basically what graham is looking for.
NYSystemsAnalyst
Since Luke was the first, and his code did demonstrate where I was going wrong, he get's the tick.
graham.reeds
A: 

You need to declare a variable and select into it

DECLARE @DisAvg DOUBLE, @DelAvg DOUBLE
SELECT @DisAvg = AVG(Dis) / 8, @DelAvg = AVG(Del) #PV
IF @DisAvg > 20 -- this is the bit I am having problems grokking
 RETURN 1
ELSE
 -- do longer calculation

Declare with the correct type of the Dis field.

Edit - corrected the return value.

rslite
return can return only integers
Binoj Antony
Dude. fail. RETURN can return FLOATS, BIT, etc... Sif only INTEGERS. tsk tsk!
Pure.Krome
yeah, check this- http://doc.ddart.net/mssql/sql70/ra-rz_14.htm
Binoj Antony
Yep, you're right. I corrected the code example. I just copied the pseudo-code and missed that.
rslite
A: 
DECLARE @DisAvg DECIMAL
DECLARE @ReturnValue bit

// use SET or SELECT to assign values to @DisAvg

IF (@DisAvg > 20)
BEGIN 
    SET  @ReturnValue = 1
END
ELSE
BEGIN

    -- do longer calculation
SET @ReturnValue = 0
END 
SELECT @ReturnValue

Only integers can be returned from the RETURN statement, you can use SELECT instead

Binoj Antony
You are not populating @DisAvg with the average - you are taking the last Dis entry from #PV
Luke Bennett
+1  A: 

Guys,

why is everyone creating a Temp Table? Variable tables are so much nicer (and more performant) :)

lets see...

DECLARE @PV TABLE ([Date] DATETIME,
    Dis FLOAT,
    Del FLOAT,
    Sold FLOAT)

INSERT INTO @PV
EXEC [dbo].[GetPVSummaryReport] @ID, @PID, @From, @To


-- Create some variables, which will contain the results.
DECLARE @DisAvg AS FLOAT,
    @DelAvg AS FLOAT

-- Retrieve results into a variables.
SELECT @DisAvg = AVG(Dis) / 8, @DelAvg AVG(Del)
FROM @PV

-- Check results...    
IF @DisAvg > 20 
   RETURN TRUE
ELSE BEGIN
    -- do longer calculation
END

-- NOTE: I'm not sure where you use @DelAvg .. 
--       I'm assuming it's in the 'do longer calculation' section.

Good luck!

Pure.Krome
Did you actually read the article you linked to? In certain conditions table variables perform far worse than temporary tables.
Luke Bennett
According to this (http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html) you can't use a table variable if you "INSERT @table EXEC sp_someProcedure" which is exactly what I am doing.
graham.reeds
The @DelAvg was supposed to be in the OR section of the logic. I removed it when trying to determine how to work with SQL.
graham.reeds
@Luke : Yep, I have ages back. In certain conditions? please. In certain conditions, i'll fall out of bed and an asteroid will hit the earth. There's _always_ a `certain condition`. What about the `common/general condition`? I'm under the impression that, generally, it (variable tables) perform better that the old school temp tables.
Pure.Krome
@Graham.Reeds: Sure about that Graham? i'm doing `INSERT INTO @table` all the time. (/me runs off, tests this out .. runs back). Confirmed. It _does_ work. Please try it yourself. takes one sec.
Pure.Krome
+1  A: 

LABELS are a beautiful things. LABELS allow you to use GOTO's and this lets you write simple to follow logic inside your stored procedures.


DECLARE @Result int
SET @Result = 1 -- 1=True 0=False

IF TEST1 Failed
BEGIN
  SET @Result = 0
  GOTO ENDPROCESSING
END

IF TEST2 Failed
BEGIN
  SET @Result = 0
  GOTO ENDPROCESSING
END
IF TEST3 Failed
BEGIN
  SET @Result = 0
  GOTO ENDPROCESSING
END

ENDPROCESSING:
SELECT @Result
Cape Cod Gunny
Neat. Never knew SP had goto.
graham.reeds