Solved

HELP with SQL

Posted on 2013-12-26
3
295 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
3 Comments
 
LVL 3

Expert Comment

by:CvD
ID: 39740849
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BULK LOGGED - log full 9 25
Filtered index 5 56
Insert Salary Period that has 2 months 11 25
My Query is not giving correct result. Please help 5 32
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now