【发布时间】:2014-06-26 09:54:47
【问题描述】:
我是 sql 的新手,我正在尝试连接两个表并合并值。 下面的 Coalesce 代码在单独运行时有效
Select @ContactName = COALESCE(@ContactName + ', ', '') + c.ContactName from contacts c inner join functions b on c.pid= b.pid
但是当我合并整个代码时,它没有执行。有人可以建议在下面。
select
a.*,
b.tfunction,
b.SSummary,
b.ContactType,
( Select @ContactName = COALESCE(@ContactName + ', ', '') + c.ContactName from contacts c inner join functions b on c.pid = b.pid),
( Select @Address = COALESCE(@Address + ', ', '') + c.Address from contacts c inner join functions b on c.pid = b.pid),
( Select @ContactPhone = COALESCE(@ContactPhone + ', ', '') + c.ContactPhone from contacts c inner join functions b on c.pid = b.pid),
( Select @ContactEmail = COALESCE(@ContactEmail + ', ', '') + c.ContactEmail from contacts c inner join functions b on c.pid = b.pid)
from contracts a
inner join functions b on a.pid = b.pid
错误详情
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ')'.
ps:- 随意修改代码。
PS- 我有所有 4 个属性的声明变量 - 由于某种原因,堆栈溢出不允许我粘贴它们。
【问题讨论】:
-
有什么问题?您是否收到错误消息?它没有返回你所期望的吗?请更具描述性。
its not executing没有太多关于您的问题的信息。 -
错误详情 Msg 102, Level 15, State 1, Line 12 '=' 附近的语法不正确。消息 102,级别 15,状态 1,第 13 行 ',' 附近的语法不正确。消息 102,级别 15,状态 1,第 14 行 ',' 附近的语法不正确。消息 102,级别 15,状态 1,第 15 行 ')' 附近的语法不正确。
-
基本上查询没有执行。
-
d.ContactType 无效。您只声明了带有“a”和“b”的表
-
错字 - 将更新设为 b.contacttype
标签: sql sql-server plsql plsqldeveloper