0

您好我正在尝试执行以下查询,但没有运气:

SELECT AA.Id, AA.ObjId, AA.NsId, AA.statusId, AA.LCID, AA.Title, AA.MessageBody_ClearText, AA.MessageBody_HTMLText, 
               AA.File1, AA.File2, AA.File3, AA.Status, AA.name, AA.createdate, AA.updatedate, AA.boneid, AA.main, AA.Sort, 
               BB.ID AS Expr1, BB.MainIDNum, BB.MessageID, BB.Message1, BB.Message2, BB.Message3, BB.MessageDate, 
               BB.AttachmentGUID1, BB.AttachmentGUID2, CC.MessageID AS Expr2, CC.Identification, CC.Viewed, CC.Deleted, CC.Id AS Expr3, 
               AA.FileName1, AA.FileName2, AA.FileName3
FROM  dbo.gnvw_ObjectsBones AS AA INNER JOIN
              --- XXX.dbo.MESSAGES AS BB ON BB.MessageID = AA.ObjId LEFT OUTER JOIN ---
               dbo.PersonalMessages AS CC ON CC.MessageID = BB.MessageID AND CC.Identification = BB.MainIDNum

标记的行需要更改为函数调用,我有一个名为“GetMessages”的函数,它接受一个int“输入”,它必须是那个:“CC.Identification”,所以我试图替换它变成这样:

SELECT AA.Id, AA.ObjId, AA.NsId, AA.statusId, AA.LCID, AA.Title, AA.MessageBody_ClearText, AA.MessageBody_HTMLText, 
               AA.File1, AA.File2, AA.File3, AA.Status, AA.name, AA.createdate, AA.updatedate, AA.boneid, AA.main, AA.Sort, 
               BB.ID AS Expr1, BB.MainIDNum, BB.MessageID, BB.Message1, BB.Message2, BB.Message3, BB.MessageDate, 
               BB.AttachmentGUID1, BB.AttachmentGUID2, CC.MessageID AS Expr2, CC.Identification, CC.Viewed, CC.Deleted, CC.Id AS Expr3, 
               AA.FileName1, AA.FileName2, AA.FileName3
FROM  dbo.gnvw_Data_PersonalMessages_ObjectsBones AS AA INNER JOIN
               XXX.dbo.GetMessages(CC.Identification) AS BB ON BB.MessageID = AA.ObjId LEFT OUTER JOIN
               dbo.PersonalMessages AS CC ON CC.MessageID = BB.MessageID AND CC.Identification = BB.MainIDNum

我也尝试了其他解决方案,但都没有奏效,这里有人看到问题了吗?这是我从运行该“解决方案”中得到的错误:

'无法绑定多部分标识符“CC.Identification”。'

和,

'无效的列名'ID'。' SQL 不是我的强项,我很乐意听到任何解决方案,希望我解释正确。

4

1 回答 1

1

当需要加入表值函数时,分别使用CROSS APPLY/OUTER APPLY而不是INNER/LEFT加入。

...
FROM  dbo.gnvw_Data_PersonalMessages_ObjectsBones AS AA 

CROSS APPLY XXX.dbo.GetMessages(CC.Identification) AS BB
....
WHERE BB.MessageID = AA.ObjID

更新 我刚刚注意到连接条件 fordbo.AgamUsersPersonalMessages AS CC仅指BB,而连接条件 forBB仅取决于CC. 这是不正确的,你仍然会得到错误。不知道你想得到什么,我无法给你解决方案;你需要自己弄清楚加入条件......

于 2013-04-15T14:21:31.703 回答