Nested INNER JOIN with bcp

时光总嘲笑我的痴心妄想 提交于 2019-12-12 03:44:21

问题


So this query works fine without the nested SELECT/INNER JOIN. Not sure what is wrong with the nested SELECT, any ideas?

So can't seem to get it to work in conjunction with bcp

SELECT @SQLCmd = + 'bcp ' + 
                         '"SELECT ''<?xml version=""1.0"" encoding=""UTF-8""?>'' + ' + 

                         ' (SELECT CardId, Initials, firstname, lastname ' +
                         '      (SELECT CardId, SetVal ' +
                         '      FROM Business_data as bd ' +
                         '      INNER JOIN Business_set as bs on bd.SetVal=bs.id ' +
                         '      WHERE bd.CardID=ic.CardID ' +
                         '      FOR XML PATH(''BD''), TYPE ' +
                         '      ) ' +
                         ' FROM IndexCards as ic' + 
                         ' FOR XML PATH(''Employee''), ELEMENTS,  ROOT(''Employees'')) "' +
                         ' queryout '  +
                   @FileName +
                   ' -w -T -S' + @@SERVERNAME

These are the tables that i'm using

Indexcards

CardId  | Initials  | firstname | lastname  |
1       | AH        | Ash       | Hart      |
2       | AL        | Alex      | Lang      |

Business_set

ID  | Val     |
1   | Media   |
2   | Tech    |

Business_data

CardId  | SetVal  | 
1       | 1       |
2       | 1       |
2       | 2       |

回答1:


From your last question I take, that this is in database Employees.dbo. I think, that you should either fully qualify all your table's names of - better - place an USE Employees; before your select.

And I think, that Dani Mathew is right, that there is a comma missing. The sub-select is - seen form the main select - just a column to inlcude in the output.

Try it like this:

SELECT @SQLCmd = + 'bcp ' + 
                        '"USE Employees; SELECT ''<?xml version=""1.0"" encoding=""UTF-8""?>'' + ' + 

                        ' (SELECT CardId, Initials, firstname, lastname, ' +
                        '      (SELECT CardId, SetVal ' +
                        '      FROM dbo.Business_data as bd ' +
                        '      INNER JOIN dbo.Business_set as bs on bd.SetVal=bs.id ' +
                        '      WHERE bd.CardID=ic.CardID ' +
                        '      FOR XML PATH(''BD''), TYPE ' +
                        '      ) ' +
                        ' FROM dbo.IndexCards as ic' + 
                        ' FOR XML PATH(''Employee''), ELEMENTS,  ROOT(''Employees'')) "' +
                        ' queryout '  +
                @FileName +
                ' -w -T -S' + @@SERVERNAME


来源:https://stackoverflow.com/questions/37855004/nested-inner-join-with-bcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!