Question

We have some SQL Agent jobs running in our product. But we want to do away with SQL server agent so that we can use Sql server express. So I am writing our own service to execute stored procedures that currently run as jobs. Everything else except for "Owner" can be duplicated easily. As far as I understand, when you specify owner in SQL server agent, jobs are run in that login context. I am using SqlCommand class from .NET to run stored procedures. But that class does not support giving different login context. Is there another way by which I can specify different login for a job just like SQL Server Agent allows you to do?

Thanks in advance, -Neel.

No correct solution

OTHER TIPS

if you don't want to use current connection to run stored procedure from code then first create a user in sql server and use that one for execution the stored procedure.

SQlCommand takes connection as parameter which holds owner information. You will have to explicitly create new connection with new user and use it to execute your SP.

You can use EXECUTE AS command in your stored procedure to execute as a different user which is passed as parameter:

create procedure testing
    (@username nvarchar(100))
as
    execute as user = @username;

    select USER_NAME(); -- this is just to test the current user name, remove it later
    -- your code goes here

    revert;

You can read more about it and required permissions here: http://msdn.microsoft.com/en-us/library/ms181362.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top