Expand a view or views referenced in a query
I think it would be great if SQL Prompt had the functionality to expand a view that is used in a query. So if you have this schema:
CREATE TABLE dbo.Person
(
PersonID INT IDENTITY(1, 1)
PRIMARY KEY,
LastName VARCHAR(30),
FirstName VARCHAR(30),
);
GO
CREATE TABLE dbo.Student
(
StudentID INT IDENTITY(1, 1)
PRIMARY KEY,
PersonID INT,
StudentNo VARCHAR(100)
);
GO
CREATE TABLE dbo.Class
(
ClassID INT IDENTITY(1, 1)
PRIMARY KEY,
ClassName VARCHAR(100)
);
GO
CREATE TABLE dbo.ClassRoster
(
ClassID INT,
StudentID INT
);
GO
CREATE VIEW dbo.Students
AS
SELECT
S.StudentID,
P.LastName,
P.FirstName,
S.StudentNo
FROM
dbo.Person AS P
JOIN dbo.Student AS S
ON P.PersonID = S.PersonID;
GO
CREATE VIEW dbo.Classes
AS
SELECT
C.ClassID,
C.ClassName,
CR.StudentID
FROM
dbo.Class AS C
JOIN dbo.ClassRoster AS CR
ON C.ClassID = CR.ClassID;
GO
Then you have this query:
SELECT
S.StudentID,
S.LastName,
S.FirstName,
S.StudentNo,
C.ClassID,
C.ClassName,
C.StudentID
FROM
dbo.Students AS S
JOIN Classes AS C
ON S.StudentID = C.StudentID;
You could right-click or have a keyboard combination that say Expand Views and the query becomes:
SELECT
S.StudentID,
S.LastName,
S.FirstName,
S.StudentNo,
C.ClassID,
C.ClassName,
C.StudentID
FROM
(
SELECT
S.StudentID,
P.LastName,
P.FirstName,
S.StudentNo
FROM
dbo.Person AS P
JOIN dbo.Student AS S
ON P.PersonID = S.PersonID
) AS S
JOIN (
SELECT
C.ClassID,
C.ClassName,
CR.StudentID
FROM
dbo.Class AS C
JOIN dbo.ClassRoster AS CR
ON C.ClassID = CR.ClassID
) AS C
ON S.StudentID = C.StudentID;
I would find this useful when working with third party products or when coming in new to an environment as it would immediately show me the places where there are multiple references to the same table or where a view is providing some layer of filtering that I'm not aware of.
-
Adrian Brough commented
This would be really useful when dealing with auto-generated code from MS Access which already needs cleaning up and has further levels of redundancy. Would save me hours a week and probably lead to significant performance gains as noted by Aaron Brindell.
-
Aaron Brindell commented
One strong point toward this suggestion; when views are nested, the query optimizer will have more difficulty properly identifying and using indexes on the underlying tables, causing significantly less efficient queries. However, views can be incredibly useful in encapsulating code and keeping code redundancy to a minimum.
To that end, having the ability to expand views in place would be tremendously helpful in creating speed-optimized code while still being able to write much shorter code up-front.
In an ideal world, this could be coupled with some kind of a "Smart Alter" functionality for views that would go back and edit expanded view code within other objects to match changes to the base view, even if the view isn't directly referenced as a view. This would certainly be tricky, but searching for exact matches of view text could be a start, maybe using some kind of Red Gate specific text flag (--VIEWMATCH dbo.MyView) or something could also work?
-
Jack commented
This is not how I format my sql. The forum just didn't keep the formatting :-D