SQL: How to include an incrementing column in your query without a cursor or loop

I had the need to do a recursive call that included depth and sort orders. I wanted to reformat the sort orders in a single query without using a cursor or loop.

Turns out there is an easy way with SQL 2005 or higher.

@Folders is a table var from my CTE with Depth and SortOrder in it.

SELECT ID,(ROW_NUMBER() OVER (ORDER BY f.Depth,f.SortOrder,r.SortOrder)) * 100 as SortOrder
FROM someTable r
JOIN @Folders f
ON r.FolderID = f.ID
ORDER BY SortOrder;

Happy Coding!

I can’t drop a database user: The user owns one or more databases

This was frustrating me a bit, as I wish SQL Management Studio would just prompt me to remove myself as owner from those db’s and let me drop. Nope. It doesn’t.

So, what to do? Well, I used management studio to select another one of my accounts. I get the awesome message of “The proposed new database owner is already a user or aliased in the database.”. So, no go there.

I then realized I never mapped SA to these db. Nice and simple. Assign each db owner to SA, then your done.

Or run this script and keep changing the db name. Probably could automate this more by querying the db’s and looping.


alter authorization on DATABASE::[MyDBName] to [SA]

Hope this helps someone.

Happy Coding!

How to fix SqlError: FILESTREAM feature is disabled.

I was trying to restore a database and got the error “System.Data.SqlClient.SqlError: FILESTREAM feature is disabled. (Microsoft.SqlServer.Smo) from the Restore Wizard.

This baffled me a bit as I know I went into the DB Server setting in Sql Manager and specifically set that value to Allow it. Well, after smacking the computer around a bit, she finally confessed that there is another place you have to enable that setting. Go figure.

You need to go to the Sql Configuration Manager (Not SQL Server Management Studio) and also enable the settings.


Here is the original error:


Here is me checking the settings on the DB:

Look, I think it’s set:



But really, no. Check the Running Values to see it laugh at you.



So, resolution? Go to Sql Configuration Manager. Open the properties window of your Server.


Check the boxes. Hit OK. Now right click your server instance and hit Restart. Watch the progress bar and ponder great thoughts.

Now, go to SQL Management Studio.

Connect to your server and run this simple command.

EXEC sp_configure filestream_access_level, 2



Now restart your Sql instance once more. You are done. Repeat whatever miserable operation you were doing before you got this error and repeat to yourself. “When I design software, I will never make someone set an option in two programs ever….ever….never ever…”


Happy Coding!

SQL Script to extract address information into individual fields

I had a need to write a script or a program that would extract an address that is stored in one field and break it out into its parts. Address, Address2, City, State and Postal Code(zip code).

It’s a real bugger to do in a single SQL statement, I had one but it was really really ugly. I mean, wing man, take one for the team, dating the hot girls friend kind of ugly.

So, I made this simple script that you can either run outright to convert your data or you can easily modify this into a procedure.


I created the new fields on the original table and I also added a field I called SiteAddressBackup. I did an update into this field before running this script in order run against real live data and still have it safe and sound if I mucked things up. I suggest you do the same to preserve your data.

The script. Should be easy to follow.

--Seekford script to extract addresses
--Tested on 2100 real addresses from early production db
--1234 GREENVIEW DR SW,SUITE # 222,ROCHESTER, MN 55902-1080
--1555 SAM NUNN BLVD,PERRY, GA 31069-1954
DECLARE AddressUpdate CURSOR FOR         
SELECT MyOrdersID,SIteAddress
FROM tMyOrders
DECLARE @SiteAddress VARCHAR(255)        

update tMyOrders set SiteAddress = SiteAddressBackup
OPEN AddressUpdate        
FETCH NEXT FROM AddressUpdate         
INTO @ID, @SiteAddress
WHILE @@FETCH_STATUS = 0        

declare @Addr as varchar(100)
declare @Addr2 as varchar(100)
declare @City as varchar(100)
declare @State as varchar(100)
declare @Postal as varchar(100)
declare @Temp as varchar(100)

--extract the postal and state
set @Temp = rtrim(ltrim(SUBSTRING(@SiteAddress,( LEN(@SiteAddress) - CHARINDEX(',', REVERSE(@SiteAddress), 1)) +2 ,100)))
set @Postal = SUBSTRING(@Temp,( LEN(@Temp) - CHARINDEX(' ', REVERSE(@Temp), 1)) +2 ,100)
set @State = SUBSTRING(rtrim(ltrim(@Temp)),0,( LEN(@Temp) - CHARINDEX(' ', REVERSE(@Temp), 1))+1)

--extract the city )pulls everything but the state and zip
set @Temp = SUBSTRING(@SiteAddress,0,( LEN(@SiteAddress) - CHARINDEX(',', REVERSE(@SiteAddress), 1))+1  )

set @City = SUBSTRING(@Temp,( LEN(@Temp) - CHARINDEX(',', REVERSE(@Temp), 1)) +2 ,100)

set @Temp = SUBSTRING(@temp,0,len(@temp) - len(@city) )
set @Addr2 = SUBSTRING(@Temp,( LEN(@Temp) - CHARINDEX(',', REVERSE(@Temp), 1)) +2 ,100)

if(LEN(@Addr2) =0) 
set @Addr = SUBSTRING(@temp,0,len(@temp) - len(@Addr2)+1 )
set @Addr = SUBSTRING(@temp,0,len(@temp) - len(@Addr2) )

update tMyOrders set SiteAddress = @Addr,siteaddress2=@Addr2,SiteCity = @City,SiteState = @State , SitePostalCode = @Postal
where MyOrdersID = @ID 
FETCH NEXT FROM AddressUpdate         
INTO @ID, @SiteAddress
CLOSE AddressUpdate        
DEALLOCATE AddressUpdate   

--Query to show them broken out against the original
SELECT [MyOrdersID]      ,
SiteAddress,SiteAddress2,SiteCity,SiteState,SitePostalCode  ,siteaddressbackup   
  FROM [tMyOrders]

EntityDataSource and Relationships, why oh why did you not bring along my relationship?

I spent about 3 hours cursing up a storm over some data binding I was doing using the EntityDataSource against my Entity Framework context. I was feeling lazy and using the declarative data sources to bind against my grid for some automatic operations.  So, should be easy right? Slap on the data source, bind to the grid, add the other data source for the lookups and bind that to the column.

So why was my column always showing the wrong value? Hmm….It was always the first value in the list? hmm…So I thought my grid was defective and went down the route of beating it up. The kicker was I could edit the record, and the newly set value would go into the database. It would never reflect on the grid though. This was rediculously annoying.

I finally gave up and created a new data source and ran my own Linq on the context to pull the objects. Thats when the Ah HA! moment came. I typed my INCLUDE off the context to pull the relationships in.

WAIT! You tell me. Wasn’t that on the data source setup wizard screen when you said pick all the properties? NO. No it isn’t. All the relationships are missing as selectable items on that screen. I guess the EntityDataSource is not usable in these scenarios then, right?

Wrong. After banging your head on the wall a few more times, you gingerly go to the properties window of the EntityDataSource object you created. You delicately go to the Include property and type in the name of the property.

What a pile of crap! I spend 3 hours and multiple rewrites to figure out I could have typed the 8 characters in on the property window and been good to go.

Well, it all works now. I just wish that option would have been in the wizard. If I am going the lazy route with declarative bindings, I want to be able to go full lazy.

Update a table from a Select query

I had to update a table with missing product id’s using the item master table to do the correlation. This led to having to formulate a lovely SQL query that would let me update the table based on matching criteria. In effect, I knew the SKU in both records should match, so I could correlate them via that data point. This SQL can easily be adapted to work with your scenario. I used this against a SQL Server 2005 database.

update torderdetails set torderdetails.productid = tproducts.productid
from torderdetails join tproducts on tproducts.partnumber = torderdetails.partnumber where torderdetails.productid =0

Hopefully this helps someone out their with the headaches of these rare tasks.

How to delete those pesky duplicate SQL records…

So every once in a while I find that I have some duplicate records in my database as a result of testing or development or plain oversight. How to remedy the situation?

Well, its as easy as this simple SQL.

delete sourceTable
from YOURTABLENAME sourceTable, YOURTABLENAME compareTable
where sourceTable.fieldWithDuplicatesToMatch= compareTable.fieldWithDuplicatesToMatch
and sourceTable.PrimaryKey > compareTable.PrimaryKey

Basically, replace YOURTABLENAME with the name of your table.

Replace fieldWithDuplicatesToMatch with the field on the table that has the duplicates. Such as SKU or Email.

Replace the PrimaryKey with the table PrimaryKey or a time stamp (DateTime) field or whatever you want to use as the basis for which record to keep.

Voila! Duplicates be gone.

.NET Illuminator v1 Alpha Released to the wild

So….I finally decided to release my alpha version to all you hooligans who want to see whats under the hood of fellow .NET’ers applications. You can use .NET Illuminator to see all the UI objects in another .NET application and even manipulate them. Yes, pretend like you are in Visual Studios Form Designer and just start viewing and changing properties.

You can also view all connection strings the application has used to any SQL  servers.

I will post more when I add more.  Happy hacking!

Click here for .NET Illuminator Page

Let’s all jump into the pool..The SQL Connection pool that is..

I was out swimming in the data pool and I started thinking to myself, how many people are sharing the same pool without me even knowing they are there? How did I even get into the pool? And why is the water so cold?

Well, if you are using ADO.NET with SqlConnection , you are playing in the pool to. ADO.NET is a pretty smart animal. It realizes that if you are going to be connecting to a database a lot, then its probably a good idea to keep the connection to the database server open awhile. It does this behind the scenes, so when you create multiple data connections, it only really grabs one from the available pool of connections and uses those.

So….with that in mind, I started thinking about how I can tell what data connections an application has used and who is swimming in my pool. Coupling that with the brainstorming involved in .NET Illuminator, I came up with a very nice and simple piece of code that grabs all the connection strings from the Pool and throws them in rafts for you to see.

Why might this be handy for .NET Illuminator, well…Its always interesting to see what data connections an app has made and what the connection string is for them 🙂

Here is my simplistic example of using C# with .NET to walk the SQL pooled connections and return their connection strings.

         /// <summary>
        /// Loads all pooled connection strings.
        /// </summary>
        /// <returns></returns>
        public static List<string> LoadAllPooledConnectionStrings()
                SqlConnection connection = new SqlConnection();

                FieldInfo connectionFactory = connection.GetType().GetField("_connectionFactory", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Static);

                object cFactory = connectionFactory.GetValue(connection);
                FieldInfo connectionPoolDictionary = connectionFactory.FieldType.GetField("_connectionPoolGroups", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);

                object dictPools = connectionPoolDictionary.GetValue(cFactory);

                Type poolDictionaryType = dictPools.GetType();
                PropertyInfo pooledKeys = poolDictionaryType.GetProperty("Keys", BindingFlags.Public | BindingFlags.Instance);

                object keys = pooledKeys.GetValue(dictPools, null);

                List<string> allKeys = new List<string>();
                foreach (object key in (IEnumerable)keys)
                return allKeys;
            }catch(Exception err)
                throw err;

So…What did we learn? Just because you don’t see the pool….smell the chlorine…or even get wet, you open a data connection and you have jumped all the way in.. and left your stench behind in the water..

Happy swimming!