Cannot find the object because it does not exist or you do not have permissions. Error in SQL Server

Viewed 243428

I have a database and have a Sql script to add some fields to a table called "Products" in the database.

But when i am executing this script, I am getting the following error:

Cannot find the object "Products" because it does not exist or you do not have permissions

Why is the error occurring and what should I do to resolve it?

14 Answers

Are you sure that you are executing the script against the correct database? In SQL Server Management studio you can change the database you are running the query against in a drop-down box on one of the toolbars, or you can start your query with this:

USE SomeDatabase

Does the user you're executing this script under even see that table??

select top 1 * from products

Do you get any output for this??

If yes: does this user have the permission to modify the table, i.e. execute DDL scripts like ALTER TABLE etc.? Typically, regular users don't have this elevated permissions.

Look for any DDL operation in the script. Maybe the user does not have access rights to run changes.

In my case it was SET IDENTITY_INSERT tblTableName ON

You can either add db_ddladmin for the whole database or for just the table to solve this issue (or change the script)

-- give the non-ddladmin user INSERT/SELECT as well as ALTER:
GRANT ALTER, INSERT, SELECT ON dbo.tblTableName TO user_name;

This could a permission issue. The user needs at least ALTER permission to truncate a table. Another option is to call DELETE FROM instead of TRUNCATE TABLE, but this operation is slower because it writes to the Log file, whereas TRUNCATE does not write to the log file.

The minimum permission required is ALTER on table_name. TRUNCATE TABLE permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, and are not transferable. However, you can incorporate the TRUNCATE TABLE statement within a module, such as a stored procedure, and grant appropriate permissions to the module using the EXECUTE AS clause.

In my case the sql server version on my localhost is higher than that on the production server and hence some new variables were added to the generated script from the localhost. This caused errors in creating the table in the first place. Since the creation of the table failed, subsequent query on the "NON EXISITING" table also failed. Luckily, in among the long list of the sql errors, I found this "OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF" to be the new varialbe in the script causing my issue. I did a search and replace and the error went away. Hope it helps someone.

The TRUNCATE statement was my first problem, glad to find the solution here. But I was using SSIS and trying to load data from another database, and it failed with the same error on any table that used IDENTITY to create an auto-incrementing ID. If I was scripting it myself I'd first need to use the command SET IDENTITY_INSERT tablename ON, and then SET IDENTITY_INSERT tablename OFF when the table update was done. But this requires ALTER permissions on the table, which I do not have. Hence the error message in SSIS on the table load (even though the previous step had just deleted all the data out of the table.)

You receive this error, when you use an ORM like GORM (https://gorm.io/) in Go for example.

When you try to create a struct and accidentally pass the ID (primary key) although it's inserted automatically.

Rich features IDE like Visual Studio Code make this mistake happen easily:

if tx := db.Create(&myStruct{
    Ts:                Time.Now(),
    ID:                42,
}); tx.Error != nil {
    t.Fatal(tx.Error)
}

You can still use auto-filling by Visual Studio Code, but delete your entry for your model's primary keys:

if tx := db.Create(&myStruct{
    Ts:                Time.Now(),
}); tx.Error != nil {
    t.Fatal(tx.Error)
}
Related