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

前端 未结 14 2010
面向向阳花
面向向阳花 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:17

    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.)

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

    I've been trying to copy a table from PROD to DEV but get an error: "Cannot find the object X because it does not exist or you do not have permissions."

    However, the table did exist, and I was running as sa so I did have permissions.

    The problem was actually with CONTRAINTS. I'd renamed the table on DEV to be old_XXX months ago. But when I tried to copy the original one over from PROD, the Defaut Constraint names clashed.

    The error message was misleading

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

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

    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.

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

    In my case I was running under a different user than the one I was expecting.

    I had 'DRIVER={SQL Server};SERVER=...;DATABASE=...;Trusted_Connection=false;User Id=XXX;Password=YYY' as the connection string I passed to pypyodbc.connect(), but the connection was still using the credentials of the Windows user that ran the script (I verified this using the SQL Server Profiler and by trying an invalid uid/password combination - which didn't result in an expected error).

    I decided not to dig into this further, since switching to this better way of connecting fixed the issue:

    conn = pypyodbc.connect(driver='{SQL Server}', server='servername', database='dbname', uid='userName', pwd='Password')
    
    0 讨论(0)
  • 2020-12-05 13:24

    Sharing my case, hope that will help.

    In my situation inside MY_PROJ.Database->MY_PROJ.Database.sqlproj I had to put this:

    <Build Include="dbo\Tables\MyTableGeneratingScript.sql" />
    
    0 讨论(0)
提交回复
热议问题