我一直在尝试连接特定行的值。说我有疑问:
SELECT
tblContacts_2.contactid,
(COALESCE (tblSites_1.sitenm, '')
+ COALESCE (' / ' + tblSites_1.sitenmalt, '')
+ ' ' + COALESCE (tblSites_1.addr1, '') + ' '
+ COALESCE (tblSites_1.zipcodeid, '') ) AS SiteNameAddr,
tblSites_1.siteid
FROM
dbo.tblcontacts AS tblContacts_2
INNER JOIN
dbo.tjncsitecontacts AS tjncSiteContacts_1 ON tblContacts_2.contactid = tjncSiteContacts_1.contactid
INNER JOIN
dbo.tblsites AS tblSites_1 ON tblSites_1.siteid = tjncSiteContacts_1.siteid
此输出为
ContactID SiteNameAddr SiteID
--------------------------------------------------------------
329 TWIN PARKS SOUTHWEST / TWIN PARKS SOUTH... 1
1788 TWIN PARKS SOUTHWEST / TWIN PARKS S.... 1
我想要的是这种格式的输出,其中一个联系人ID的sitenameaddrs如下所示连接在一起,其中包含与之关联的网站ID的数量:
ContactID SiteNameAddr CountSite
-----------------------------------------------------------------------
321 RIVERSIDE PARK / 3333 BROADWAY 10035, URBAN... 8
322 WESTVIEW 625 MAIN ST 10044 1
问题是,执行此操作时,以下代码失败 -
SELECT
tblContacts_2.contactid,
dbo.Removelastchar(dbo.Strconcat(COALESCE (tblSites_1.sitenm, '')
+ COALESCE (' / ' +
tblSites_1.sitenmalt, '')
+ ' ' + COALESCE (tblSites_1.addr1, '')
+ ' '
+ COALESCE (tblSites_1.zipcodeid, '') +
', '))
AS SiteNameAddr,
Count(tblSites_1.siteid) AS CountSite
FROM
dbo.tblcontacts AS tblContacts_2
INNER JOIN
dbo.tjncsitecontacts AS tjncSiteContacts_1 ON tblContacts_2.contactid = tjncSiteContacts_1.contactid
INNER JOIN
dbo.tblsites AS tblSites_1 ON tblSites_1.siteid = tjncSiteContacts_1.siteid
GROUP BY
tblContacts_2.contactid
错误:
执行用户定义时发生.NET Framework错误 例行或聚合" strconcat":
System.Data.SqlServer.TruncationException:尝试转换返回 大小为8086字节的值或输出参数为带有的T-SQL类型 较小的大小限制为8000字节。
这是因为其中一个联系ID有92个与之关联的站点,并且连接的字符串变得太大。此外,dbo.Removelastchar
和dbo.Strconcat
是标量函数,因此它们只能返回nvarchar
,因此无法在其中使用ntext
。数据类型的类型转换也不起作用。
请告诉我这是替代方案。连接字符串由Access
中的VBA代码获取