Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
赋值语句中的SQL Distinct关键字_Sql_Sql Server_Tsql_Distinct - Fatal编程技术网

赋值语句中的SQL Distinct关键字

赋值语句中的SQL Distinct关键字,sql,sql-server,tsql,distinct,Sql,Sql Server,Tsql,Distinct,我有一个有效的查询: DECLARE @ProductID int SET @ProductID = '1234' SELECT DISTINCT TOP 12 a.ProductID FROM A a WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE b.ProductID = @ProductID) AND a.ProductID != @ProductID 它返回12个产品编号的列表,所有编号都是唯一的 我需要将这些结果

我有一个有效的查询:

DECLARE @ProductID int
SET @ProductID = '1234'

SELECT DISTINCT TOP 12 a.ProductID
FROM A a
WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE b.ProductID = @ProductID)
AND a.ProductID != @ProductID
它返回12个产品编号的列表,所有编号都是唯一的

我需要将这些结果存储在一个以逗号分隔的变量中,因为这是第三方存储过程所需要的。所以我有这个:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT TOP 12 @relatedprods = @relatedprods + CONVERT(VARCHAR(20), a.ProductID) + ', '
   FROM A a
   WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE B.ProductID = @ProductID)
   AND a.ProductID != @ProductID

 SELECT @relatedprods
现在,这些都不是不同的,但它返回12行

现在我将“distinct”添加回,如第一个查询中所示:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT DISTINCT TOP 12 @relatedprods = @relatedprods + CONVERT(VARCHAR(20), a.ProductID) + ', '
   FROM A a
   WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE B.ProductID = @ProductID)
   AND a.ProductID != @ProductID

 SELECT @relatedprods
逗号分隔列表中只返回一个产品!“distinct”在赋值语句中不起作用吗?我做错了什么?还是有办法绕过这个问题

提前谢谢

结论:

我不知道是什么导致了这个问题,尽管提出的猜测似乎合乎逻辑。我能够通过子查询解决此问题,我正在发布它,以便其他人可以看到解决方案:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT @relatedprods = @relatedprods + CONVERT(VARCHAR(20), c.ProductID) + ',' 
    FROM (SELECT DISTINCT TOP 12 a.ProductID FROM A a WHERE a.CategoryID IN 
      (SELECT b.CategoryID 
      FROM B b 
      WHERE B.ProductID = @ProductID) 
    AND a.ProductID != @ProductID ) c

 SET @relatedprods = SUBSTRING(@relatedprods, 0, LEN(@relatedprods))
 SELECT @relatedprods

你能用一张临时桌吗

DECLARE @relatedProdList TABLE (prod VARCHAR(20))
INSERT INTO @relatedProdList 
SELECT DISTINCT TOP 12 CONVERT(VARCHAR(20), a.ProductID)
FROM A a 
WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE b.ProductID = @ProductID) 
AND a.ProductID != @ProductID 

SELECT @relatedprods = @relatedprods + prod + ', '
FROM @relatedProdList

获取子查询中的12条记录:

declare
  @ProductID int,
  @relatedprods varchar(8000)

set @ProductID = '1234'
set @relatedprods = ''

select @relatedprods = @relatedprods + cast(ProductID as varchar) + ','
from (
  select distinct top 12 a.ProductId
  from A a
  inner join B b on b.CategoryID = a.CategoryID
  where  B.ProductID = @ProductID and a.ProductID != @ProductID
) x

这只是一个粗略的猜测,但我认为在
SELECT
中执行赋值操作会因
DISTINCT
而失败,因为它必须在生成列值后进行排序,以增强区分性。在添加一个非平凡的
orderby
子句(例如,通过表达式排序)时,我见过类似的行为

如果您使用的是SQL Server 2005或更高版本,也可以将
改为用于XML路径

DECLARE @ProductID int
DECLARE @relatedprods varchar(8000)
SET @ProductID = 1234

SET @relatedprods = (SELECT DISTINCT TOP 12
    CONVERT(VARCHAR(20), a.ProductID) + ','
    FROM A a
    WHERE a.CategoryID IN (...)
    AND a.ProductID != @ProductID
    FOR XML PATH(''))

-- FOR XML PATH will add an extra comma at the end, so remove it.
IF LEN(@relatedProds) > 1
    SET @relatedProds = SUBSTRING(@relatedProds, 1, LEN(@relatedProds) - 1)

因为您使用的是子查询而不是变量赋值语法,所以这些限制不适用。

@Brandi-什么版本的SQL Server?