SQL Server : reverse engineering view and stored procedure - sql-server

I need some help from your side.
For example I have a SQL Server view MyView.
Adb.vs.MyView
with columns:
ID
Name
Address
Email
Phone
Logic behind the view is next
SELECT
Ac.AccountID AS ID,
Ac.AccountName AS Name,
Ad.Main_Adress AS Address,
Em.Main_Email AS Email,
Concat(Ph.Phone_Area_Code,Ph.Mobile_Phone_Number) AS Phone
FROM
Bdb.dbo.Account AS Ac
INNER JOIN
Cdb.dbo.Address AS Ad ON Ac.AccountID = Ad.AccountID
INNER JOIN
Cdb.dbo.Emails AS Em ON Ac.AccountID = Em.AccountID
INNER JOIN
Cdb.dbo.PhoneBook AS Ph ON Ac.AccountID = Ph.AccountID
NOTE:
No KEY relations build between all this tables.
My target to reverse engineer this view to get next kind of result:
Please suggest any kind of tool/tools or scripts to perform this.
Also, if somebody know similar solution but for Rev.En. stored procedures which was used to populating data into tables I will be really appreciated
bec. I will need to reverse tons of such kind views and stored procedures in nearest future.
Thanks in advance for any kind of support !

Take a look at the system views. Most of what you want is probably available in INFORMATION_SCHEMA.VIEW_COLUMN_USAGE. For example:
USE Adb
GO
Select * from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
where VIEW_NAME='MyView'
GO

Views are just stored SQL scripts that SQL Server adds into your query as a sub select. Consequently the fields used are not actually saved within SQL Server in the same way that table definitions are. Your best bet is to script out all your views using SQL Server Management Studio and plug the files into a tool such as the General SQL Parser which can output the columns and tables that are used in that script.
It isn't perfect but should get you a long way towards what you are trying to achieve. You can try it for free here.

Related

What is the best way to cross reference two SQL Views in Report Builder?

I'm new to SQL queries and need to build a custom report in Microsoft SQL Report Builder 3.0. The data source is a SCCM database. I need help with understanding the best approach to achieve the following:
We need to cross reference if a computer exist in two Views, if so show that name in the report.
InputParameter1 = "Please select a View"
InputParameter2 = "Cross reference with this other View"
If I know the names of the Views beforehand I have a query to get what I need from the SQL server, but I need to create a parameter-based report where you select two Views dynamically and the report cross reference them for you and present which computers exists in both Views.
This is the query I can use for a static result. v_CM_RES_COLL_CMS0020B and v_CM_RES_COLL_CMS000D1 are examples of names of possible Views, until I can solve the parameter issue in Report Builder:
SELECT v_GS_SYSTEM.Name0
FROM v_GS_SYSTEM
WHERE Name0 IN
(SELECT Name from v_CM_RES_COLL_CMS0020B)
AND Name0 IN
(SELECT Name from v_CM_RES_COLL_CMS000D1)
I don't know how to proceed in how to make the above query into a parameter report in Report Builder. Somehow I need to change v_M_RES_COL_CMS00### to what ever the user inputs to the parameters. Does anyone know how? Any help is greatly appreciated.

Finding all views that are using linked server

I am updating url for linked servers. Before make the changes, I would like to know all views that have reference to this linked servers. Is there any programmatic way (TSQL) to perform this task?
Thanks for your help.
I am using SQL Server 2005, 2008 and 2012. The database servers that referencing linked servers are mostly SQL Server 2005
While it may return false positives, and won't capture any cases where a four-part name is constructed using dynamic SQL, this is probably the simplest approach:
SELECT name FROM sys.views
WHERE LOWER(OBJECT_DEFINITION([object_id])) LIKE LOWER('%LinkedServerName%');
This will find the views:
SELECT t2.name, OBJECT_DEFINITION(t1.[object_id]) view_definition
FROM sys.views t1 join sys.servers t2 on
OBJECT_DEFINITION(t1.[object_id]) like '%['+ t2.name + '].%' ESCAPE '['
It can fail if a table, view, schema or database has same name as a linked server.
In case some views have eluded the first check you can add this line this part is not checking for the square brackets surrounding the linked server name. But be aware that this part is more likely to include
extra unwanted views
or OBJECT_DEFINITION(t1.[object_id]) like '% '+ t2.name + '.%'
EDIT: Changed sys.sysservers to sys.servers. Thanks Aaron Bertrand
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
Try This:
SELECT name, OBJECT_DEFINITION([object_id]) FROM sys.views
where OBJECT_DEFINITION([object_id]) like '%.%.dbo.%'

Loading Fact Tables with SQL Server

I'm creating a warehouse using SQL Server 2008 and Analysis Services. I've managed to create and populate the dimension tables, but I'm having a lot of trouble writing the SQL for loading the fact table. For one thing, I'm not sure how to load the keys of the fact table with the PKs from the dimension table. I tried writing a query that had a series of JOINs to get the keys and the measures I want, but the statement got so complicated that I got lost.
This is the star schema that I have to work from:
http://i.imgur.com/C3DGj.png
What am I doing wrong? I have a feeling that I'm missing something pretty basic, but I'm fairly new to this and most of the information I found online seemed to deal with using SSIS, which I don't have installed.
Any help would be appreciated.
Todays Data Warehouse Developer uses SSIS for loading dimensional models. Typically, lookups are used to convert the dimensional attribute into a key. Most of the time, the data is going to be on another server or in a flat file or something else that forces you to use an ETL tool like SSIS, but in your case, you can get it done without. If your enterprise is serious about BI, you should push to get SSIS installed and learn it.
For your situation, assuming you have a table loaded with raw facts locally, you should be able to do an insert/select.
Basically, you'll want to inner join (since you've had no problems populating the dimension tables) each dimension to the raw facts table. Something like:
INSERT trainingcentrefact
(timekey,locationkey,instructorkey,coursekey,paid,notpaid,... etc)
SELECT
t.timekey
,l.locationkey
,i.instructorkey
,c.coursekey
,rf.paid
,rf.notpaid
,... etc
FROM rawfacts rf
INNER JOIN timedimension t ON rf.time = t.time
INNER JOIN locationdimension l on rf.location = l.location
INNER JOIN instructordimension i on rf.instructor = i.instructor
INNER JOIN coursedimension c on rf.course = c.course

Search Query - SQL Server 2005 - Ideas - Knowledge Sharing

Currently I am designing a database schema where one table will contains details about all students of a university.
I am thinking the way how can I create the search engine query for administrators where they will search for students. (Some properties are Age, Location, Name, Surname etc etc) (approx 20 properties - 1 table)
My idea is to create the sql query dynamically from the code side. Is it the best way or is there any other better ways?
Shall I use a stored procedure?
Is there any other ways?
feel free to share
I am going to assume you have a front end that collects user input, executes a query and returns a result. I would say you HAVE to create the query dynamically from the code side. At the very least you will need to pass in variables that the user selected to query by. I would probably create a method that takes in the key/value search data and use that to execute the query. Because it will only be one table there would probably be no need for a view or stored procedure. I think a simple select statement including your search criteria will work fine.
I would suggest you to use LINQ to SQL and this will allow you to write such queries just in C# code without any SQL procedures. LINQ to SQL will care about security and prevent SQL injections
p.s.
Do not ever compose SQL from concatenated strings like SQL = "select * from table where " + "param1=" + param1 ... :)

Directly query TFS Warehouse using T-sql

Hi I'm wondering if anybody has had any experience querying a Team Foundation Server store to retrieve current work item information. It is my understanding that the information I require is stored in the TFSWarehouse Database.
I'm well aware that there are APIs that provide this functionality and I've used them myself. However these come with the requirement to install Team Explorer on any client PC that will reference the API.
All I want is a list of Work Items with some selected fields such as Title, State and Created By.
Has anybody attempted this themselves, and indeed does anybody know of any pitfalls with this approach?
---------- EDIT 16/02
Just to add after speaking with TreeUK. What would be great is if anybody perhaps has an example query, or maybe some explanation as to the table structure?
If you have access to the SQL database that hosts TFS, you can write a query that pulls this information out.
The database you want to look at is TFSWarehouse.
Below is a query that gets Work Item general information.
select System_Id as WorkItemId, System_Title as Title,
System_State as Status, System_Reason as Reason,
System_WorkItemType as Type
from [Work Item]
It's kind of clear what they all do, check that table to get any other properties of interest.
TFS 2005, it'll need to be [workitemtracking].dbo.[workitemsare]
SELECT DISTINCT Top(100) FactWorkItemLinkHistory.WorkItemLinkTypeSK, DimWorkItemLinkType.LinkName, DimWorkItem.System_Id, DimWorkItem.System_Title
FROM FactWorkItemLinkHistory
INNER JOIN DimWorkItemLinkType ON DimWorkItemLinkType.LinkID = FactWorkItemLinkHistory.WorkItemLinkTypeSK
INNER JOIN DimWorkItem ON DimWorkItem.System_Id = FactWorkItemLinkHistory.SourceWorkItemID
WHERE FactWorkItemLinkHistory.WorkItemLinkTypeSK = 2
TFS 2013, it'll need to be [Your Tfs_Warehouse database].dbo.DimWorkItem
SELECT System_Id as WorkItemId, System_Title as Title,
System_State as Status, System_Reason as Reason,
System_WorkItemType as Type
FROM dbo.DimWorkItem

Resources