Solved

HELP with SQL

Posted on 2013-12-26
3
291 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
In this article I will describe the Backup & Restore 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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

14 Experts available now in Live!

Get 1:1 Help Now