Why is it considered bad practice to use cursors in SQL Server?

Because cursors take up memory and create locks. What you are really doing is attempting to force set-based technology into non-set based functionality. And, in all fairness, I should point out that cursors do have a use, but they are frowned upon because many folks who are not used to using set-based solutions use cursors … Read more

SQL Server: how to write an alter index statement to add a column to the unique index?

You cannot alter an index – all you can do is drop the old index (DROP INDEX (indexname) ON (tablename)) re-create the new index with the additional column in it: CREATE UNIQUE NONCLUSTERED INDEX (indexname) ON dbo.YourTableName(columns to include) The ALTER INDEX statement in SQL Server (see docs) is available to alter certain properties (storage … Read more

Can there be constraints with the same name in a DB?

No – a constraint is a database object as well, and thus its name needs to be unique. Try adding e.g. the table name to your constraint, that way it’ll be unique. CREATE TABLE BankAccount ( BankAccountID INT PRIMARY KEY, EmployerCode VARCHAR(20) NOT NULL, Amount MONEY NOT NULL, CONSTRAINT FK_BankAccount_Employer FOREIGN KEY (EmployerCode) REFERENCES Employer … Read more

When using Trusted_Connection=true and SQL Server authentication, will this affect performance?

Not 100% sure what you mean: Trusted_Connection=True; IS using Windows credentials and is 100% equivalent to: Integrated Security=SSPI; or Integrated Security=true; If you don’t want to use integrated security / trusted connection, you need to specify user id and password explicitly in the connection string (and leave out any reference to Trusted_Connection or Integrated Security) … Read more

How to use alias column name in where clause in SQL Server

You can’t use aliased columns in a WHERE clause. You can try using a derived table. Perhaps something like this (sorry, not tested): SELECT * FROM ( SELECT SQRT( POWER( cast(Program_Latitude as float) – cast(‘41.5126237’ as float), 2) + POWER( cast(Program_Longitude as float) – cast(‘-81.6516411’ as float), 2) ) * 62.1371192 AS DistanceFromAddress from tblProgram … Read more