相关疑难解决方法(0)

连接/聚合字符串的最佳方式

我正在寻找一种方法将不同行的字符串聚合成一行.我想在很多不同的地方做这个,所以有一个功能来促进这将是很好的.我尝试使用COALESCE和的解决方案FOR XML,但他们只是不为我削减它.

字符串聚合会执行以下操作:

id | Name                    Result: id | Names
-- - ----                            -- - -----
1  | Matt                            1  | Matt, Rocks
1  | Rocks                           2  | Stylus
2  | Stylus
Run Code Online (Sandbox Code Playgroud)

我已经看过CLR定义的聚合函数作为替代COALESCEFOR XML,但显然SQL Azure 支持CLR定义的东西,这对我来说很痛苦因为我知道能够使用它会解决很多问题.我的问题.

是否有任何可能的解决方法,或类似的最佳方法(可能不如CLR那样最优,但嘿,我会采取我能得到的)我可以用来聚合我的东西?

sql sql-server aggregate-functions azure azure-sql-database

91
推荐指数
6
解决办法
13万
查看次数

nvarchar concatenation/index/nvarchar(max)莫名其妙的行为

我今天在SQL Server(2008R2和2012)中遇到了一个非常奇怪的问题.我正在尝试使用串联和select语句来构建字符串.

我找到了解决方法,但我真的很想了解这里发生了什么以及为什么它没有给我预期的结果.有人可以向我解释一下吗?

http://sqlfiddle.com/#!6/7438a/1

根据要求,这里的代码也是:

-- base table
create table bla (
    [id] int identity(1,1) primary key,
    [priority] int,
    [msg] nvarchar(max),
    [autofix] bit
)

-- table without primary key on id column
create table bla2 (
    [id] int identity(1,1),
    [priority] int,
    [msg] nvarchar(max),
    [autofix] bit
)

-- table with nvarchar(1000) instead of max
create table bla3 (
    [id] int identity(1,1) primary key,
    [priority] int,
    [msg] nvarchar(1000),
    [autofix] bit
)

-- fill the three tables with the same values …
Run Code Online (Sandbox Code Playgroud)

sql-server nvarchar string-concatenation sql-server-2012

12
推荐指数
1
解决办法
4765
查看次数