Yshayy
Yshayy

Reputation: 325

SqlBulkCopy in SQLCLR stored procedure?

I created SQLCLR stored procedure that retrieve large amount of data from external service. Can I bulk copy the data to a new table in the same SQLCLR stored procedure?

Upvotes: 0

Views: 750

Answers (1)

gjvdkamp
gjvdkamp

Reputation: 10516

I'll zoom out here to the general problem of how to expose data from a service inside SQL. What can might do is rewrite your stored procedure as a Table Valued CLR function that streams the results. Then you can use like this:

insert into MyTable(id, name)
select id, name from dbo.MyTableFunc(agrs) 

The infrastructure for this is well optimized, I used it and was quickly able to get it to some 3k rows per second when I stopped optimizing, and the bottleneck was in getting the rows over the network.

The upside of exposing the service as a function is that you also directly use it in queries and joins, no need to store it in to a table on disk first (extra step with slow disks, transaction log, locks yadayada). It also cancels gracefully and you can do top 100 etc.

If you tell us more/ give some code we can help more.

Upvotes: 1

Related Questions