SQL Server database publishing with the ‘generate smart defaults’ option

If you’ve ever run a database package (.dacpac) publish against a SQL Server or Azure SQL database, chances are good that you have run into the following error when changing the schema for a table that contains data:

SQL72014: .Net SqlClient Data Provider: Msg 50000, Level 16, State 127, Line 6 Rows were detected. 
The schema update is terminating because data loss might occur.
Continue reading

How to find users near a central coordinate (spatial nearest neighbor queries)

Scenario

You have a database table of users who have a location specified. Given an input location point, coordinate, or city – find the top N (or all) of the nearest neighbors in range. So for example, if I am in Seattle, return 15 nearest users to my location within a 50 mile range. This is a classic spatial/geography scenario with many applicable uses. I used this technique when building some Tourvival features.

Setup

Our example uses Microsoft SQL Server, or SQL Azure database. Let’s start by making an example users table.
Continue reading