sql - Stored procedure, pass table name as a parameter -
I have almost half a dozen generic, but quite complex stored procedures and functions that I would like to use in a more general fashion.
Ideally I want to be able to pass the table name as a parameter in the process, because at present it is a tough code.
To use dynamic SQL to tweak in the dynamic table name from the I parameter I tried to replace all the existing SQL, although I was thinking that the table is referenced in another way Any easy way to do this?
For example:
SELECT * FROM @MyTable WHERE ...
If so, How do I set up myTable variable?
I am using SQL Server 2005.
Dynamic SQL is the only way, but I reconsider the architecture of my application if it needs to be . SQL is not very good at "normalized" code. It works best when it's designed and coded to do personal work.
Selecting from TableA table is not similar to selection from the tab, even if the selected details look the same, different indexes, different table sizes, data delivery, and so on.
You can generate your personalized stored procedures, which is a general approach. There is a code generator that creates different selected stored procedures for the required tables. Each table will have its own SP (S), which you can add in your application.
I have written these types of generators in T-SQL, but you can easily do it with most programming languages. This is a very basic thing.
Scott E brought the ORM because just to add another thing ... you should be able to use these accumulated procedures with the most refined ORM.
Comments
Post a Comment