BeachBum
BeachBum

Reputation: 571

How can I use permissions other than SQLAgent account to run BULK INSERT from within code?

We have some code that we want to run a BULK INSERT sql command, but it's getting access denied because the account SQLAgent is installed as doesn't have permissions to the folder the file is in. I don't want that account to have permissions to that folder, and would love to use a proxy account if I could instead.

Don't think this helps much, but here's the vb code we're using to run the sql command:

        Dim sql As String = "BULK INSERT Visa From '@file' With (ROWTERMINATOR = '\n', FIELDTERMINATOR = '\t', KEEPNULLS)"
        sql = Replace(sql, "@period", _date)
        sql = Replace(sql, "@file", FullPath)
        DBExecute(sql)

How to get this to work?

Upvotes: 1

Views: 661

Answers (1)

Filip Popović
Filip Popović

Reputation: 2655

As HardCode wrote, You can use RunAs for each job step. Basically, You have to:

  • add credentials - credentials means: "impersonate to this credentials"
  • add proxy - proxy means: link between credentials and type of actions (subsystems) like running SSIS package, running command, ...
  • configure job step to use added proxy

Note that credentials You use has to have permissions to access the folder as well as permissions to connect to databse. Later is not needed if You use SQL Server login instead of Windows Authentication in your VB.NET code.

Here You can find step by step instructions on how to add credentials and proxy.

UPDATE: your comment shed some light on original question. You didn't want to change job step proxy/credentials but to use .NET to impersonate user.

You can achieve this by:

  • switching to SQL Server login - dangerous because You have to save password in connection string
  • impersonating user using .NET code - check articles: how to impersonate user and check this one for very good security hint

Upvotes: 1

Related Questions