Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL error running WITH EXECUTE AS

Posted on 2010-11-11
11
Medium Priority
?
375 Views
Last Modified: 2012-05-10
I tried to add the following to my stored procedure but it did not work

WITH EXECUTE AS 'sa'

I got an error reading.

Cannot execute as the user 'sa', because it does not exist or you do not have permission.
0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 15

Accepted Solution

by:
AmmarR earned 500 total points
ID: 34110312
Execute as will impersonate the usage of the store procedure

to know more about it read this great article

http://www.sommarskog.se/grantperm.html#EXECUTE_AS
0
 
LVL 16

Assisted Solution

by:EvilPostIt
EvilPostIt earned 1000 total points
ID: 34110336
SA is a login not a user.

Have you tried execute as 'dbo'?
0
 

Author Comment

by:Mr_Shaw
ID: 34110359
I have a stored procedure which executes an Agent Job.

The Agent Job is owned by the SA.

should i not write WITH EXECUTE AS 'sa' ?
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 6

Assisted Solution

by:subhashpunia
subhashpunia earned 500 total points
ID: 34110368
You can use "Login_Name" in Execute AS clause only for DDL triggers with server scope or logon triggers.

For more information please check the article:
http://msdn.microsoft.com/en-us/library/ms188354.aspx
0
 

Author Comment

by:Mr_Shaw
ID: 34110369
how about

WITH EXECUTE AS LOGIN = 'sa'
0
 
LVL 6

Expert Comment

by:subhashpunia
ID: 34110383
Inside stored procedure use as: WITH EXECUTE AS 'dbo'



0
 
LVL 16

Assisted Solution

by:EvilPostIt
EvilPostIt earned 1000 total points
ID: 34110396
subhashpunia,

Please see the second comment.......
0
 

Author Closing Comment

by:Mr_Shaw
ID: 34110430
using dbo through back a permission error.

I'll read through all the material and try again.

Thanks
0
 
LVL 15

Expert Comment

by:AmmarR
ID: 34110432
Dear Mr_Shaw:

EvilPostIt: post to use dbo instead of sa will solve your initial problem but soon after that you will get the below error

The server principal "User" is not able to access the database "DB" under the current security context.

so i suggest reading the article i posted above and knowing every thing about Execute as before using it, its not  a straight forward thing and has many considerations.

Regards
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34110446
Hi AmmarR.

Agreed, you should always know what your using. Plus with my original posting i didnt realise this was executing something outside of the current database.
0
 
LVL 15

Expert Comment

by:AmmarR
ID: 34110496
Sure EvilPostIt:
Agree

good day
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question