A job can run in SQL Server Management studio but fails to run in T-SQL

Source: Internet
Author: User
Tags management studio sql server management sql server management studio
Problem:

You can run a job in SQL Server Management studio, but it fails to run with a T-SQL, and vice versa.

 

Analysis: In most cases, context security issues occur during execution. When executed in SSMs, The T-SQL statement is run under the current login name. However, jobs run on the SQL Server Agent service account in the SQL Server Agent, if the account of the SQL Server Agent is different from the account that executes the T-SQL in SSMs or has different permissions, the job fails. My practice is to use a high-permission account to run the SQL Server Agent, and have an independent account, and the password cannot expire, otherwise it will not run for a period of time. However, based on the "minimum security principle", it is generally not recommended to use excessive permissions. At the same time, you cannot use the SQL Server Agent agent to execute a job because the T-SQL job step does not use any proxy. For a T-SQL job step, it is run by default in the security context of the job owner. Solution: Method 1: Open the permissions of the job owner to a large enough value, but do not use SysAdmin. Method 2: run the T-SQL job with the run as user prompt in the T-SQL job. In this way, you do not need to change the original permissions. However, this process ensures that you have sufficient permissions to grant the run as user. Method 3: This method is mainly scripted method 2, in the beginning of the T-SQL: [SQL] View plaincopyprint?
    1. Execute As User='Xxxx'
    2.  -- The preceding statement grants the following script XXXX logon user permissions.
    3.  Select*FromHumanResources. Department
    4.  -- Revoke permissions after running:
    5. Revert;
 
Execute as user = 'xxxx' -- the preceding statement grants the following script XXXX logon user permissions. Select * From HumanResources. Department -- revoke permission after running: Revert;

Finally, you can use sqlserver profiler to monitor the accounts that execute jobs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.