简体   繁体   English

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

[英]What is happening in this T-SQL code? (Concatenting the results of a SELECT statement)

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. 我刚刚开始学习T-SQL,可以使用一些帮助来理解特定代码块中发生的事情。 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. T-SQL代码完全符合我的要求,即根据查询结果生成单个结果,然后将在另一个查询中使用。

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. 但是,我想不出如何SELECT @column_list =...语句把多个值到一个字符的字符串通过是一个内部SELECT语句。 Without the assignment to @column_list , the SELECT statement would simply return multiple rows. 如果没有赋值给@column_listSELECT语句将只返回多行。 How is it that by having the variable within the SELECT statement that the results get "flattened" down into one value? 如何通过在SELECT语句中使用变量将结果“平化”为一个值? How should I read this T-SQL to properly understand what's going on? 我该如何阅读此T-SQL以正确理解发生了什么?

In SQL Server: 在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). 这是一种怪癖模式(我现在无法找到对功能文档的引用 - 这已经在SQL Server社区中使用了多年)。 If @var is NULL at the start (ie an uninitialized value), then you need a COALESCE or ISNULL (and you'll often use a separator): 如果@var在开始时为NULL(即未初始化的值),则需要COALESCE或ISNULL(并且您经常使用分隔符):

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): 或者(由KM提供 ,依赖于NULL +','产生NULL以消除列表中第一项的STUFF需求):

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

You will want to look into the COALESCE function. 您将需要查看COALESCE功能。 A good article describing what is happening can be seen here . 这里可以看到一篇描述正在发生的事情的好文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM