Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBCC CHECKIDENT on a temporary table throwing permissions error for wrong user

I'm logged into a SQL Server 2005 database as a non-sa user, 'bhk', that is a member of the 'public' server role only. The following code tries to execute within a stored procedure called by user 'bhk'. This line of code...

TRUNCATE TABLE #Table1
DBCC CHECKIDENT('#Table1', RESEED, @SequenceNumber) WITH NO_INFOMSGS

causes this error...

User 'guest' does not have permission to run DBCC CHECKIDENT for object
'#Table1__00000000007F'.

I'm aware of the permissions required to run DBCC CHECKIDENT...
Caller must own the table, or be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role.

So I have two questions:

  1. Since 'bhk' is calling a stored procedure that creates a temporary table, shouldn't 'bhk' be the owner and be allowed to run DBCC CHECKIDENT?
  2. Why does the error message return that user 'guest' doesn't have permission? To my knowledge, I'm not logged in as 'guest'.

Any help would be greatly appreciated.

like image 218
Brad Knowles Avatar asked Oct 09 '08 15:10

Brad Knowles


People also ask

What does DBCC Checkident do?

DBCC CHECKIDENT returns the current identity value and the current maximum value of the identity column. If the two values are not the same, you should reset the identity value to avoid potential errors or gaps in the sequence of values.

What is reseed in SQL?

Reseeding / Resetting identity value in SQL Server 2005 You can reseed indentity values, that is, to have identity values reset or start at new predefined value by using DBCC CHECKIDENT. For example, a table named nwdsTable can be reseeded with the indentity column to 3.

How do I find the current identity value of a table in SQL Server?

To check the last identity value information for your table you can run the following command: DBCC CHECKIDENT ( 'yourtable', NORESEED ); Where yourtable is set to the table in which you what to check the identity value information.


1 Answers

Here is an alternate solution, that may work if you need to re-seed with a sequence number of more than 1.

TRUNCATE #Table1

SET IDENTITY_INSERT #Table1 ON

INSERT INTO #Table1 (TableID) -- This is your primary key field
VALUES (@SequenceNumber - 1)

SET IDENTITY_INSERT #Table1 OFF

DELETE FROM #Table1

What this is doing is to set the IDENTITY_INSERT on your temporary table, to allow you to add a row with an explicit ID. You can then delete this row, but further inserts should start from the last sequence number.

like image 128
Tim C Avatar answered Sep 22 '22 02:09

Tim C