Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaztk committed Sep 4, 2022
1 parent ab6a8ae commit 28be38d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Lineage for Microsoft SQL Server T-SQL Queries
# Data Lineage for Microsoft T-SQL Queries

Data Lineage Transact SQL (T-SQL) for [Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server) enables you to find the data origins and data destinations in your query. It gives you the visibility over query data columns and ability to track the changes over time.
Data Lineage Transact SQL (T-SQL) for [Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server) or [Azure SQL Server](https://azure.microsoft.com/en-us/services/sql-database/campaign/) enables you to find the data origins and data destinations in your query. It gives you the visibility over query data columns and ability to track the changes over time.


# Features
Expand Down Expand Up @@ -37,23 +37,30 @@ Run **TSQL_data_lineage.sql** file to create a lineage procedure. This script

```sql
-- Get your query:
declare @test_query VARCHAR(8000) = '
DECLARE @test_query VARCHAR(MAX) = '
-- This is a sample query to test data lineage
SELECT
s.[BusinessEntityID]
,p.[Title]
,p.[FirstName]
,p.[MiddleName]
,p.[LastName]
-- ,p.[LastName]
,p.[Suffix]
,e.[JobTitle] as imeSluzbe
,e.[JobTitle] as JobName
,p.[EmailPromotion]
,s.[SalesQuota]
,s.[SalesYTD]
,s.[SalesLastYear]
,( SELECT GETDATE() ) AS DateNow
,( select count(*) FROM [AdventureWorks2014].sales.[SalesPerson] ) as totalSales
/*
Adding some comments!
*/
FROM [AdventureWorks2014].sales.[SalesPerson] s
LEFT JOIN [AdventureWorks2014].[HumanResources].[Employee] e
ON e.[BusinessEntityID] = s.[BusinessEntityID]
Expand Down
55 changes: 40 additions & 15 deletions TSQL_data_lineage.sql
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,33 @@ CREATE TABLE dbo.SQL_query_table (
******************************** */



DECLARE @orig_q VARCHAR(MAX)
SELECT @orig_q = COALESCE(@orig_q + ', ', '') + sp_text_fin
FROM dbo.TK_RES
order by rn asc

DROP TABLE IF EXISTS TK_TEST2


DECLARE @stmt2 NVARCHAR(MAX)
SET @stmt2 = REPLACE(REPLACE(@orig_q, CHAR(13), ' '), CHAR(10), ' ')


select
TRIM(REPLACE(sp_text_fin, ' ','')) as val
,dbo.fn_removelistChars(sp_text_fin) as val_f
TRIM(REPLACE(value, ' ','')) as val
,dbo.fn_removelistChars(value) as val_f
,row_number() over (ORDER BY (SELECT 1)) as rn
INTO TK_TEST2
from TK_RES
where sp_text_fin <> ' '
from string_split(REPLACE(@stmt2, CHAR(13), ' '), ' ' )
WHERE
REPLACE(value, ' ','') <> ' '
OR REPLACE(value, ' ','') <> ' '



DROP TABLE IF EXISTS dbo.TK_RES

-- DROP TABLE IF EXISTS dbo.TK_RES


-- @token = @tokenen
Expand Down Expand Up @@ -283,8 +296,7 @@ BEGIN

SET @ttok = ' ' + @token + ' as ('
--IF (@ttok NOT IN (SELECT @token))
-- IF (@ttok NOT IN (SELECT @stmt2))
IF (@ttok NOT IN (SELECT @InputQuery))
IF (@ttok NOT IN (SELECT @stmt2))
INSERT INTO @table (tik, tok, order_)
SELECT @token_i, @token, @order

Expand Down Expand Up @@ -315,8 +327,10 @@ INTO dbo.fin_res
FROM @table


SELECT * FROM dbo.fin_res

SELECT tik AS Clause_name
,tok AS Object_Name
,rn AS order_DL
FROM dbo.fin_res


-- END of procedure
Expand All @@ -326,26 +340,36 @@ GO



/* **************************
*
* -- TEST functionalities
*
************************* */

-- TEST

declare @test_query VARCHAR(8000) = '
DECLARE @test_query VARCHAR(MAX) = '
-- This is a sample query to test data lineage
SELECT
s.[BusinessEntityID]
,p.[Title]
,p.[FirstName]
,p.[MiddleName]
,p.[LastName]
-- ,p.[LastName]
,p.[Suffix]
,e.[JobTitle] as imeSluzbe
,e.[JobTitle] as JobName
,p.[EmailPromotion]
,s.[SalesQuota]
,s.[SalesYTD]
,s.[SalesLastYear]
,( SELECT GETDATE() ) AS DateNow
,( select count(*) FROM [AdventureWorks2014].sales.[SalesPerson] ) as totalSales
/*
Adding some comments!
*/
FROM [AdventureWorks2014].sales.[SalesPerson] s
LEFT JOIN [AdventureWorks2014].[HumanResources].[Employee] e
ON e.[BusinessEntityID] = s.[BusinessEntityID]
Expand All @@ -356,4 +380,5 @@ FROM [AdventureWorks2014].sales.[SalesPerson] s


EXEC dbo.TSQL_data_lineage
@InputQuery = @test_query
@InputQuery = @test_query

0 comments on commit 28be38d

Please sign in to comment.