parameters for view in SQL server - sql

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?

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)

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

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
)

Related

SQL - how to rename column being used on inner join

I have created a view in Microsoft SQL Server Management Studio, and I now want to join it to the correct spatial information.
I have created the query below
select * from v_postal_address_view pa
INNER JOIN SPATIAL_INFO sp ON sp.ECAD_ID = pa.BUILDING_ID
This runs fine as the ECAD_ID and BUILDING_ID are linked. However, I now want to save my results as a new view but cannot because the v_postal_address_view contains ECAD_ID already, therefore forbidding the SPATIAL_INFO tables ECAD_ID to be joined as the name is not unique.
How can I rename the ECAD_ID column in spatial_info during the inner join to the view??
CREATE VIEW RESOLVE_AMBIGUOUS_NAMES AS
SELECT T1.id AS id_1,
T2.id AS id_2
FROM TABLE_1 T1
INNER
JOIN TABLE_2 T2
ON T1.fk = T2.pk

Multiple table combined PIVOT table using SQL

I have multiple tables in my data set. I need two columns from one table to be pivoted against one column in a different table.
Code:
SELECT DISTINCT
table1.name,
table6.count,
table6.car_name
From Master_Table
Inner Join table1
On Master_Table.ID_SEQNO = table1.ID_SEQNO
Inner Join table2
On table1.table1_ID_SEQNO = table2.table1_ID_SEQNO
Inner Join table3
On table2.ID_SEQNO = table3.ID_SEQNO
Inner Join table4
On table3.ID_SEQNO = table4.ID_SEQNO
Left Outer Join table5
On table4.CANDIDATE_ID_SEQNO = table5.CANDIDATE_ID_SEQNO
Left Outer Join table6
On table5.ID_SEQNO = table6.ID_SEQNO
ORDER BY table1.name
Output I need:
What I have now :
Master_Table : Has names , Date, Time of ownership etc
table1 has info on each name , adress phone number email etc
table2 has info like cars insurance etc so on... I have used id_seqno to join statements without which the code doesn't work.
Each table has a id_seqno with which I map it to every other table... I am modifying an existing code so pretty new to this.

How to select records that do not exist in two (or more) tables

I have 3 tables of accounts that all contain the same fields. Table1 contains all accounts while Table2 and Table3 contain subsets of the accounts. I'm trying to select records in Table1 that do no exist in Table2 or Table3.
Let's say the table layout is like this and is the same for all 3 tables:
|AcctNum|Name|State|
I know how to do this if it was just Table1 and Table2, using a left join and Is Null, but the 3rd table is throwing me. Is this possible to do in one query? Can you combine left joins? I should point out I'm using Access 2010.
Yes you can combine left joins and with the odd syntax Access uses the query should look like this:
SELECT T1.AcctNum
FROM (Table1 AS T1 LEFT JOIN Table2 AS T2 ON T1.AcctNum = T2.AcctNum)
LEFT JOIN Table3 AS T3 ON T1.AcctNum = T3.AcctNum
WHERE (((T2.AcctNum) Is Null) AND ((T3.AcctNum) Is Null));
You can use Access to create a view called TableCombined that is a union of both Table2 and Table3.
At that point, you can use your left join and Is Null query and join TableCombined to Table1.
Hope this helps!
You can also do a NOT EXISTS statement which makes sense logically for what you are trying to achieve.
For example:
SELECT ACCTNUM
FROM TABLE1
WHERE NOT EXISTS (SELECT TABLE2.ACCTNUM FROM TABLE2 INNER JOIN TABLE3 WHERE TABLE2.ACCTNUM IS NULL AND TABLE3.ACCTNUM IS NULL)

MS Access Inner Join On 3 Tables with the same field_name

I'm doing an assignment for class, and I'm at my whits end. Basically, I need to write a query that uses INNER JOIN's to combine all the data in 4 tables, while avoiding having titles columns with identical names (Ie Table1.Master_Number and Table2.Master_Number). The literal instructions are as follows:
Step 1: Create a view of all the columns (only list the columns once if the columns are duplicated in the tables) in all the tables of the Lecture 5 database. Save this query as Lecture_5_View and us this query to build the following 3 queries. In the “FROM” clause, you MUST use an “INNER JOIN” in this query. Remember ACCESS does not support the “CREATE VIEW” command so you must create a select query. `
Here is a screenshot of the database, that shows all the headings and column headings.
Based on other posts like this, I thought that it would be pretty simple. I have this so far, but it does not want to run
(ie Syntax error(missing operator) in query expression 'Table1.Master_Number = Table2.Master_Number INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number)
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.Master_Number = Table2.Master_Number
INNER JOIN Table4 ON Table1.Master_Number = Table4.Master_Number
Where am I going wrong so far and how to I accomplish this query?
Much thanks,
Josh
With Access you need to use parentheses when doing more than one join:
SELECT *
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This is essentiall spliting down you query so you have at most one join per section, i.e. Your First query is:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number;
Then you have:
SELECT *
FROM YourFirstQuery
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
This differs slightly from subqueries as you are still able to reference all fields from Table1 and Table2.
EDIT
To avoid duplicating columns you will need to explicitly list the columns you want (although you should be doing this anyway):
SELECT Table1.Master_Number,
Table1.Asset_Tag,
Table1.Serial_Number,
Table2.Last_Name,
Table2.First_Name,
Table4.Office_Number,
Table4.Location,
Table4.Department
FROM (Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number;
Try this:
SELECT * FROM ((Table1
INNER JOIN Table2
ON Table1.Master_Number = Table2.Master_Number)
INNER JOIN Table4
ON Table1.Master_Number = Table4.Master_Number)

sql access inner join of 4 tables

I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.
Build using the query design window
Then switch to sql view
Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.