I have a stored procedure that returns a list of projectIDs at the moment. I want to extend this to return some extra information to determine if a specific user has access to this project.
To determine if a user has access I need to look up the user in a table called 'Project_XXX' where XXX is the projectID. Yes, this is shocking database design but sadly there is nothing I can do about it. My first instinct was that I could use a function as such:
SELECT ProjectID WHERE [criteria] AND myfunc(ProjectID, @username)=1
However, because the lookup needs to run dynamic SQL it tells me that I can't do this.
This leaves me with the option of putting my logic in a stored procedure but then the syntax of my select is not simple.
The way I can think of to do it is to run the select to get the list of projectIDs and then run a cursor over that table calling my stored procedure but I stopped thinking there because it seems far too complicated...
So is there some way I can do what I want with similarly simple syntax to above? Is there some way to get a user function to return based on dynamic SQL? Is there a nice way to use stored procedures as I have above? I'm sure I am missing something obvious - I rarely need to do anything complicated with SQL though. ;-)
I should also reiterate that I know that the underlying problem is with the stupid individual project tables but this is not something that can be changed.
One option would be to create a view from all the project_xxx tables. Something like:
Then you can simply query your view as though the database had been designed properly in the first place ;-)
As you state that you will be frequently adding new project tables, I would suggest you could have a security table populated with a dynamic query every morning. For example:
As you seem to know, you cannot run dynamic SQL or call a stored procedure from within a function.
There is a very ugly workaround that involves calling a batch script (.bat) from within your function using
xp_cmdshell
, and usingsp_executesql
in the batch script to execute the dynamic SQL.However, I'd go with RB's view solution for the sake of simplicity!