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

前端 未结 14 2009
面向向阳花
面向向阳花 2020-12-05 12:35

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 foll

相关标签:
14条回答
  • 2020-12-05 13:03

    It can also happen due to a typo in referencing a table such as [dbo.Product] instead of [dbo].[Product].

    0 讨论(0)
  • 2020-12-05 13:04

    In my case I had created the tables via a script in Visual Studio's Server Explorer where I could perfectly query them. The connectionstring was ok. When rebooting my pc I saw that none of the tables came up anymore in the Server Explorer. I reran the creation script in Management studio and my problem was solved. Bizarre ? Yes.

    My lesson: if nothing helps, reboot your pc

    0 讨论(0)
  • 2020-12-05 13:06

    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.

    0 讨论(0)
  • 2020-12-05 13:09

    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.

    0 讨论(0)
  • 2020-12-05 13:10

    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
    
    0 讨论(0)
  • 2020-12-05 13:11

    You can right click the procedure, choose properties and see which permissions are granted to your login ID. You can then manually check off the "Execute" and alter permission for the proc.

    Or to script this it would be:

    GRANT EXECUTE ON OBJECT::dbo.[PROCNAME]
        TO [ServerInstance\user];
    
    GRANT ALTER ON OBJECT::dbo.[PROCNAME]
        TO [ServerInstance\user];
    
    0 讨论(0)
提交回复
热议问题