views:

552

answers:

8

I am thinking about creating stored procedures on the fly.

ie running CREATE PROCEDURE... when the (web) application is running.

What are the risks or problems that it can cause?

  • I know that the database account needs to have the extra privileges.
  • It does NOT happen everyday. Only from time to time.
  • I am using sql server and interested in mysql and postgres as well.

Update1:

Thanks to comments, I am considering creating a new version of stored procedure and switching over instead of ALTERing the sp. example: sp1 -> sp2 -> sp3

Update2:

The reason:

My schema changes because of custom fields (unknown number and type of columns) I tried dynamic sql and sp_executesql first. Of course it works. Dynamic sql works greate for 1,2,3 simple update,inserts.

But it got too ugly and a lot of work and it does not mix well with stored procedure, problems with sql parameterization because it is used inside a stored procedure and the number and type of params is not known at compile time (long story).

At least the basic scenario for this solution is not that complicated. The logic of the sp does NOT change. For each custom field I have to add a new parameter to sp and add a column to update, insert, etc.

I also considered making stored procedure parameters dynamic like sp_executesql that accepts any number and type of params but could not find a way.

A: 

Firstly, the answer to this question really depends on what exactly this stored procedure is intended to do. If it's just reading data or creating a result set for reporting and you don't mind if it's a little inconsistent, then you're probably fine. If it's doing anything remotely interesting with your data then it's a very risky thing to be doing. You should think about whether it's possible (and what would happen) for two users users (or the same user twice) to run multiple versions of the the same stored procedure at the same time. Smells like a train wreck to me. One option is to only allow this procedure alteration to take place when no other users are logged into the system, or forcibly boot them off the database if they are. Another option is to create your new stored procedure with a slightly different name and swap them over when you deem it safe to do so.

ninesided
logic doesnt change.Only columns are added to updates,inserts.Put it simply new sp is used instead of dynamic sql.I do not know if there can be two versions of sp running at the same time but if it happens it won't be a problem.It is like running variations of a dynamic sql(with different columns)
A: 

Another issue is that one of the major benefits of stored procedures is that the execution plan is cached, meaning it will execute faster. If you are creating them on the fly you lose that advantage.

Valerion
I mentioned it does NOT happen everyday. only once in a while
Ah, so you did. I must confess I read and answered this before having any coffee!
Valerion
The execution plan for _all_ sql is cached, so SPs don't really offer any advantage from that point of view
David Kemp
I would no longer categorize this as a major benefit of stored procedures, at least in SQL Server.
Cade Roux
+1  A: 

For a transactional system it's probably quite expensive. If you have a large batch job and want to use a code generator for some reason (quite a common architecture in ETL tools, notably Oracle Warehouse Builder and Wherescape Red), it's not unreasonable to do this.

ConcernedOfTunbridgeWells
A: 

If you really need to do this then you should randomise the name of the procedure to avoid clashing with other users. Remember always that other users may be doing their own thing at the same time - most database systems won't give transactional isolation for stored procedures (Postgres is the only one I know of that does).

It would be extremely rare that this would be a desirable thing to do - could you elaborate at all on what made you choose this approach?

A: 

I would not do that personally.

As you mentioned you will need extra privileges to grant access to create/alter database objects. That can create a serious security risk as nothing would stop your application from creating a malicious stored procedure if someone discovered a security hole in it.

If your schema changes, change the stored procedures with the schema.

kristof
schema changes at run time when users add a custom field
A: 

You will not be able to alter the procedure if one or more users are running the procedure, or another procedure that references your procedure. You will block until all the dependent procedures and the procedure you want to compile (and I think the procedure s you invoke from your procedure, but I am not certain) are not in use. This may be a long time on a busy production system, and if you are unlucky, you may timeout waiting for all the dependencies to not be in use (5 minutes on Oracle).
You can also get into very ugly situations (I have). Take for example stored procedures B and C, both of which call A, the procedure that you are trying to compile. When no one is running B, the system locks B. Now any user trying to run B will stall. The system then tries to lock C, but C is generating a very lengthy report that will not be done for another 10 minutes. You will timeout waiting for the lock, and some of your users will have an unresponsive system for 5 minutes. My experience is with Oracle, I would make sure your target DBMS does not behave in the same fashion, or has quicker failures or a better lock acquisition strategy.
I guess I am cautioning that what looks like may work on a development server may fail dramatically on a busy production system.

Tony BenBrahim
A: 

I'm not sure that the locking discussed by Tony BanBrahim is true in SQL Server 2005.

I have some long-running SPs (a 3 hours batch process of about 30 sub-processes), and I have been able to alter the SP while it is still running. (I don't believe the changes take effect until the next run, but it doesn't cause any blocking or any error). Now the outer long-running SP does both call SPs dynamically with EXEC and statically, but I've change both the root and nested SPs while they are running without error messages or blocks.

WRT your original question, I would think that your tactic is fine if used in a controlled way.

Cade Roux
A: 

You mentioned that you would be adding and/or changing the calling profile of the stored procedure when you do this alteration. How are you lock-stepping the new calling profile with the application that makes the call to this? What's your roll-back plan if you ever have to revert a change that was made?

In the past what I've done is just append an incrementing numeric suffix to the stored procedure name with the new calling profile -- then you can modify the old version of the SP to call the new one with a default value for the parameter, and then you can release your software calling the new version.

If something breaks in your new version and you have to rollback, calls to the old stored proc will still work without error, and just populate the custom fields with your default values.

scwagner