Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

When creating a view, execute code found in a table

$
0
0

I use SQL Server 2012; we have a table where the user can save data needed to build a view. This table looks like this:

terminal selection_code ABC123 AND emp.Employee_Code = 'JOHN'

We need to create a view that joins the "real" data table with the selections table mentioned above (where the selection criteria is stored). The view should look like this:

CREATE VIEW new_view AS SELECT emp.* FROM employee_table emp WITH (NOLOCK) INNER JOIN selections_table sel WITH (NOLOCK)  ON sel.terminal = 'ABC123' WHERE emp.Employer = 'Amazon' sel.selection_code

As you can see, the last line above has to be "dynamic"; in other words, when the view code executes, it should use the current contents of the selections table to display the view records. I'm not sure if this can be achieved, or how.

Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>