这段 T-SQL 代码中发生了什么? (连接 SELECT 语句的结果)

发布于 2024-08-28 00:32:58 字数 1096 浏览 9 评论 0原文

我刚刚开始学习 T-SQL,需要一些帮助来理解特定代码块中发生的情况。我修改了我在上一个问题中收到的答案,这里是有问题的代码:

DECLARE @column_list AS varchar(max) 
SELECT @column_list = COALESCE(@column_list, ',') + 
    'SUM(Case When Sku2=' + CONVERT(varchar, Sku2) + 
    ' Then Quantity Else 0 End) As [' + 
    CONVERT(varchar, Sku2) + ' - ' + 
    Convert(varchar,Description) +'],'
FROM OrderDetailDeliveryReview 
Inner Join InvMast on SKU2 = SKU and LocationTypeID=4
GROUP BY Sku2 , Description
ORDER BY Sku2 

Set @column_list = Left(@column_list,Len(@column_list)-1)

Select @column_list

----------------------------------------

1 row is returned:
,SUM(Case When Sku2=157 Then Quantity Else 0 End) As [157 -..., SUM(Case ...

T-SQL 代码正是我想要的,即根据查询的结果生成单个结果,然后将其用于另一个查询。

但是,我无法弄清楚 SELECT @column_list =... 语句如何通过位于 SELECT 语句内将多个值放入单个字符串中。如果没有分配给 @column_listSELECT 语句将仅返回多行。通过在 SELECT 语句中使用变量,如何将结果“扁平化”为一个值?我应该如何阅读此 T-SQL 才能正确理解发生了什么?

I'm just starting to learn T-SQL and could use some help in understanding what's going on in a particular block of code. I modified some code in an answer I received in a previous question, and here is the code in question:

DECLARE @column_list AS varchar(max) 
SELECT @column_list = COALESCE(@column_list, ',') + 
    'SUM(Case When Sku2=' + CONVERT(varchar, Sku2) + 
    ' Then Quantity Else 0 End) As [' + 
    CONVERT(varchar, Sku2) + ' - ' + 
    Convert(varchar,Description) +'],'
FROM OrderDetailDeliveryReview 
Inner Join InvMast on SKU2 = SKU and LocationTypeID=4
GROUP BY Sku2 , Description
ORDER BY Sku2 

Set @column_list = Left(@column_list,Len(@column_list)-1)

Select @column_list

----------------------------------------

1 row is returned:
,SUM(Case When Sku2=157 Then Quantity Else 0 End) As [157 -..., SUM(Case ...

The T-SQL code does exactly what I want, which is to make a single result based on the results of a query, which will then be used in another query.

However, I can't figure out how the SELECT @column_list =... statement is putting multiple values into a single string of characters by being inside a SELECT statement. Without the assignment to @column_list, the SELECT statement would simply return multiple rows. How is it that by having the variable within the SELECT statement that the results get "flattened" down into one value? How should I read this T-SQL to properly understand what's going on?

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

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

发布评论

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

评论(2

夜巴黎 2024-09-04 00:32:58

在 SQL Server 中:

SELECT @var = @var + col
FROM TABLE

实际上连接值。这是一种奇怪的模式(我目前无法找到该功能的文档参考 - 该功能已在 SQL Server 社区中使用多年)。如果 @var 在开头为 NULL(即未初始化的值),那么您需要 COALESCE 或 ISNULL(并且您经常使用分隔符):

SELECT @var = ISNULL(@var, '') + col + '|'
FROM TABLE

或者这样创建一个逗号分隔的列表,然后仅删除前导逗号:

SELECT @var = ISNULL(@var, '') + ',' + col
FROM TABLE

SET @var = STUFF(@var, 1, 1, '')

或 (由 KM 提供,依靠 NULL + ',' 产生 NULL 来消除第一项对 STUFF 的需要在列表中):

SELECT @var = ISNULL(@var + ',', '') + col
FROM TABLE 

或使用此命令创建一个带有前导、分隔和尾随逗号的列表:

SELECT @var = ISNULL(@var, ',') + col + ','
FROM TABLE

In SQL Server:

SELECT @var = @var + col
FROM TABLE

actually concatenates the values. It's a quirks mode (and I am unable at this time to find a reference to the documentation of feature - which has been used for years in the SQL Server community). If @var is NULL at the start (i.e. an uninitialized value), then you need a COALESCE or ISNULL (and you'll often use a separator):

SELECT @var = ISNULL(@var, '') + col + '|'
FROM TABLE

or this to make a comma-separated list, and then remove only the leading comma:

SELECT @var = ISNULL(@var, '') + ',' + col
FROM TABLE

SET @var = STUFF(@var, 1, 1, '')

or (courtesy of KM, relying on NULL + ',' yielding NULL to eliminate the need for STUFF for the first item in the list):

SELECT @var = ISNULL(@var + ',', '') + col
FROM TABLE 

or this to make a list with a leading, separated and trailing comma:

SELECT @var = ISNULL(@var, ',') + col + ','
FROM TABLE
戈亓 2024-09-04 00:32:58

您将需要研究 COALESCE 函数。 此处可以看到一篇描述正在发生的事情的好文章。

You will want to look into the COALESCE function. A good article describing what is happening can be seen here.

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