查询中没有结果已声明变量

时间:2018-04-06 10:50:11

标签: sql-server

我创建了一个多数据库连接查询。通常查询工作。但我需要改进我的查询。我决定使用变量。我不是在sql-server中使用变量的专家。

执行此查询时,没有结果。只有“命令已成功完成。”

我该怎么办?

DECLARE @SoldQuantity INT;
DECLARE @InvoiceCount INT;
DECLARE @Stock DECIMAL;
DECLARE @StockCode NVARCHAR(MAX);
DECLARE @StockName NVARCHAR(MAX);
DECLARE @Unit NVARCHAR(MAX);
DECLARE @SalePrice DECIMAL;

SELECT
    @StockCode = p.StockCode,
    @StockName = p.Name,
    @Unit = p.Unit,
    @SalePrice = MAX(p.SalePrice),
    @SoldQuantity = COALESCE(SUM(d.Quantity), 0),
    @InvoiceCount = COUNT(DISTINCT d.InvoiceId),
    @Stock = (
        select SUM(ISNULL(ENVANTER, 0)) as ENVANTER 
        FROM B_DB.dbo.F0101D0002TBLDEPOENVANTER 
        WHERE DEPO = 101 AND STOKNO = p.ProductId 
    ) 
FROM A_DB.dbo.InvoiceDetails as d 
    LEFT JOIN A_DB.dbo.Products as p ON p.ProductId = d.ProductId
WHERE d.InvoiceId IN (
    select Id from A_DB.dbo.Invoices 
    WHERE Canceled = 0 AND Returned = 0 AND Branch = 'BRANCH_A' 
        AND CreatedAt >= '2018-01-05' AND CreatedAt< '2018-04-05') AND d.Canceled = 0 
        AND p.ProductId NOT IN (select ProductId from A_DB.dbo.Products WHERE CreatedAt >= '2018-01-05' AND CreatedAt < '2018-04-05')
GROUP BY p.ProductId, p.StockCode, p.Name, p.Unit
HAVING COUNT(DISTINCT d.InvoiceId) < 10
AND @Stock > @SoldQuantity

更新

此查询正常。但是在有条款我正在重复自己。我问有没有办法不重复自我。

SELECT
    p.StockCode,
    p.Name,
    p.Unit,
    MAX(p.SalePrice),
    COALESCE(SUM(d.Quantity), 0) as SoldQuantity,
    COUNT(DISTINCT d.InvoiceId) as InvoiceCount,
    (
        select SUM(ISNULL(ENVANTER, 0)) as ENVANTER 
        FROM B_DB.dbo.F0101D0002TBLDEPOENVANTER 
        WHERE DEPO = 101 AND STOKNO = p.ProductId 
    ) as Stock
FROM A_DB.dbo.InvoiceDetails as d 
    LEFT JOIN A_DB.dbo.Products as p ON p.ProductId = d.ProductId
WHERE d.InvoiceId IN (
    select Id from A_DB.dbo.Invoices 
    WHERE Canceled = 0 AND Returned = 0 AND Branch = 'BRANCH_A' 
        AND CreatedAt >= '2018-01-05' AND CreatedAt< '2018-04-05') AND d.Canceled = 0 
        AND p.ProductId NOT IN (select ProductId from A_DB.dbo.Products WHERE CreatedAt >= '2018-01-05' AND CreatedAt < '2018-04-05')
GROUP BY p.ProductId, p.StockCode, p.Name, p.Unit
 HAVING COUNT(DISTINCT d.InvoiceId) < 10
AND (select SUM(ISNULL(ENVANTER, 0)) as ENVANTER 
        FROM B_DB.dbo.F0101D0002TBLDEPOENVANTER 
        WHERE DEPO = 101 AND STOKNO = p.ProductId ) > COALESCE(SUM(d.Quantity), 0)

1 个答案:

答案 0 :(得分:1)

为变量赋值时,不会给出输出结果集。例如,请使用以下简单声明:

DECLARE @i int;
SELECT @i = 1;

就它本身而言,这不会返回结果集。您需要使用另一个SELECT语句来返回@i

的值
DECLARE @i int;
SELECT @i = 1;
SELECT @i AS i;

似乎也是,你认为变量可以包含多个值,这是错误的。变量只能有1个值。所以,让我们稍微扩展一下:

DECLARE @i int;
SELECT @i = i
FROM (VALUES (1),(2)) V(i);
SELECT @i AS i;

请注意,@i的值为2

但是,您似乎并不需要在SELECT中根据需要为结果集分配变量的值,而且您可以设置的值是SELECT中的变量,并在HAVING中使用它(错误)。因此,这可能是你所追求的:

SELECT
    p.StockCode,
    p.Name,
    p.Unit,
    MAX(p.SalePrice),
    COALESCE(SUM(d.Quantity), 0),
    COUNT(DISTINCT d.InvoiceId),
    s.ENVANTER AS Stock
FROM A_DB.dbo.InvoiceDetails as d 
    LEFT JOIN A_DB.dbo.Products as p ON p.ProductId = d.ProductId
    CROSS APPLY (
        select SUM(ISNULL(ENVANTER, 0)) as ENVANTER 
        FROM B_DB.dbo.F0101D0002TBLDEPOENVANTER 
        WHERE DEPO = 101 AND STOKNO = p.ProductId 
    ) Stock s
WHERE d.InvoiceId IN (
    select Id from A_DB.dbo.Invoices 
    WHERE Canceled = 0 AND Returned = 0 AND Branch = 'BRANCH_A' 
        AND CreatedAt >= '2018-01-05' AND CreatedAt< '2018-04-05') AND d.Canceled = 0 
        AND p.ProductId NOT IN (select ProductId from A_DB.dbo.Products WHERE CreatedAt >= '2018-01-05' AND CreatedAt < '2018-04-05')
GROUP BY p.ProductId, p.StockCode, p.Name, p.Unit, s.ENVANTER
HAVING COUNT(DISTINCT d.InvoiceId) < 10
AND s.ENVANTER > SUM(d.Quantity);

然而,这是未经测试(因为我无法在没有数据的情况下测试它,如果您收到错误,则需要进行故障排除)。查询可能会被简化(因为子查询中有一些子查询),但是,如果没有进一步的理解,我已经离开了你的查询。已删除变量,并且HAVING子句已更改。我还将SELECT中的子查询移动到CROSS APPLY