• Home
  • About Me
  • Contact Us
  • Gallery
  • Resume

WikiDBA

by Virendra Yaduvanshi – Microsoft SQL Server Database Architect | Consultant | Blogger | Specialist | DBA | Speaker
Stay updated via RSS

  • SQL Authors

  • Articles

  • Blog Stats

    wordpress visitor counter View My Stats
  • Search

  • Social

    • View YaduvanshiVirendra’s profile on Facebook
    • View v_yaduvanshi’s profile on Twitter
    • View virendrayaduvanshi’s profile on LinkedIn
  • wikidba

    wikidba

A T-SQL script fail to run a job while it run successfully in SQL Server Management Studio

Posted: November 28, 2013 by Virendra Yaduvanshi in Database Administrator
Tags: A T-SQL script fail to run a job while it run successfully in SQL Server Management Studio, EXECUTE AS, Security context of SQL Server Agent Service Account, Why did a T-SQL script fail to run a job, Why did a T-SQL script fail to run a job when it could successfully run in SQL Server Management Studio, Why did a T-SQL script fail to run a job when it could successfully run in SSMS
2

This issue is usually caused by the circumstances when the execution security context of the job is different from the security context when we manually run the script in SSMS. The commands in SQL Server Management Studio are executed as the current login. However by default every job step runs in the security context of SQL Server Agent Service account. If the SQL Server Agent Service account is different from the login account and if it does not have enough permission to execute the query, the job will fail.

To resolve this we can simply give the SQL Server Agent Service account permissions to execute the query, this is not the best way from a security perspective but it may be a temporary solution. As security perspective best practices, SQL Server Agent Service account is recommended to give it the least amount of privileges, and as a resultant we cannot create a SQL Server Agent proxy to execute the job because for T-SQL job step it will not use any SQL Server Agent proxy.

For a Transact-SQL job step, by default it runs under the security context of the job owner and if the owner of the job is a member of the sysadmin fixed server role, it runs under the SQL Server Agent service account.

Below are few methods to resolve this issue:

1. Changing the job owner to a login who has enough permission to execute the T-SQL script but the job owner should not be a sysadmin.

2. Specifying the “Run as User” option for the Transact-SQL job step in case you have several job steps and you do not want to change the job owner so as not to impact other job steps execution. You can go to the Advanced options of the job step, and input an user to the “Run as User” field. Ensure you are a sysadmin to edit this option.

3. Adding an EXECUTE AS statement ahead of your T-SQL script.

Share this:

  • Facebook
  • LinkedIn
  • Twitter
  • Tumblr
  • Print
  • Email

Like this:

Like Loading...

Related

Comments
  1. prabhdeep Singh says:
    November 28, 2013 at 4:45 pm

    That was really good

    LikeLike

    Reply
  2. Imtiyaz Khan says:
    November 28, 2013 at 4:52 pm

    Nice sir jee…..

    LikeLike

    Reply

Leave a Reply Cancel reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. ( Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. ( Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. ( Log Out /  Change )

Cancel

Connecting to %s

Full database backup file is larger than database size
Ignoring “Restart computer” rule when installing SQL Server 2008

  • Upcoming Events

    No upcoming events

  • Follow WikiDBA on WordPress.com
  • Virendra Yaduvanshi

    Sector - 46, Gurgaon
    +91 8130396263
    Lunch: 11am - 2pm
    Dinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • WikiDBA
    • Join 691 other followers
    • Already have a WordPress.com account? Log in now.
    • WikiDBA
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • View post in Reader
    • Manage subscriptions
    • Collapse this bar
%d bloggers like this: