T-SQL - 参数分配、数据检索和比较在一行中完成 - 这可能吗?

发布于 2024-10-12 12:03:51 字数 709 浏览 2 评论 0原文

我想在T-SQL中做这样的事情,但它会返回一个错误:

DECLARE @Stock int

IF(SELECT @Stock = [Stock] FROM dbo.Products WHERE [ProductID] = 1) > 5
    PRINT 'Stock is good: Current stock is ' + @Stock
ELSE
    PRINT 'Order some more - we are below minimum stock'

返回的错误是:

消息 102,级别 15,状态 1,第 2 行: “=”附近的语法不正确。

我当然可以这样做:

DECLARE @Stock int

SELECT @Stock = [Stock] FROM dbo.Products WHERE [ProductID] = 1
IF(@Stock > 5)
    PRINT 'Stock is good: Current stock is ' + @Stock
ELSE
    PRINT 'Order some more - we are below minimum stock'

我不确定这是否只是 T-SQL 语言的工作方式,对此没有什么可做的。我基本上是在寻找一种编写更少代码的捷径:)

有人有一些好的想法吗?

I want to do something like this in T-SQL, but it will return an error:

DECLARE @Stock int

IF(SELECT @Stock = [Stock] FROM dbo.Products WHERE [ProductID] = 1) > 5
    PRINT 'Stock is good: Current stock is ' + @Stock
ELSE
    PRINT 'Order some more - we are below minimum stock'

The error returned is:

Msg 102, Level 15, State 1, Line 2:
Incorrect syntax near '='.

I can of course do something like this instead:

DECLARE @Stock int

SELECT @Stock = [Stock] FROM dbo.Products WHERE [ProductID] = 1
IF(@Stock > 5)
    PRINT 'Stock is good: Current stock is ' + @Stock
ELSE
    PRINT 'Order some more - we are below minimum stock'

I am not sure if it is just the way the T-SQL language works and there is nothing to do about it. I am basically looking for a shortcut to write less code :)

Anybody has some good ideas?

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

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

发布评论

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

评论(2

千笙结 2024-10-19 12:03:51

MAX 聚合将返回一行 行,没有 GROUP BY (根据我的回答)

SELECT
     CASE
         WHEN MAX(Z.Stock) > 5 THEN 'Stock is good: Current Stock' + CAST(MAX(Z.Stock) as varchar(10))
         ELSE 'Order some more - we are below minimum stock'
     END
FROM
    dbo.Products Z WHERE [ProductID] = 1

编辑:

无论如何,你永远不会从存储过程中打印..

--Do stuff

SELECT
     @Stock = MAX(Z.Stock),
     @UserMessage = 
         CASE
             WHEN MAX(Z.Stock) > 5
                 THEN 'Stock is good: Current Stock' + CAST(MAX(Z.Stock) as varchar(10))
             ELSE 'Order some more - we are below minimum stock'
         END
FROM
    dbo.Products Z WHERE [ProductID] = 1

--Do more stuff with @Stock

A MAX aggregate will return one row without a GROUP BY (as per my answer here)

SELECT
     CASE
         WHEN MAX(Z.Stock) > 5 THEN 'Stock is good: Current Stock' + CAST(MAX(Z.Stock) as varchar(10))
         ELSE 'Order some more - we are below minimum stock'
     END
FROM
    dbo.Products Z WHERE [ProductID] = 1

Edit:

You would never PRINT from a stored proc anyway..

--Do stuff

SELECT
     @Stock = MAX(Z.Stock),
     @UserMessage = 
         CASE
             WHEN MAX(Z.Stock) > 5
                 THEN 'Stock is good: Current Stock' + CAST(MAX(Z.Stock) as varchar(10))
             ELSE 'Order some more - we are below minimum stock'
         END
FROM
    dbo.Products Z WHERE [ProductID] = 1

--Do more stuff with @Stock
左岸枫 2024-10-19 12:03:51

假设您不打算稍后在过程中使用 @Stock,您可以执行以下操作:

Select Case
        When Z.Stock > 5 
            Then 'Stock is good: Current Stock' + Cast( Z.Stock as varchar(10))
        Else 'Order some more - we are below minimum stock'
        End
From (
    Select P.Stock
    From dbo.Products As P
    Where P.ProductId = 1
    Union All
    Select Value
    From ( Select 0 As Value ) As Z
    Where Not Exists    (
                        Select 1
                        From dbo.Products As P1
                        Where P1.ProductId = 1
                        )
    ) As Z

如果您想稍后在过程中重用 @Stock,则必须执行多个语句。不能将赋值与在 T-SQL 中的同一语句中返回数据的语句组合起来。只需尝试查看错误即可:

Declare @Foo int

Select @Foo = 42, object_id
From sys.objects

消息 141,级别 15,状态 1,第 3 A 行
赋值的 SELECT 语句
不得组合到变量
与数据检索操作。

Assuming you are not planning on using @Stock later in the procedure you can do:

Select Case
        When Z.Stock > 5 
            Then 'Stock is good: Current Stock' + Cast( Z.Stock as varchar(10))
        Else 'Order some more - we are below minimum stock'
        End
From (
    Select P.Stock
    From dbo.Products As P
    Where P.ProductId = 1
    Union All
    Select Value
    From ( Select 0 As Value ) As Z
    Where Not Exists    (
                        Select 1
                        From dbo.Products As P1
                        Where P1.ProductId = 1
                        )
    ) As Z

If you wanted to reuse @Stock later in your procedure then you must do it multiple statements. You cannot combine an assignment with a statement that returns data in the same statement in T-SQL. One need only try to see the error:

Declare @Foo int

Select @Foo = 42, object_id
From sys.objects

Msg 141, Level 15, State 1, Line 3 A
SELECT statement that assigns a value
to a variable must not be combined
with data-retrieval operations.

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