SQL Server 2012

I have a maintenance script that I wrote for some of my operators. They are members of the fixed db_datareader and db_datawriter roles. Part of the script creates a table variable, then populates the variable with some statistics generated during the script.

The problem occurs when the script attempts to UPDATE the table variable. INSERT appears to work, but not UPDATE.

--!!! THIS WORKS !!!--
INSERT INTO @tblRecCounts([table_name])
select name from sys.tables where 
name not in

--!!! THIS FAILS !!!--
USE db1
UPDATE @tblRecCounts
SET f.db11PreLoadCount=dtRC.row_count
FROM @tblRecCounts f
<some subquery>

The script works if I run it with higher permissions (e.g. owner). The error that db_datawriter gets is "The user does not have permission to perform this action."

What and how do I set permissions for these roles for a table variable that they create??? I would think the entity that creates the variable (table) would own it.

  • 2
    Can you post a functional script that demonstrates the problem? Is the table variable a table type or one that's defined locally?
    – Dan Guzman
    Jul 2, 2015 at 23:21
  • 2
    Does the subquery itself work? Jul 2, 2015 at 23:40
  • In your "this fails" example, if you really are switching databases after declaring the variable, then it could be related to cross database ownership chaining
    – BateTech
    Jul 3, 2015 at 1:42

1 Answer 1


Martin Smith's comment ("Does the subquery itself work?") was ultimately the answer and highlighted my insufficient forensic work! ;-)

The subquery was returning ROW COUNTS from tables:

INNER JOIN sys.dm_db_partition_stats AS ddps 

The fixed roles I was using don't give VIEW DATABASE STATE permission (which is required to view the db_partition_stats table). So, I'll either need to mess with the roles for these users or find another workaround.

I guess it's worth noting that if you come across this circumstance, you need to deconstruct your query. The fixed db roles that I refer to CAN CREATE, INSERT, UPDATE, and DELETE records from a table variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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