这段 T-SQL 代码中发生了什么? (连接 SELECT 语句的结果)
我刚刚开始学习 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_list
,SELECT
语句将仅返回多行。通过在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL Server 中:
实际上连接值。这是一种奇怪的模式(我目前无法找到该功能的文档参考 - 该功能已在 SQL Server 社区中使用多年)。如果 @var 在开头为 NULL(即未初始化的值),那么您需要 COALESCE 或 ISNULL(并且您经常使用分隔符):
或者这样创建一个逗号分隔的列表,然后仅删除前导逗号:
或 (由 KM 提供,依靠 NULL + ',' 产生 NULL 来消除第一项对 STUFF 的需要在列表中):
或使用此命令创建一个带有前导、分隔和尾随逗号的列表:
In SQL Server:
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):
or this to make a comma-separated list, and then remove only the leading comma:
or (courtesy of KM, relying on NULL + ',' yielding NULL to eliminate the need for STUFF for the first item in the list):
or this to make a list with a leading, separated and trailing comma:
您将需要研究 COALESCE 函数。 此处可以看到一篇描述正在发生的事情的好文章。
You will want to look into the COALESCE function. A good article describing what is happening can be seen here.