存储过程中的逻辑

发布于 2024-08-08 07:45:20 字数 499 浏览 5 评论 0原文

我需要存储过程中的一些逻辑。所有存储过程都会执行一些逻辑规则,然后根据结果返回 true 或 false。

伪 SQL 代码:

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

如何执行这种逻辑?

代码注释:表 #PV 有 4 个字段 - 提供的字段(Date、Dis、Del 和 Sold)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稀香 2024-08-15 07:45:20
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
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
感性不性感 2024-08-15 07:45:20

为什么每个人都创建临时表?变量表要好得多(性能更高): )

让我们看看...

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.

祝你好运!

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!

还如梦归 2024-08-15 07:45:20

标签是一件美丽的事情。 LABELS 允许您使用 GOTO,这使您可以在存储过程中编写简单易懂的逻辑。


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

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
欢烬 2024-08-15 07:45:20

您需要声明一个变量并选择它

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

并使用 Dis 字段的正确类型进行声明。

编辑 - 更正返回值。

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.

护你周全 2024-08-15 07:45:20
DECLARE @DisAvg DECIMAL
DECLARE @ReturnValue bit

// 使用SET或SELECT给@DisAvg赋值

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

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

RETURN语句只能返回整数,可以使用SELECT代替

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文