I have two views that returns the same columns, but different rows.
I am creating a stored procedure that uses these views. The stored procedure is big and searches across columns for rows that match.
Now, my wish is this: If i receive a bit parameter with the value 0, then i should get information from one view, if the parameter is 1 then i should get the information from another view.
I have understood that dynamic SQL is a bad idea, and the stored procedure i am working with is already quite complex. Recreating it to be dynamic is not an option.
I have also read about it being impossible to do this:
select * from @table
Because table names should be static, and not passed in to ex. a stored procedure by code.
What i am trying to accomplish is something like this:
SELECT TOP(@top) subtaskid,activityid FROM
(CASE WHEN @allProjects=1 THEN view_project_all ELSE view_project_mine) v
however i get error messages saying “incorrect syntax near keyword ‘CASE’. I realize that this question is closely related to dynamic sql, and variable views\tables questions all over stackoverflow, however i think that since my two tables are actually in the stored procedure this is more functionality than hack…
Question is this: Is there a way to set static variables so this one select can ask different views, according to parameter given?
The simple way:
If you wish, you can create a view that unions these tables then do your queries against that view:
Query against it: