1

In SQL Server 2005, I have a complex view that needs to specify additional conditions on certain relations. These conditions are not known at the time of view creation. Here's a greatly simplified version.

SELECT fields FROM table1 
LEFT JOIN table2 ON ((table1.pid = table2.fid) AND (table2.condition1 = @runtimecondition));
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON .... 
LEFT JOIN table5 ON ....

Dynamic SQL to directly access the tables and do the joins is not an option for me because of 3rd party constraints (this is an integration and they want to have a single point of access for me code, preferably a view -- rather than grant access to a variety of tables). Can this be done with a view? Do I have to use a stored procedure? Is this a problem that could be addressed by a Table Valued Function?

1
  • You have to use something more powerful than a view, such as a TVF. Views cannot be parameterized. Commented May 8, 2013 at 13:47

3 Answers 3

5

You can use Inline Table-Valued function

CREATE FUNCTION dbo.Condition
 (
  @condition1 int,
  @condition2 int,
  )
RETURNS TABLE
AS
RETURN (
        SELECT *
        FROM table1 t 
          LEFT JOIN table2 t2 ON t.pid = t2.fid 
            AND t2.condition1 = ISNULL(@condition1, t2.condition1)
          LEFT JOIN table3 t3 ON t.pid = t3.fid
            AND t3.condition1 = ISNULL(@condition2, t3.condition1)
          LEFT JOIN table4 ON .... 
          LEFT JOIN table5 ON ....
        )

Demo on SQLFiddle

For improving performance use this indexes:

CREATE INDEX x ON dbo.table1(pid)
CREATE INDEX x ON dbo.table2(condition1) INCLUDE(fid, pid)
CREATE INDEX x ON dbo.table3(condition1) INCLUDE(fid, pid)

Plan Diagram(on example of the three tables)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You can expose field(s) of interest to list of produced fields:

CREATE VIEW myview AS
SELECT fields,
     table2.condition1 AS condition1
FROM table1 
LEFT JOIN table2 ON (table1.pid = table2.fid);
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON .... 
LEFT JOIN table5 ON ....

This lets VIEW users impose conditions when they use your view, something like this:

SELECT * FROM myview
WHERE condition1 = @runtimecondition

2 Comments

But in this case semantic of queries will be different.sqlfiddle.com/#!3/d9824/2
Yes--that's exactly the problem I was running into. The queries don't seem to be logically equivalent--I get different result sets.
0

This one could be less efficient than the function table (but I don't think it will).

View defined as :

SELECT
    fields
    , table2.fid AS t2fid
    , table2.condition1 AS t2condition1 
FROM table1 
LEFT JOIN table2 ON (table1.pid = table2.fid)
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON .... 
LEFT JOIN table5 ON ....

View called like:

SELECTT
    fields
FROM dbo.MyView
WHERE (
    t2fid IS NULL
    OR
    t2condition1 = @runtimecondition
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.