Facebook Twitter Sign in | Join

			Connect your existing Source Control system to SSMS in 5 minutes
Getting started with SSIS - Part 1: Introduction to SSIS
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.

Jacob's Blog

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Connect your existing Source Control system to SSMS in 5 minutes
Sponsored [Advertise Here]
Learn XSD and XML Schema Collections
beyondrelational.com
This book helps you to learn XML Schema Collections from basic to advanced levels through simple examples and easy to follow walk through labs.

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

SSRS Tutorial - Getting Started with SQL Server Reporting Services
beyondrelational.com
This tutorial will help you to get started with SQL Server Reporting Services (SSRS)
Loading

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

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



Jacob Sebastian
1 · 100% · 16868
1
Liked
4
Comments



Submit

4   Comments   

Subscribe to Notifications
  • 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
    2173 · 0% · 1
  • 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
    23 · 7% · 1233
  • 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
    23 · 7% · 1233
  • @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% · 16868

Your Comment


Sign Up or Login to post a comment.

Copyright © Beyondrelational.com