?
Solved

HELP with SQL

Posted on 2013-12-26
3
Medium Priority
?
316 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
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…

777 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