Convert a query of views into a single query with derived tables - sql

Is it possible to convert (as in get the query as text) a query that consists of a lot of views within views into a query that is just based on the original tables?
The obvious is to go through all the views and then do it manually but wondered whether or not there was a quicker way?

It's not pretty, and like #David Faber commented, not sure how practical this will be, but here goes...
There's a whole lot of assumptions that one has to make for this to work, like
All of the views start with SELECT. (no CTE's)
The view is not enclosed in [ ] when referenced in another view
View names don't have spaces in them
This version only goes 1 level deep, but it should be possible to resolve any additional views you find in the output in a similar fashion.
And probably some more I didn't think of
I'm using the sys tables, and not the newer schema information objects, simply because I know the sys table structure better.
Assuming the following view
CREATE VIEW VW_DUMMY
AS
SELECT c.Name as Company, g.Name as [Group], gu.UserId
FROM VW_Company c
JOIN VW_Group g ON c.Id = g.CompanyId
JOIN VW_GroupUser gu ON g.Id = gu.GroupId AND gu.CompanyId = c.Id
Here's what I did.
1) Grab the view definition from syscomments for VW_DUMMY.
2) Strip off the CREATE VIEW part
2) Grab a list of objects that VW_DUMMY depends on from sysdepends
3) Grab the view definition from syscomments for all the dependant objects.
4) Strip off the CREATE VIEW part
5) Replace the name of the 'depends' object in the original view with the
definition...
1)
DECLARE #SQL VARCHAR(MAX);
SET #SQL = REPLACE((
SELECT c.text AS [text()]
FROM syscomments c
WHERE c.id = OBJECT_ID('VW_DUMMY')
FOR XML PATH('')), '
', '');
2)
SET #SQL = SUBSTRING(#SQL, PATINDEX('%SELECT%', #SQL), LEN(#SQL))
3), 4) and 5)
SELECT #SQL = REPLACE(#SQL, ' ' + name + ' ', '(' + SUBSTRING(text, PATINDEX('%SELECT%', text), LEN(text)) + ') ')
FROM (
SELECT DISTINCT OBJECT_NAME(depid) as name,
REPLACE((
SELECT c.text AS [text()]
FROM syscomments c
WHERE c.id = d.depid
FOR XML PATH('')), '
', '') as text
FROM sysdepends d
WHERE d.id = OBJECT_ID('VW_DUMMY')
AND exists(select 1 from sysobjects c where c.id = d.depid and c.type='V')) data
SELECT #SQL
I tried in on VW_DUMMY in my database, and the output is some of the worst formatted code that you might ever see, but the result is the same as the view.
Here's the output (bad formatting is deliberate)
SELECT c.Name as Company, g.Name as [Group], gu.UserId
FROM(SELECT *
FROM Company
) c
JOIN(SELECT *
FROM [Group]
) g ON c.Id = g.CompanyId
JOIN(SELECT *
FROM [GroupUser]
) gu ON g.Id = gu.GroupId AND gu.CompanyId = c.Id
Does that help?

Related

T-SQL Query to get table dependencies for all tables in database

I have the start of a query below which gives me dependencies of a particular table:
SELECT DISTINCT OBJECT_NAME(object_id) AS referencing_object_name
FROM sys.sql_dependencies
WHERE referenced_major_id = OBJECT_ID('TABLE_NAME_HERE')
But is there a way to alter this to show:
1) The above with a column with tablename being populated
2) The above relating to ALL tables within a set database (not just a single table as the original query shows)
3) All results on a single row
Final output looking like image below:
screenshotoffinalquery
.
.
.
EDIT:
I seem to be very close with this, but last column is duplicating a single result
SELECT DISTINCT b.name, a.referenced_major_id, b.object_id,
substring((
SELECT ' || ' +OBJECT_NAME(a.object_id)
FROM sys.sql_dependencies a JOIN sys.tables b ON a.referenced_major_id = b.object_id
For XML PATH ('')
), 2, 1000) AS [TextLine]
FROM sys.sql_dependencies a JOIN sys.tables b ON a.referenced_major_id = b.object_id
ORDER BY b.name ASC

Complex SQL Pivot query

A quick background so that my problem makes sense: The system collects data from the user in the form of questionnaires. Users belong to Organisations, Organisations belong to Sectors, and Questions/Calculations (as found on the questionnaires) differ across the Sectors. (Questions are answered by users ; Calculations are calculated by the system).
The following tables exist:
Sectors (SectorID, Name)
Organisations (OrganisationID, Name, SectorID)
Years (YearID, Name)
Questions (QuestionID, DisplayText, CommonName, SectorID)
Answers (AnswerID, Answer, OrganisationID, YearID, QuestionID)
Calculations (CalculationID, DisplayText, CommonName, SectorID)
CalculationResults (CalculationResultID, Result, OrganisationID, YearID, CalculationID)
I need to display data in the following way:
The thing that makes this particularly complex (for me) is that questions are displayed (to the user) in different ways across the different sectors that they belong to, but some of them can still be common questions. E.g. "Manufacturing sales" would be the same thing as "Sales (manufacturing)". I need to be using the CommonName field to determine commonality.
I've managed to use SQL Pivot to get close to what I want - SQL Fiddle (if you run the SQL you'll notice the nulls and the "commonality" issue). However some things are missing from my attempt:
Commonality and column names - I need the column names to be the CommonName field, not the QuestionID field.
I've only selected from the Answers table - I need to also select from the CalculationResults table which is identically structured.
Edit: Desired result with the SQL Fiddle data is:
(The two blocks with the orange corners need to shift all the way to the left, so that there are a total of 3 columns for the Questions - the 3 unique CommonName values. The next 3 columns are for the 3 unique CommonName values for Calculations. I hope I've made sense, if not let me know.)
Edit2: Another edit just for fun. I've definitely thought about redesigning the db but it's not an option at this stage - too risky on this legacy system. In case anyone saw the design and thought that. I need a solution in the form of Pivot hopefully.
Sometimes instead of PIVOT you can use [Aggregate](CASE EXPRESSION) to get the same data. And sometimes it's faster.
For your problem you can use OUTER APPLY with dynamic MAX(CASE)
DECLARE #Questions NVARCHAR(MAX),
#Calculations NVARCHAR(MAX),
#Sql NVARCHAR(MAX)
SELECT #Questions = COALESCE(#Questions + ', ', '')
+ 'MAX(CASE WHEN q.CommonName = ''' + CommonName + ''' THEN a.Answer END) AS ' + QUOTENAME(CommonName)
FROM Questions
GROUP BY CommonName
SELECT #Calculations = COALESCE(#Calculations + ', ', '')
+ 'MAX(CASE WHEN c.CommonName = ''' + CommonName + ''' THEN cr.Result END) AS ' + QUOTENAME(CommonName)
FROM Calculations
GROUP BY CommonName
SET #Sql = N'
SELECT
o.Name As [Organisation],
y.Name As [Year],
q.*,
c.*
FROM
Organisations o
CROSS JOIN Years y
OUTER APPLY (
SELECT ' + #Questions + '
FROM Answers a
JOIN Questions q ON a.QuestionID = q.QuestionID
WHERE a.OrganisationID = o.OrganisationID
AND a.YearID = y.YearID
) q
OUTER APPLY (
SELECT ' + #Calculations + '
FROM CalculationResults cr
JOIN Calculations c ON cr.CalculationID = c.CalculationID
WHERE cr.OrganisationID = o.OrganisationID
AND cr.YearID = y.YearID
) c
'
SQL FIDDLE DEMO
Basically we want to get the order of the QuestionID Grouped By SectorID, and Name.
You can do this using PARTITION BY with something like this:
ROW_NUMBER() OVER(PARTITION BY q.SectorID, y.Name ORDER BY a.QuestionID)
this should do it:
DECLARE #cols AS NVARCHAR(MAX)
, #query AS NVARCHAR(MAX);
SELECT #cols = STUFF(
(SELECT DISTINCT
','+QUOTENAME(CAST(ROW_NUMBER() OVER(PARTITION BY q.SectorID
, y.Name ORDER BY a.QuestionID) AS VARCHAR(10)))
FROM Answers a
LEFT JOIN Years y ON a.YearID = y.YearID
LEFT JOIN Organisations o ON a.OrganisationID = o.OrganisationID
LEFT JOIN Questions q ON a.QuestionID = q.QuestionID
FOR XML PATH(''), TYPE).value
('.', 'NVARCHAR(MAX)'), 1, 1, '');
SET #query = '
SELECT Organisation, Year, '+#cols+' from
(
SELECT QuestionID = ROW_NUMBER() OVER(PARTITION BY q.SectorID
, y.Name ORDER BY a.QuestionID)
, o.Name AS Organisation
, y.Name AS Year
, a.Answer
FROM Answers a
LEFT JOIN Years y ON a.YearID = y.YearID
LEFT JOIN Organisations o ON a.OrganisationID = o.OrganisationID
LEFT JOIN Questions q ON a.QuestionID = q.QuestionID
) src
pivot
(
max(Answer)
for QuestionID in ('+#cols+')
) piv
order by Organisation, Year
';
PRINT(#query);
EXECUTE (#query);
RESULT:

Append Table Name to Field Name with Select *

Sorry if this is a duplicate. I have searched but only find aliasing fields and tables.
I have a query:
SELECT *
FROM MyTable1 ca LEFT OUTER JOIN MyTable2 dcn ON dcn.dstrct_code = ca.dstrct_code
LEFT OUTER JOIN MyTable2 cdn ON cdn.dstrct_code = ca.cost_dstrct_cde
LEFT OUTER JOIN MyTable3 bb ON bb.supplier_code = ca.supplier_code
WHERE ca.dstrct_code = '0001'
AND ca.req_232_type = 'P'
AND ca.requisition_no = '264982 000'
AND ca.alloc_count = '01'
ORDER BY ca.alloc_count ASC
Please dont shoot me down for using * im not done with the query yet. If I execute this query I get a row of data however the tables I am selecting from all have a good number of fields and many are simularly named. So my question is... Is there anyway to select * from and append the table name to the field name so it is more obvious which field belongs to which table?
I don't think there's a way to do that directly but you can do this instead. Run a query like this:
SELECT
(case t.name when 'MyTable1' then 'ca' when 'MyTable2' then 'dcn' when 'MyTable3' then 'cdn' when 'MyTable4' then 'bb' end)
+ '.' + c.name
+ ' AS "' + t.name + '.' + c.name + '",'
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE t.name in ('MyTable1', 'MyTable2', 'MyTable3', 'MyTable4')
ORDER BY t.name
Run it, preferably with results to Text (Ctrl+T), and use the results instead of the * in your original query. You have to manually remove the comma from the last line.
If you like the approach, you could streamline the process with some dynamic SQL.

carriage return in sql server 2012

Hey I am using the following query to display the problem list separated by commas.
SELECT tt.VrNo, STUFF((select ','+ Er1.ErrorDesc
from ( select * from CallRegErrors )as Main
left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode
WHERE (main.VrNo = tt.VrNo)
FOR XML PATH('')) ,1,1,'') AS Problemlist
query is giving the output like a,b,c,d etc
But my actual requirement is I want to display each error description in a new line like,
a
b
c
d
etc
I tried the following query for it:
SELECT tt.VrNo, STUFF((select char(13)+char(10)+ Er1.ErrorDesc
from ( select * from CallRegErrors )as Main
left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode
WHERE (main.VrNo = tt.VrNo)
FOR XML PATH('')) ,1,1,'') AS Problemlist
and also i have used
SELECT tt.VrNo,Replace(STUFF((select ','+ Er1.ErrorDesc as [text()] from ( select * from CallRegErrors )as Main left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode
WHERE (main.VrNo = tt.VrNo)
FOR XML PATH('')),1,1,''),',',char(13)+char(10)) AS Problemlist
from (select main.VrNo, Er1.ErrorDesc from ( select * from CallRegErrors )as Main left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode )as tt
group by tt.VrNo
but now get the problem list seperated by spaces instead of commas after using the above query
but its does not give the output that i want.
please help..
Thanks in advance
I think we need more information before we can help you.
I think you are trying to format the information at the child level in a parent child relationship into a list. You probably saw something like this blog on the web.
However, your query is not correctly formatted.
Is the ErrorMaster (Production.ProductCategory) the parent and CallRegErrors (SUB.ProductCategoryID) the child?
If so just change the query to those table name field names for it to work.
I used the REPLACE function on the overall result to change COMMAS to CR + LF.
-- Sample database
USE AdventureWorks2012
GO
-- Change SQL from www.sqlandme.com for this users problem
SELECT
CAT.Name AS [Category],
REPLACE(STUFF((
SELECT ',' + SUB.Name AS [text()]
FROM Production.ProductSubcategory SUB
WHERE SUB.ProductCategoryID = CAT.ProductCategoryID
FOR XML PATH('')
), 1, 1, '' ), ',', CHAR(13) + CHAR(10))
AS [Sub Categories]
FROM Production.ProductCategory CAT
You can only see carriage returns in the output window when the type is set to TEXT in SSMS.
I hope this solves your problem. If not, please write back with more information!!

Visual Studio 2010 and Query Designer SQL Problem with XML Path

I have an awesome Visual Studio 2010 SQL frustration:
I found code on StackOverflow on how to concatenate strings from multiple rows onto a single row by using FOR XML PATH('').
When I write the following line in Query and View Designer (because, presumably, there is no other way to write SQL in Visual Studio and then save it as a "View"), I get results that are not what I want:
Code:
Names AS
(SELECT DISTINCT
Z.code,
(SELECT DISTINCT CAST(B.fname + ' ' + B.lname + ' ' AS VARCHAR(MAX))
FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id
WHERE (A.code = Z.code) FOR XML PATH('')) AS EmployeeNames
FROM Employees AS Z)
I get a result like:
Code EmployeeNames
---- -------------------------
1234 <Expr1>First Last</Expr1>
Why? Because Visual Studio 2010, in all it's glory, adds "as Expr1" to the following:
(SELECT DISTINCT CAST(B.fname + ' ' + B.lname + ' ' AS VARCHAR(MAX))
FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id
WHERE (A.code = Z.code) FOR XML PATH('')) *as Expr1*
Is there any work-around for this? I've noticed that the problem comes with the Query and View Designer Parser... and I can intentionally break the parser by putting a random function that the parser doesn't support (like OVER()) somewhere in my code. This fixes the problem, and removes the XML tags, but seems unnecessary.
Does anyone else have this problem? Does anyone know a work-around?
Thank you very much for your time and effort with my problem.
I ran into something simular and the fix for me was to define the Cast in each case.
Example:
Names AS
(SELECT DISTINCT
Z.code,
(SELECT DISTINCT CAST(B.fname AS varchar(10) + ' ' + CAST(B.lname AS varchar(10) + ' ')
FROM Records AS A LEFT OUTER JOIN Employees AS B ON A.id = B.id
WHERE (A.code = Z.code) FOR XML PATH('')) AS EmployeeNames
FROM Employees AS Z)