How to combine GROUP BY and ROW_NUMBER?

Wow, the other answers look complex – so I’m hoping I’ve not missed something obvious. You can use OVER/PARTITION BY against aggregates, and they’ll then do grouping/aggregating without a GROUP BY clause. So I just modified your query to: select T2.ID AS T2ID ,T2.Name as T2Name ,T2.Orders ,T1.ID AS T1ID ,T1.Name As T1Name ,T1Sum.Price FROM … Read more

Check if Database Exists Before Creating

As of SQL Server 2005, the old-style sysobjects and sysdatabases and those catalog views have been deprecated. Do this instead – use the sys. schema – views like sys.databases private static bool CheckDatabaseExists(SqlConnection tmpConn, string databaseName) { string sqlCreateDBQuery; bool result = false; try { tmpConn = new SqlConnection(“server=(local)\\SQLEXPRESS;Trusted_Connection=yes”); sqlCreateDBQuery = string.Format(“SELECT database_id FROM sys.databases … Read more

How to implement badges?

A similar-to-Stackoverflow implementation is actually a lot simpler than you have described, based on bits of info dropped by the team every once in awhile. In the database, you simply store a collection of BadgeID–UserID pairs to track who has what (and a count or a rowID to allow multiple awards for some badges). In … Read more

Debugging stored procedures in SQL Server Management Studio

Only with SQL 2008 and SSMS 2008. Select from menu ‘Debug\Start Debugging’ or press Alt+F5 and will launch the step by step T-SQL debugger. On 2005 the only way is to attach Profiler and monitor for the SP:StmtCompleted event, which is not exactly debugger step-by-step, but at least you’ll see the execution flow. Not to … Read more

Is there any way to view whitespace in the query editor for SQL Server Management Studio Express 2005?

In case you still need to know how to do this. Edit the Registry Key in HKEY_CURRENT_USER: Software\Microsoft\Microsoft SQL Server\xx\Tools\Shell\Text Editor\Visible Whitespace Where xx is the version you have (90 is 2005, 100 is 2008) And set it to 1 to show. Or for SQL Server 2012 and up client tools it is also in … Read more