SQL MERGE Statement with More Than 2 Tables

Table 1 Id Name Address Table 2 Id Company Salary Table3 Id Name Address Company Salary Age Sex I want to Merge These table in a single MERGE statement, Is This Possible?? Like MERGE table1 AS tg1 MERGE table2 AS tg2 USING (SELECT Id ,Name ,Address FROM Table3) AS sr ON tg1.Id = sr.Id and […]

Select average duration

I have a table Statistics with info about user’s interaction with objects on my site. The structure of the table is like this: id | object_id | user_id | interaction_time | interaction_type —-+———–+———+———————+—————— 1 | 1 | 1 | 2015-07-08 12:00:00 | opened 2 | 1 | 2 | 2015-07-08 12:10:00 | opened 3 | […]

sp_getapplock transaction within transaction

I have a very quick question in regards to sp_getapplock. Is calling sp_getapplock with the same resource (lock id) from one stored procedure to another allowed? For example: CREATE PROCEDURE [uspTest1] ( … … ) AS BEGIN TRY BEGIN TRANSACTION … … EXEC @RC = sp_getapplock @Resource = “Test 123”, @LockMode = ‘Exclusive’, @LockOwner = […]

How to separate writes from reads to minimize effect of heavy read queries?

I have write-heavy tables in my database. There is a need to run read-only queries by someone else. I have no idea about complexity and volume of their queries but I do know when they start doing it, writes become superslow. So separating writes from reads seems the way to go. Is the replication an […]

ASPNET Identity 2.0 ROLES work in Development but not Production

An application running on my development system (below) successfully implements roles on the SQL Express DB that exists within the application (moved from outside the app db). Pretty straight stuff, fortunately, as I am not a rocket scientist. On the production machine, the app successfully communicates with the user from the DB for log-ins, etc. […]

Better Practice: Delete&Insert vs. Update

I need to parse data (~60000 rows) from CSV file and write them to MSSQL table (the data is Date/Time and Value which is a decimal number). Each day I get one such CSV file. The catch is, that in the CSV file that I get each day I have data from the last 5 […]

How to avoid computed-column duplicate computation in an SQL Query

I have the following query: Use Northwind select OrderID as ‘Order ID’, (UnitPrice * Quantity) as ‘Total’, case when (UnitPrice * Quantity) > 100 then ‘Good’ else ‘Bad’ end as ‘Rating’ from [Order Details] Theoretically, it computes (UnitPrice * Quantity) two times, and I believe that this is a bad choice for the performance. How […]

“Must declare variable”, but when I declare it, “variable already declared”

This question already has an answer here: SQL Server Output Clause into a scalar variable 2 answers

DbFunctions for DateTime/TimeSpan addition (datetime / time(7) in SQL Server)

I have the following properties in my EF6 code-first model: public DateTime StartTime { get; set; } public TimeSpan Duration { get; set; } EF translates this to the SQL Server columns StartTime DATETIME Duration TIME(7) This works great so far. Now I want to load all entries which have their EndDate = StartDate + […]

Launching SSMS 2014 VsDebugPresentationPackage Error

I am getting this error when trying to launch SQL Server Management Studio 2014. Package VsDebugPresentationPackage did not load correctly. And in error log: SetSite failed for package [VsDebugPresentationPackage, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a] Any ideas?

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.