What I want
To be able to insert multiple rows into a table, in SQL Server, using python, and have my INSTEAD OF INSERT trigger only triggered when all rows have been inserted.
Background
I have an INSTEAD OF INSERT trigger on my table which performs actions on previously unseen data.
The usual mode of operation is that periodically, multiple rows are inserted at a time.
When I'm inserting from python (using pyodbc), if I use:
cursor.executemany('INSERT INTO table_name VALUE (?, ?, ...)', list_of_lists)
The trigger will be triggered for every row - not what I want.
I'm currently using:
sql = 'INSERT INTO table_name VALUES (...),(...)...;' # string built with format-strings
cursor.execute(sql)
This theoretically opens me up to SQL injection, but my environment is locked down so not really an issue.
Also, I don't know what the max limit for the query string is.
I've tried to find the limit out using the method in this post, but I get back an empty result. So I'm not sure if there is no limit or if it is unknown.
The number of rows I'd be inserting at a time isn't expected to be crazy (no more than 1-2k rows, handful of columns). So I don't imagine I'd hit a limit but I don't want to be surprised later.
There is then the option of BULK INSERT with the FIRE_TRIGGERS option, but that would require uploading my data as a file to the machine running SQL Server. This is complexity that I would like to avoid.
Is there a better option or is my current solution fine?