Friday, 30 November 2018

Logging MERGE statement changes using OUTPUT

We can log all the information with the help $action variable. It can be used to log the merge action.  This variable will take one of three values:  “INSERT”, “UPDATE”, or “DELETE”.

See the example:
  
EMP_ID=2 address will be updated and EMP_ID 10 and 11 will be inserted.
See below sql script
declare @MergeLog table
(ID int, MergeAction varchar(10))
MERGE Emp_Target T
Using Emp_Source S
ON
T.EMP_ID = S.EMP_ID and
T.EMP_NAME=S.EMP_NAME
WHEN MATCHED and T.EMP_ADD != S.EMP_ADD
THEN
UPDATE SET T.EMP_ADD=S.EMP_ADD
WHEN NOT MATCHED BY TARGET
THEN
INSERT VALUES (S.EMP_ID,S.EMP_NAME,S.EMP_ADD)
OUTPUT S.EMP_ID, $action into @MergeLog;

SELECT MergeAction, count(*)
FROM   @MergeLog
GROUP BY MergeAction

  
See the records in the table
  
EMP_ID=2 address will be updated and EMP_ID 10 and 11 will be inserted.

Thanks.

MERGE Statement in SQL Server to insert, update and delete at the same time

The MERGE statement is used to make changes in one table based on values matched from another.   It can be used to combine insert, update, and delete operations into one statement. The MERGE statement basically merges data from a source result set to a target table based on a condition that we specify and if the data from the source already exists in the target or not. The new SQL command combines the sequence of conditional INSERT, UPDATE and DELETE commands in a single atomic statement, depending on the existence of a record.
Syntax of the Merge statement
MERGE targetTable
Using sourceTable
ON mergeCondition
WHEN MATCHED
THEN updateStatement
WHEN MATCHED and othercondtion --optional
THEN updateStatement 
WHEN NOT MATCHED BY TARGET
THEN insertStatement
WHEN NOT MATCHED BY SOURCE
THEN deleteStatement


The merge statement works using two tables, the source Table and target Table.  The target Table is the table to be modified based in data contained within the source Table.
There will be three conditions
  • MATCHED(Source & Target)
  • NOT MATCHED BY TARGET
  • NOT MATCHED BY SOURCE

MATCHED – these are rows satisfying the match condition.  They are common to both the source and target tables. 
NOT MATCHED – This is also known as NOT MATCHED BY TARGET; these are rows from the source table that didn’t match any rows in the target table. 
NOT MATCHED BY SOURCE – these are rows in the target table that were never matched by a source record.
See the example:
Here I am having two tables known as EMP_Source and EMP_TARGET
Checking EMP_ID and EMP_NAME
If a match and EMP_ADD is not same then update EMP_ADD records in Target table.
If not match by the target, in this case, insert a record in Target table.



EMP_ID 2 address will be updated and set it to ‘PUNE’.
MERGE Emp_Target T
Using Emp_Source S
ON
T.EMP_ID = S.EMP_ID and
T.EMP_NAME=S.EMP_NAME
WHEN MATCHED and T.EMP_ADD != S.EMP_ADD
THEN
UPDATE SET T.EMP_ADD=S.EMP_ADD
WHEN NOT MATCHED BY TARGET
THEN
INSERT VALUES (S.EMP_ID,S.EMP_NAME,S.EMP_ADD);


Running this script.
  
See the result



Target has been updated from ‘Patna’ to ‘Pune’.

Tuesday, 27 November 2018

Extract SQL Profiler result in Excel file

There is no direct option to extract the SQL profile result in Excel or CSV file. We need to use fn_trace_gettable()  in SQL server.

Let’s see step by step how to extract SQL profiler result in excel file.
Open SQL server and profiler.


Here I am running below SQL query.
  
See it in profiler.
  
Now we need to save this result in .trc file
   
I saved this with name abc.trc file.
Now open the query window and run the below query
  
Now we can save the query result in excel or csv file.
select  * from fn_trace_gettable('D:\abc.trc',default)
   
Here I am saving it in csv format.
   
We can add the filer on the function to retrieve the profiler result for the particular user.
select TextData,ApplicationName,LoginName,StartTime,EndTime,
  ServerName,NTDomainName,NTUserName from fn_trace_gettable('D:\abc.trc',default)
  where NTUserName='Bagesh'




SQL Server files names and versions

When we are opening SQL server file in the program, we are getting files with name 80, 90,100,110,120 etc. Do we know what the means of these files?
  
  

Shared Files for All Instances of SQL Server
Common files used by all instances on a single computer are installed in the folder below the folder
C:\Program Files\Microsoft SQL Server

The following table identifies versions for the paths.
Folder name
SQL server Version
140
SQL Server 2017 (14.x)
130
SQL Server 2016 (13.x)
120
SQL Server 2014
110
SQL Server 2012 (11.x)
105
SQL Server 2008 R2(10.50.X)
100
SQL Server 2008 (10.X)
90
SQL server 2005 (9.X)
80
SQL Server 2000 (8.X)


Hope this will be using full to understand the SQL server folder structure.

Popular Posts