【问题标题】:Sql Query to join table and coalesce values用于连接表和合并值的 Sql Query
【发布时间】: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


【解决方案1】:

这是您的查询:

 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
        ),
        (  varchar(1000)  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;

这有多个明显的语法错误:

  • 您不能在子查询中设置变量值。事实上,一个查询不能既返回行又设置变量值。
  • varchar(1000) 只是在那里闲逛,就好像您正在尝试用 C 或其他东西进行强制转换。

那么你还有额外的逻辑错误:

  • 在同一语句中分配和使用变量。
  • 返回多于一行的子查询。
  • 看似相关的子查询缺乏相关性。

您似乎想从联系人那里获取事物列表。这在 SQL Server 中是一个难题,但也许这会让您走上正确的道路:

 select a.*, b.tfunction, b.SSummary, b.ContactType,
        stuff((select ', ', c.ContactName
               from contacts c
               where c.pid = b.pid
               for xml path ('')
              ), 1, 2, '') as Contacts,
        stuff((Select ', ' + c.Address
                from contacts c
                where c.pid = b.pid
                for xml path ('')
              ), 1, 2, '') as Addresses,
         . . .
from contracts a join
     functions b
     on a.pid = b.pid;

【讨论】:

    【解决方案2】:

    问题可能是您的 INNER JOIN 必须在 FUNCTIONS 中有一行才能看到任何结果。

    如果您希望 CONTACTS 中的行在 FUNCTIONS 中的行不匹配时也显示,请将 INNER JOIN 替换为 LEFT JOIN。

    Select  
        @ContactName = COALESCE(@ContactName + ', ', '') + c.ContactName 
    from contacts  c 
    LEFT join  functions b on c.pid= b.pid
    

    【讨论】:

    • 即使我用左连接替换相同的问题。查询未执行。给出以下错误 - Msg 102, Level 15, State 1, Line 12 '=' 附近的语法不正确。消息 102,级别 15,状态 1,第 13 行 ',' 附近的语法不正确。消息 102,级别 15,状态 1,第 14 行 ',' 附近的语法不正确。消息 102,级别 15,状态 1,第 15 行 ')' 附近的语法不正确。
    • 您是否声明了@ContactName?例如声明@ContactName varchar(max);设置@ContactName=''
    • 我做了,堆栈溢出不允许我添加那个 sn-p。
    • 是的,重新检查并再次检查并复制/粘贴了声明的属性。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2012-04-24
    • 2023-02-14
    相关资源
    最近更新 更多