[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

HELP with SQL

I have this SQL.   'ScottPletcher' answered a previous question of mine that gave me this.  If you see this post, this SQL works fine in a VIEW.    What/how does "FOR XML PATH('')) AS" do?    I want to understand how SQL this is working.

Also, although the view works, whenever I open it in design mode, I get an error related to the WHERE clause, which I'm assuming is related to the FOR XML PATH('')) .

Thanks.


SELECT FR.ID, REPLACE(SUBSTRING(CAST
                   ((SELECT CAST('~' + 'Restriction: ' + CONVERT(varchar(10), RES.create_date, 101) + ' - ' + RES.notes AS varchar(8000))
                     FROM   dbo.tds_grant_funding_request_restriction RES
                     WHERE res.FK_funding_requestID = FR.ID FOR XML PATH('')) AS varchar(8000)), 2, 8000), '~', CHAR(13) + CHAR(10)) AS Restrictions
FROM  dbo.tds_grant_funding_request FR
0
HLRosenberger
Asked:
HLRosenberger
1 Solution
 
CvDCommented:
0
 
Kevin CrossChief Technology OfficerCommented:
Yes, I do not believe you can visualize a FOR XML query in design mode.  I typically will right-click the view and use the script as options.  You can then edit the SQL to alter the view from a SQL query window.

The above Technet link should be helpful in understanding FOR XML; however, I will add some additional insight into the XML trick Scott leveraged here.

As you will read, FOR XML PATH is one of the methods for converting a result set into XML.  With no column name, the value of each row becomes an XML text node.  Therefore, the basic part of the code is concatenating the creation date in each row, so you have this: '~Restriction: 12/27/2013'.  By default, this may come up as <row>~Restriction: 12/27/2013</row><row>...</row>.  

For example, using AdventureWorks:
SELECT TOP 3 '~Restriction: '+CONVERT(VARCHAR(10), DueDate, 101)
FROM Production.WorkOrder
FOR XML PATH
;

Open in new window


Results in:
<row>~Restriction: 07/15/2005</row>
<row>~Restriction: 07/15/2005</row>
<row>~Restriction: 07/15/2005</row>

Open in new window


By setting the PATH, you control the element/node name for each row.  Hence, if you set this value to '', you will get a string of values with no separation.

~Restriction: 07/15/2005~Restriction: 07/15/2005~Restriction: 07/15/2005

Open in new window


Note the extra '~' at the front.  To remove it, the outer code does a SUBSTRING from character 2 to the end of the string.  From there, it replaces '~' with the character codes for carriage return and line feed, so you end up with:

Restriction: 07/15/2005
Restriction: 07/15/2005
Restriction: 07/15/2005

Open in new window


I hope that helps!
0
 
HLRosenbergerAuthor Commented:
thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now