开发者

Visual Studio 2010 and Query Designer SQL Problem with XML Path

I have an awesome Visual Studio 2010 SQL frustration:

I found code on StackOverflow on how to concatenate strings from multiple rows onto a single row by using FOR XML PATH('').

When I write the following line in Query and View Designer (because, presumably, there is no other way to write SQL in Visual Studio and then save it as a "View"), I get results that are not what I want:

Code:

Names AS
(SELECT DISTINCT 
  Z.code,
  (SELECT DISTINCT CAST(B.fname开发者_如何学Go + ' ' + B.lname + ' ' AS VARCHAR(MAX))
   FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id
   WHERE (A.code = Z.code) FOR XML PATH('')) AS EmployeeNames
 FROM Employees AS Z)

I get a result like:

Code  EmployeeNames
----  -------------------------
1234  <Expr1>First Last</Expr1>

Why? Because Visual Studio 2010, in all it's glory, adds "as Expr1" to the following:

(SELECT DISTINCT CAST(B.fname + ' ' + B.lname + ' ' AS VARCHAR(MAX))
 FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id
 WHERE (A.code = Z.code) FOR XML PATH('')) *as Expr1*

Is there any work-around for this? I've noticed that the problem comes with the Query and View Designer Parser... and I can intentionally break the parser by putting a random function that the parser doesn't support (like OVER()) somewhere in my code. This fixes the problem, and removes the XML tags, but seems unnecessary.

Does anyone else have this problem? Does anyone know a work-around?

Thank you very much for your time and effort with my problem.


I ran into something simular and the fix for me was to define the Cast in each case.

Example:

Names AS (SELECT DISTINCT Z.code, (SELECT DISTINCT CAST(B.fname AS varchar(10) + ' ' + CAST(B.lname AS varchar(10) + ' ') FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id WHERE (A.code = Z.code) FOR XML PATH('')) AS EmployeeNames FROM Employees AS Z)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜