[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

HELP with SQL

Posted on 2013-12-26
3
Medium Priority
?
319 Views
Last Modified: 2014-01-07
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
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:CvD
ID: 39740849
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 39741780
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
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 39764177
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question