从视图中调用函数(在其他数据库上)

时间:2013-04-15 14:14:38

标签: sql-server-2008

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

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'input'并且它必须是那个:'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不是我强有力的一面,我很乐意听到任何解决方案,希望我解释得对。

1 个答案:

答案 0 :(得分: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

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