Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
XQuery 69
TSQL 67
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
August 2009 19
June 2009 19
May 2010 18
January 2009 15
July 2008 15
January 2010 14
October 2008 14
February 2010 12

XQuery Lab 48 - Sorting Query files in SQL Server Management Studio (SSMS) Solution/Project

Aug 22 2009 2:36PM by Jacob Sebastian   

I was working on my presentation for 24-hour-pass event on 2nd September and came across the sorting issue with the solution explorer. I have a number of script demos and I wanted to organize them by name. No matter, what I did, the solution explorer continued to display the script files in its own order, and not in the way I wanted them.

My Frustration increased when I opened the project XML file and found that the XML element is set to be “sorted”, but the sorting flag has no effect in the way SSMS displayed the items.

<LogicalFolder Name="Queries" Type="0" Sorted="true">
 
Though the “Sorted” attribute is set to “true”, no sorting took effect in the SSMS. As a quick work around, I opened the “.ssmssqlproj” XML file in an XML editor and modified the physical order of the elements in the project file. I saved the file and after I reopened the project, the query files were displayed in the correct order.
 
I felt so bad about this behavior and thought of writing a script that can automate this process the next time I need this. So I wrote a stored procedure that loads the content of the project file and order it and outputs a file with the sorted items.
 
Here is the content of the stored procedure that performs this.
 
CREATE PROCEDURE SortSSMSProjectFiles
(
@ProjectFileName VARCHAR(512)
)
AS

DECLARE @x XML, @qry NVARCHAR(500), @param NVARCHAR(100)
SELECT @qry = N'
SELECT
@x = CAST(bulkcolumn AS XML)
FROM OPENROWSET(BULK '
'' + @ProjectFileName + ''',
SINGLE_BLOB) AS x'

PRINT @qry
SELECT @param = '@x XML OUTPUT'
EXECUTE sp_executesql @qry, @param, @x OUTPUT

SELECT @x.query('
<SqlWorkbenchSqlProject
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Name="{SqlWorkbenchSqlProject/@Name}">
<Items>
{
for $lf in SqlWorkbenchSqlProject/Items/LogicalFolder
return
if ($lf/@Name = "Queries")
then
<LogicalFolder Name="{$lf/@Name}"
Type="{$lf/@Type}" Sorted="{$lf/@Sorted}">
<Items>
{
for $i in $lf/Items/*
order by $i/@Name
return $i
}
</Items>
</LogicalFolder>
else $lf
}
</Items>
</SqlWorkbenchSqlProject>
'
)

This is how you can execute this stored procedure

EXECUTE SortSSMSProjectFiles
@ProjectFileName = 'C:\temp\demo\demo\demo.ssmssqlproj'

The @ProjectFileName should point to the “.ssmssqlproj” file of your project. This stored procedure will produce an XML document. You can open it in SSMS and use “File->save as” menu to overwrite your existing project  file.

If you want to completely automate this, you can use osql.exe or sqlcmd.exe to execute this stored procedure and generate an output file in the desired location.

To demonstrate this problem, I created a demo project and added some files in random order. Here is how SSMS displays my files now.

ssms1

I wanted to get them organized and I ran the project file through my stored procedure. The stored procedure updated the project file and here is how the files are displayed in SSMS after the change.

ssms2

I tested it with my projects and it works. I would like to hear your comments and suggestions on this script.

Next Lab: XQuery Lab 49 – Deleting rows from a table based on the data in an XML document

Previous Lab: XQuery Lab 47 – Generating HTML table from XML Data

View all labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials


Tags: XQuery-Functions, XQuery-Labs, XML, XQuery, SSMS, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 22473
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • Jacob - This is really cool... and exactly what I was looking for. The only consideration I ran into (and it wasn't a big deal) was that I am using source control so I had to update the procedure to make sure that the source control binding information stayed intact. Thanks again! Todd

    commented on Mar 16 2010 2:27PM  .  Report Abuse This post is not formatted correctly
    Todd
    2298 · 0% · 2
  • How can I view only particular tables in SSMS? Say, I have tables that start with rs and I only want to see them.

    commented on Jun 29 2010 6:02PM  .  Report Abuse This post is not formatted correctly
    Naomi
    30 · 6% · 1381
  • See also http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/049f30ac-6c62-4cde-821c-91cb3494b870

    commented on Jun 29 2010 6:03PM  .  Report Abuse This post is not formatted correctly
    Naomi
    30 · 6% · 1381
  • @Naomi, You can add filters in SSMS. Right click on the 'tables' node and select 'filters' (or you can use the tool bar button). The filter window allows you to filter the items that you see in the list.

    commented on Jun 30 2010 1:58AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22473

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising