Hierarchical table sorting with a parent-child relationship

This code snippet works beautifully. Just replace the table/field names like so: 

  • AccountID with ID of your child record
  • name with name of your child record
  • parentID with the parent ID of of your child record
  • @tbl_accounts with the name of your hierarchical table

Run and done:

with cte as
(
select
    Accountid,
    name,
    parentid,
    cast(row_number()over(partition by parentid order by name) as varchar(max)) as [path]
from @tbl_accounts
where parentid = 0
union all
select
    t.AccountID,
    t.name,
    t.ParentID,
    [path] + cast(row_number()over(partition by t.parentid order by t.name) as varchar(max))
from
30.    cte
join @tbl_accounts t on cte.AccountID = t.ParentID
)
 
select 
    Accountid,
    name,
    ParentID,
    [path]
from cte
order by path

 

Thanks, Kev Riley!

http://ask.sqlservercentral.com/questions/48518/hierarchical-table-sorting-with-a-parent-child-rel.html

What to do when VS database project is missing master.dacpac file

http://www.brandonmartinez.com/2013/03/22/teamcity-and-ssdt-resolve-a-master-dacpac-missing-file-exception/

To fix this issue, copy files from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\SQLDB\Extensions\SqlServer\110\SQLSchemas to the path that VS is using to look for it.

 

Prevent saving changes that require table re-creation

I love this little tip because I prefer to edit a sql db table through the GUI if possible. (I know, I know. Leave me alone.)

Pedro’s answer is short and sweet:

Go into Tools -> Options -> Designers-> Uncheck “Prevent saving changes that require table re-creation”. Voila.

That happens because sometimes it is necessary to drop and recreate a table in order to change something. This can take a while, since all data must be copied to a temp table and then re-inserted in the new table. Since SQL Server by default doesn’t trust you, you need to say “OK, I know what I’m doing, now let me do my work.”

http://stackoverflow.com/questions/1969096/saving-changes-after-table-edit-in-sql-server-management-studio