2

I would appreciate confirmation on the answer I found digging through T-SQL's documentation.

I have a stored procedure that references a view twice. Once in the procedure and then a second time when executing the dynamic sql statement.

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(Course)
FROM (SELECT DISTINCT Course FROM CourseSalesView) AS Courses

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT Year, ' + @ColumnName + '
    FROM CourseSalesView
    PIVOT(SUM(Earning) 
          FOR Course IN (' + @ColumnName + ')) AS PVTTable'

--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

My database has SQL Server's default isolation level of 'ReadCommited'. If something else changes my view in-between my procedure and sp_executesql will I have an concurrency problem? I.E. Could a row being removed in my view by another user at the exact right time cause a mismatch between this procedure's two statements?

I am under the assumption that the 'ReadCommited' isolation level prevents this from happening and that I do not need to explicitly declare the isolation level anywhere in my Stored Procedure as it is inherited from the default setting.

4

In your current setup, each of those queries is a separate transaction. Individually they are reading only committed data. But nothing is binding them together. If you want to guarantee the same underlying data for that view across both statements, you need to wrap them in a transaction, and add a Table Hint to the first query specifying to lock the table: WITH (TABLOCK, HOLDLOCK). I just tested this on a View and it does block access until the transaction completes, unless a query uses the WITH (NOLOCK) hint.

Regardless, you can do an easy test with your code to see how your current code works and how any changes might do what you are wanting:

Right before the EXEC sp_executesql @DynamicPivotQuery line, add the following two lines:

RAISERROR('Make a change now...', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:01:00.000' -- 1 minute pause

Then, run the code you have posted above. When you see "Make a change now" in the Messages tab, make a change to the underlying table. Once the WAITFOR finishes it will run the dynamic SQL and you should see the effects of your changes.

So the following should work:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX);
DECLARE @ColumnName AS NVARCHAR(MAX);

BEGIN TRAN;
--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(Course)
FROM (SELECT DISTINCT Course
      FROM CourseSalesView WITH (TABLOCK, HOLDLOCK)) AS Courses;

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT Year, ' + @ColumnName + '
    FROM CourseSalesView
    PIVOT(SUM(Earning) 
          FOR Course IN (' + @ColumnName + ')) AS PVTTable';

--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery;
COMMIT TRAN;

Beyond that basic interaction, there are two other options:

  • Storing the results of the View into a local Temp Table (i.e. #TableName). This has the benefit of not locking the underlying table(s) of the View, but it will have to write the entire results of the view into tempdb and that does not sound like a scalable solution if the View returns 10k rows or more.

  • Enable SNAPSHOT ISOLATION (introduced in SQL Server 2005) which handles this for you without needing to explicitly lock the table:

    • Snapshot Isolation in SQL Server
    • ALTER DATABASE SET Options
    • You will still need to wrap the two queries in an explicit Transaction, but no need for the Table Hint
    • If you don't enable READ_COMMITTED_SNAPSHOT then you will need to issue SET TRANSACTION ISOLATION LEVEL SNAPSHOT before the BEGIN TRAN
  • 1
    AH yes! I have confirmed your remarks with the test you posted. Indeed the solution you posted will work but I have a question... From what I've read Table Hints should be used as a last resort. msdn.microsoft.com/en-us/library/ms187373.aspx Does it make sense to instead load the view into a #temptable and then use in this procedure? – abaldwin99 Feb 2 '15 at 21:16
  • @abaldwin99 Well, they are just being cautious there. You could dump it to a temp table, but that becomes impractical as the size of that view increases. Though a benefit is that you don't have to lock the underlying table(s). What version of SQL Server are you on? You might be able to use SNAPSHOT ISOLATION. – Solomon Rutzky Feb 2 '15 at 21:21
  • Agreed it's impractical. However I'm nervous to go locking tables as the providers of the softare I am using have stated I'm not to make any SQL commands that could interfere with the larger system. Transactions are sort of in a grey area undefined area. I'm in 2005 which looks like it has SNAPSHOT isolation? – abaldwin99 Feb 2 '15 at 21:32
  • @abaldwin99 Yes, I just updated (we were typing at the same time) and included that info. That feature did start in SQL Server 2005 so start there. – Solomon Rutzky Feb 2 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.