Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlBulkInsert - How to set Fire Triggers, Check Constraints?

I'm performing a bulk insert with an ADO.NET 2.0 SqlBulkCopy object from a C# method into a MS SQL 2005 database, using a database user with limited permissions. When I try to run the operation, I get the error message:

Bulk copy failed. User does not have ALTER TABLE permission on table 'theTable'. ALTER TABLE permission is required on the target table of a bulk copy operation if the table has triggers or check constraints, but 'FIRE_TRIGGERS' or 'CHECK_CONSTRAINTS' bulk hints are not specified as options to the bulk copy command.

I read some documentation and created the bulk copy object with the constructor that lets me specify such things:

    SqlBulkCopy bc = new SqlBulkCopy(
        System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"],
        SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints);

But this doesn't change anything - I get the same error message as before. I tried fiddling with some of the other SqlBulkCopyOptions values but no luck. I really thought this would fix the problem, am I missing something?

I tested the procedure after granting ALTER on the table to my user, and the operation succeeded. However this is not an option for my situation.

like image 825
James Orr Avatar asked Dec 16 '08 22:12

James Orr


People also ask

How does bulk insert work?

BULK INSERT loads data from a data file into a table. This functionality is similar to that provided by the in option of the bcp command; however, the data file is read by the SQL Server process. For a description of the BULK INSERT syntax, see BULK INSERT (Transact-SQL).

Does bulk insert lock table?

Why does bulk insert lock the entire table? Specifies that a table-level lock is acquired for the duration of the bulk-import operation. A table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is specified.

How load large data in SQL Server?

Use a format file to bulk import data - SQL Server In SQL Server, you can use a format file in bulk-import operations. A format file maps the fields of the data file to the columns of the table.

What is bulk operations in SQL Server?

The SQL Server bulk copy feature supports the transfer of large amounts of data into or out of a SQL Server table or view. Data can also be transferred out by specifying a SELECT statement. The data can be moved between SQL Server and an operating-system data file, such as an ASCII file.


2 Answers

Solved it! Looks like I need a refresher on flags enums. I was bitwise ANDing the enum values when I should have been ORing them.

SqlBulkCopyOptions.FireTriggers & SqlBulkCopyOptions.CheckConstraints

evaluates to zero (which is equivalent to SqlBulkCopyOptions.Default.)

SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.CheckConstraints

Worked correctly and allowed the bulk insert to complete.

like image 101
James Orr Avatar answered Nov 01 '22 01:11

James Orr


Possibilities only, I'm sorry

SQL documentation for BULK INSERT specifies 3 cases where ALTER TABLE is needed. You listed 2 of them. Is the KeepIdentity option being set, even if not needed?

Another option is that the trigger on the table is disabled already, confusing the issue. Use ALTER TABLE dbo.SomeTable ENABLE TRIGGER ALL to ensure enabled.

like image 2
gbn Avatar answered Nov 01 '22 00:11

gbn