Jan 25, 2017

SSIS - How to Load Data from Excel File to SQL Server Table in SSIS Package

Welcome to codefari video tutorial, in this tutorial we will learn how to import data from excel file to SQL Server table in SSIS package. In this tutorial we will learn how to extract data from excel file, transform according to us and load in SQL Server table.








Jan 24, 2017

SQL Server- How to get last access/update time for tables in Database?

One day I got requirement to find last updates of tables in database. Means I want to get all the tables’ last update in database and did surfing on google and found the best way. Link: https://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/

I am taking reference from above link and try to explain. See following query it may help you to find exact result.


SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( '<your_db_name>')


If you want to see the last update of particular table then use the following query.


SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( '<your_db_name>')
AND OBJECT_ID=OBJECT_ID('table_name')




Jan 12, 2017

SQL Server - How can we truncate all tables in Database?

sp_msforeachtable can be use to truncate tables from database. sp_msforeachtable is undocumented means this is not documented by Microsoft it can be change or modified at any time. It contains two parameters @command1 and @whereand using this procedure we can truncate tables.

If you want truncate all tables in database then use following query.


Exec Sp_msforeachtable @command1='Truncate Table ?'


If you want to truncate all tables of particular schema then use following query


Exec Sp_msforeachtable @command1='Truncate Table ?',@whereand='and Schema_Id=Schema_id(''schema_name'')'




SQL Server - How to find all empty/have values tables

Many times our managers ask to find empty tables and have value tables report. It is time taken to find the tables manually. I am writing some query which may help you to find empty and have values table.
Following query help you to find all empty tables in database.

1 – See following query


SELECT IST.TABLE_NAME , MAX(SI.rows ) TOTAL_RECORDS
FROM sysindexes SIINFORMATION_SCHEMA.TABLES IST
WHERE IST.TABLE_NAME = OBJECT_NAME(SI.id)
      AND IST.TABLE_TYPE = 'BASE TABLE'
GROUP BY IST.TABLE_SCHEMAIST.TABLE_NAME
HAVING MAX(SI.rows)<=0

  
2- If you want to find all empty tables of particular “schema” then use following query.


SELECT IST.TABLE_NAME , MAX(SI.rows ) TOTAL_RECORDS
FROM sysindexes SIINFORMATION_SCHEMA.TABLES IST
WHERE IST.TABLE_NAME = OBJECT_NAME(SI.id)
      AND IST.TABLE_TYPE = 'BASE TABLE'
      AND IST.TABLE_SCHEMA='Schema_Name'
GROUP BY IST.TABLE_SCHEMAIST.TABLE_NAME
HAVING MAX(SI.rows)<=0


If you want to find those all tables that have values then use the following queries.
1 – Query to find all tables that have values.


SELECT IST.TABLE_NAME , MAX(SI.rows ) TOTAL_RECORDS
FROM sysindexes SIINFORMATION_SCHEMA.TABLES IST
WHERE IST.TABLE_NAME = OBJECT_NAME(SI.id)
      AND IST.TABLE_TYPE = 'BASE TABLE'
GROUP BY IST.TABLE_SCHEMAIST.TABLE_NAME
HAVING MAX(SI.rows)>0


2- If you want to find all tables that have values of particular “schema” then use following query.


SELECT IST.TABLE_NAME , MAX(SI.rows ) TOTAL_RECORDS
FROM sysindexes SIINFORMATION_SCHEMA.TABLES IST
WHERE IST.TABLE_NAME = OBJECT_NAME(SI.id)
      AND IST.TABLE_TYPE = 'BASE TABLE'
      AND IST.TABLE_SCHEMA = 'SCHEMA_NAME'
GROUP BY IST.TABLE_SCHEMAIST.TABLE_NAME
HAVING MAX(SI.rows)>0


Another query may also help you to find all Empty tables.


;WITH TBL AS
(
   SELECT
      SUM(row_countAS ROW_COUNT,
      OBJECT_NAME(OBJECT_IDAS TABLE_NAME,
      OBJECT_SCHEMA_NAME(OBJECT_IDAS TABLE_SCHEMA
   FROM
      sys.dm_db_partition_stats
   WHERE
      INDEX_ID = 0 OR INDEX_ID= 1
         
   GROUP BY
      OBJECT_ID
)
SELECT *
FROM TBL
WHERE ROW_COUNT = 0

  
You can filter for schema as below.


;WITH TBL AS
(
   SELECT
      SUM(row_countAS ROW_COUNT,
      OBJECT_NAME(OBJECT_IDAS TABLE_NAME,
      OBJECT_SCHEMA_NAME(OBJECT_IDAS TABLE_SCHEMA
   FROM
      sys.dm_db_partition_stats
   WHERE
      INDEX_ID = 0 OR INDEX_ID= 1
         
   GROUP BY
      OBJECT_ID
)
SELECT *
FROM TBL
WHERE ROW_COUNT = 0
AND TABLE_SCHEMA='Schema_Name'


If you wish to find all tables which have values so please change in above query ROW_COUNT >0


Jan 4, 2017

How to copy variable from one package to another SSIS package

We can easily copy and paste task, transformations, connection manager from one package to another but like this you cannot copy and paste variables from one to another package. If you have to use same variable for another package it may take time to type variable’s name one by one.


One solution for this I am described below, we can copy and paste variable from one package to another package. Suppose we have a package pck1 and we need to copy all variable from package pck1 and paste to package pck2.



To copy above variable we have to open xml code of pck1. How can we open the package in code mode please see the following image.

Copy the variable code shown as below.




Same as open package pck2 in code mode and replace variables attribute with copied code as below.


After replace code and save, package will look like as below.


Nov 29, 2016

GOTO Statement in SQL Server

After a condition/statement execution, if we need to alter the flow of execution to start from a particular label, then GOTO statement will help you. Basically, GOTO keyword uses to skip statement process and continue from a particular label.
GOTO Statement in SQL Server contains two parts –

     GOTO statement declaration: GOTO statement contains GOTO keywords and label_name as below.


GOTO label_name


    Label Declaration: Label contains label_name only with at least one statement as below.


label_name:
select * from tbl


Label_name must be unique within the scope of the code and after label_name declaration, there should be one SQL-statement.

Example: How can we use GOTO statement in SQL server, you can see below.


DECLARE @rating INT=10
WHILE @rating>0
BEGIN
            IF @rating=8
                        GOTO Codefari
            SET @rating=@rating-1
            PRINT @rating
END

Codefari:
PRINT 'www.codefari.com'



In above example, we have created one GOTO statement lable_name is Codefari if the @rating value is 8 then code will branch to the label called Codefari.

Nov 25, 2016

Transaction Logs in SQL server. Why we need and how can we read transaction log in SQL Server?

Problem: As we know SQL server Master DB store all the information of table, schema, stored procedures, function, triggers etc. But what is about the operational information, if any problem like error or bug happened in our process execution or some intruder want to do nasty on your server like SQL injection. How can we track this type of issue in Our SQL Server?

Solution: Transaction log may help you to track this problem, Transition log may be the evidence for such type of problems.

Basic Operation captured by Transaction: Before explanation about transaction log, we should to know what SQL operations are captured by transaction log.

Transaction log captured all the DML operation like INSERT, UPDATE, DELETE and also some DDL operation like CREATE, TRUNCATE and DROP,

Why to read SQL Server transaction log?

Transaction log contains all the information about the transaction we performed in our database. If we did some operation by mistake like delete or we lost some data and we want to recover lost data then transaction log may help you to resolve this problem. 

Way of reading SQL Server transaction log.

We can’t read directly to transaction log because of security reason. SQL Server provides us some function to decode it. The function are fn_dblog() and fn_dump_dblog(). For the security purpose transaction log stores the information in encrypted (not human readable) format so we could not read it directly.

View SQL Server transaction log using fn_dblog() funation:



The fn_dblog() is an undocumented function of SQL Server, which is allow to view the transaction log records in the active part of the transaction log file for the current database.

Fn_dblog() function takes two parameters, start LSN and end LSN.

Start LSN: Starting log sequence number, or LSN, you can also specify null which means it will return everything from the start of log.

End LSN: Ending log sequence number, or LSN, you can specify null which means you want to return everything to end of the transaction log.

Note: Undocumented means, if you are using this function in SQL Server, which is against of production database instances executed on your own risk. I will explain only fn_dblog() function only in this article.

How to use fn_dblog() function in SQL Server?

If I will explain uses of fn_dblog() on existing database it may create confusion so I am using new database from creation step by step.

1-Step:  Create database ‘Codefari’.


USE[master]
GO

CREATE DATABASE Codefari
GO


2-Step: Create table ‘Blogs’.


USE [Codefari]
GO
CREATE TABLE Blogs
(
            ID INT IDENTITY(1,1) NOT NULL,
            TITLE VARCHAR(MAX)
)


3-Step: In this step we will check how much process has been used by SQL Server to create database and table.




USE [Codefari]
GO
SELECT COUNT(*) FROM fn_dblog(null,null)









You can see 180 steps taken by SQL Server to complete this process.

4-Step: In this step you can see what data available in log.


USE [Codefari]
GO

SELECT
[Current LSN],
[Operation],
[Transaction Name], 
[Context],
[Transaction ID],
[AllocUnitName],
[Page ID],
[SPID],
[Parent Transaction ID],
[Description]
FROM fn_dblog(NULL, NULL)















5-Step: In this step we will perform some DML operation and will track transaction information in Log.


INSERT INTO Codefari.[dbo].[Blogs] VALUES('Transaction log in Codefari')
GO
UPDATE Codefari.[dbo].[Blogs] SET [TITLE]='Transaction log in Codefari.com' WHERE ID=1
GO
DELETE FROM Codefari.[dbo].[Blogs] WHERE ID=1
GO


6-Step: This step will show the transaction which we have performed in 5-step.


USE [Codefari]
GO

SELECT
[Current LSN],
[Transaction ID],
[Operation],
[Transaction Name],
[Context],
[AllocUnitName],
[Page ID],
[Slot ID],
[Begin Time],
[End Time],
[Number of Locks],
[Lock Information]
FROM sys.fn_dblog(NULL,NULL)
WHERE Operation IN
('LOP_INSERT_ROWS','LOP_MODIFY_ROW',
'LOP_DELETE_ROWS','LOP_BEGIN_XACT','LOP_COMMIT_XACT')


















We can see in above steps how fn_dblog() function shows the captured transaction logs and made the transactions in human readable format.

Above experiment and points give consequences as, fn_dblog() can be used when user want to view SQL transaction log. User can easily check the transaction log to see the activities like page splits or objects being drops etc.