Question

Is there a way to change the code of CLR procedure in SQL Server dynamically?

Suppose you have an assembly with your business logic deployed in MS SQL Server 2008 R2. This assembly (or assemblies) is being used constantly (for example calling some functions for each row of a table in multiple concurrent queries). So you cannot just drop assembly. Is there a way to change my business logic dynamicly or some way to execute external changable code?

I've already explored these approaches, but none worked:

  1. Reflection.Emit
  2. Mono.Cecil
  3. Loading external assembly in the assembly deployed in SQL Server

UPDATE: The question was not about release process: I want to be able to set some security rules dynamically via GUI.

For instance some users should be able to see only clients without their addresses or the transactions within the last year and so on.

The rules are not complicated but they may change almost every day and we cannot put them in the code. The rest of the business logic is implemented in TSQL. CLR was chosen because of the performance issue (dynamic SQL is too slow).

There was another option: generate clustered views (with rules in WHERE section) but it was not quick enough.

Some more details:

Suppose we have some code selecting a part of big table dbo.Transactions

select *
from dbo.Transactions
where ... --filters from your business logic

If we want to filter the result to show allowed rows we could generate some indexed view and join it with the result set like this:

select *
from dbo.Transactions t
inner join dbo.vw_Transactions v
on t.id = v.id
where ... --filters from your business logic

But if we check the execution plan in most cases the query analyzer decides not to filter dbo.Transaction and then join with vw_Transactions, but to join first and filter later (which is absolutely not desirable). Hints like FORCE ORDER doesn't help.

Was it helpful?

Solution 2

There's a good library for dynamic evaluating arithmetic expression (with parameters) - Flee

In my case I didn't have to execute any .Net code - just the expressions like "Date > '20100101' Or Status = 2", so Flee satisfies almost completely. The only issue is that its logical operators don't work with SqlBoolean type (which is used in sql expressions) but it's not a big deal to add this feature.

But in general case it's seems to be impossible to execute dynamic .Net code inside Sql Server host.

OTHER TIPS

I'm not a CLR assembly expert, but the obvious options are:

  1. ALTER ASSEMBLY
  2. Drop and re-create the assembly inside a transaction
  3. Define a maintenance window and deploy it then

gbn's point about release processes is a good one. If your procedures (and therefore your business operations) are really constantly running 24x7 then presumably you already have some form of system redundancy and established maintenance procedures for patching and upgrading applications? If so, just deploy your new code in your usual maintenance window.

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