Saturday, November 17, 2018

How to pass table result as a parameter in stored procedure

---> Create user defined datatype,table type object

CREATE TYPE StateMaster AS TABLE
(
 StateCode VARCHAR(2),
 StateDescp VARCHAR(250)
)
GO

create table StateMst(
StateCode VARCHAR(2),
 StateDescp VARCHAR(250),
 todaydate datetime)
--> Use user defined type object as parameter in stored procedure

CREATE PROCEDURE TableValuedParameterExample_SP
(
@TmpTable StateMaster READONLY
)
AS
BEGIN
 INSERT INTO StateMst
  SELECT StateCode,StateDescp,getdate() FROM @TmpTable
END
GO



BEGIN
/* DECLARE VARIABLE OF TABLE USER DEFINED TYPE */
DECLARE @MyTable StateMaster

/* INSERT DATA INTO TABLE TYPE */
INSERT INTO @MyTable VALUES('11','AndhraPradesh')
INSERT INTO @MyTable VALUES('12','Assam')

/* EXECUTE STORED PROCEDURE */
EXEC TableValuedParameterExample_SP @MyTable
GO

--select * from StateMst

Friday, August 17, 2018

Difference Between the Delay validation and validate External meta data Properties in SSIS

DelayValidation --> validation of executable is delayed until runtime. For example, if you read from a table that has not yet been created, the package will crash when you start it, unless you set this property to true. The package waits with the validation of that executable until it has to run.

ValidateExternalMetadata --> validates a component during design time using external metadata. For example, if you are reading from an Excel file, the package will show an error during design time if the excel file does not exist. If you set this to False, the error should go away.

It is possible to have a package with an error because the excel file does not exist during design time, but if you run the package it will succeed when DelayValidation is set to True.

So, basically:
DelayValidation = runtime
ValidateExternalMetadat = design time 

Friday, May 4, 2018

we create same Local temp table in different sessions in SQL Sever ? If we can create how it will be saved in TempDB?

I have tried to create a Local Temp Table ( with the name of #temp) in 2 session in sql sever.
Tables has been created successfully in both the sessions with the same Local Temp Table(#temp) name.


After successfully created temp tables ,I have checked the TempDB to know the how tables will be saved. Please check below screen shot for your reference.




Tables has saved with unique extension number based on session.


Conclusion : We can created the same temp table in different sessions.
Please correct me if I am wrong.

Wednesday, April 5, 2017

How to Tracking the Execution of the Package

SELECT
(select convert(BIGINT, format(getdate(), 'yyyyMMddHHmmssf'))) as SessionID
,sysdatetime() as CurrentDate
,null ProcessStepSuccessMessage
,null ProcessStepErrorMessage
,null ObjectName
-----------------------------------

SELECT
'Load Gems Entity data into Staging' as ProcessStepName
,'Loading Gems Entity data into Staging failed' as ProcessStepErrorMessage
,'Loading Gems Entity data into Staging completed successfully' as ProcessStepSuccessMessage

-----------------------------------
EXECUTE  TaxStage.[RECON].[prProcessExecution]  ?,?,?,?,0,0,0,0,'In-Progress',null,null,null,? OUTPUT
-------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [RECON].[prProcessExecution]
@SessionID bigint,
@ProcessStepName nvarchar(255),
@EffectiveStartDate datetime,
@EffectiveEndDate datetime,
@ExpectedRecordCount bigint,
@ActualRecordCount bigint,
@ExpectedAmountTotal numeric(25,7),
@ActualAmountTotal numeric(25,7),
@ProcessStepStatus nvarchar(255),
@ProcessLogMessage nvarchar(2048),
@ProcessErrorMessage nvarchar(2048),
@ObjectName nvarchar(512),
@ProcessExecutionID bigint OUT
--@RecordsProcessed int OUT
AS
declare @ProcTS datetime, @ProcessStepID bigint, @ProcessID bigint
declare @ProcessExecutionCount int, @ProcessStepCount int, @SessionCount int
SELECT @ProcTS = getdate(), @ProcessExecutionCount = 0, @ProcessStepCount = -1
-- Get Process Step ID
SELECT @ProcessStepID = max(ProcessStepID) from TaxStage.RECON.ProcessStep
where ProcessStepName = @ProcessStepName
-- Get Process ID
SELECT @ProcessID = max(ProcessID) from TaxStage.RECON.ProcessStep
where ProcessStepID = @ProcessStepID
select @SessionCount = count(*)  from TaxStage.RECON.[Session] where SessionID = @SessionID
-- IF new session, then insert into Session table
IF @SessionCount = 0
BEGIN
INSERT INTO TaxStage.RECON.[Session]
SELECT top 1 @SessionID, @ProcessID, @ProcTS, NULL, 'In-Progress', @ProcTS, user, @ProcTS from TaxStage.RECON.[Session]
END
-- MERGE into Process Execution Table
MERGE TaxStage.RECON.ProcessExecution as pe
USING (
select top 1
@SessionID AS SessionID
,@ProcessStepID AS      ProcessStepID
,@ProcTS AS      StartTime
,@ProcTS AS      EndTime
,@EffectiveStartDate AS      EffectiveStartDate
,@EffectiveEndDate AS      EffectiveEndDate
,@ExpectedRecordCount AS      ExpectedRecordCount
,@ActualRecordCount AS      ActualRecordCount
,@ExpectedAmountTotal AS      ExpectedAmountTotal
,@ActualAmountTotal AS      ActualAmountTotal
,@ProcessStepStatus AS      ProcessStepStatus
,@ProcessLogMessage AS      ProcessLogMessage
,@ProcessErrorMessage AS      ProcessErrorMessage
,@ObjectName AS ObjectName
  from RECON.ProcessExecution
  where 1 = 1
) as stg
ON pe.SessionID = stg.SessionID
AND pe.ProcessStepID = stg.ProcessStepID
WHEN MATCHED THEN UPDATE SET
pe.[EndTime] = stg.[EndTime],
pe.[ActualRecordCount] = stg.[ActualRecordCount],
pe.[ActualAmountTotal] = stg.[ActualAmountTotal],
pe.ProcessStepStatus = stg.ProcessStepStatus,
pe.[ProcessLogMessage] = stg.[ProcessLogMessage],
pe.[ProcessErrorMessage] = stg.[ProcessErrorMessage],
pe.ObjectName = stg.ObjectName,
pe.LastUpdateTs = @ProcTS,
pe.EffectiveStartDate = stg.EffectiveStartDate,
pe.EffectiveEndDate = stg.EffectiveEndDate
WHEN NOT MATCHED THEN
INSERT(
[SessionID]
      ,[ProcessStepID]
      ,[StartTime]
      ,[EndTime]
      ,[EffectiveStartDate]
      ,[EffectiveEndDate]
      ,[ExpectedRecordCount]
      ,[ActualRecordCount]
      ,[ExpectedAmountTotal]
      ,[ActualAmountTotal]
      ,ProcessStepStatus
      ,[ProcessLogMessage]
      ,[ProcessErrorMessage]
 ,ObjectName
      ,[CreateTs]
      ,[LastUpdateUserID]
      ,[LastUpdateTs]
)
VALUES(
@SessionID
,@ProcessStepID
,@ProcTS
,@ProcTS
,@EffectiveStartDate
,@EffectiveEndDate
,@ExpectedRecordCount
,@ActualRecordCount
,@ExpectedAmountTotal
,@ActualAmountTotal
,@ProcessStepStatus
,@ProcessLogMessage
,@ProcessErrorMessage
,@ObjectName
,@ProcTS
,user
,@ProcTS
);
-- Derive Process Status for Session Table
SELECT @ProcessStepCount = COUNT(*) FROM RECON.ProcessStep where ActiveFlg = 'Y' and ProcessID = @ProcessID
SELECT @ProcessExecutionCount = COUNT(*) FROM RECON.ProcessExecution where SessionID = @SessionID
and ProcessStepStatus = 'Success'
IF @ProcessStepCount = @ProcessExecutionCount OR @ProcessStepStatus = 'Fail'
BEGIN
UPDATE TaxStage.RECON.[Session]
SET ProcessStatus = @ProcessStepStatus,  [LastUpdateUserID] = user ,[LastUpdateTs] = @ProcTS, EndTime = @ProcTS
WHERE SessionID = @SessionID
END
ELSE
BEGIN
UPDATE TaxStage.RECON.[Session]
SET ProcessStatus = 'In-Progress',  [LastUpdateUserID] = user ,[LastUpdateTs] = @ProcTS
WHERE SessionID = @SessionID
END
-- Return Process Execution ID
SELECT @ProcessExecutionID = max(ProcessExecutionID) from TaxStage.RECON.ProcessExecution
where ProcessStepID = @ProcessStepID
and   SessionID = @SessionID

-----------------------------------------------------

EXECUTE  TaxStage.[RECON].[prProcessExecution]  ?,?,?,?,0,?,0,0,'Success',?,null,null,? OUTPUT

Wednesday, October 5, 2016

How to read data from multiple Excel worksheets with SQL Server Integration Services

https://www.mssqltips.com/sqlservertip/4157/how-to-read-data-from-multiple-excel-worksheets-with-sql-server-integration-services/

Thursday, September 22, 2016

Friday, September 2, 2016

INTRODUCTION TO SSIS

Sql Server Integration Services (SSIS) is ETL (Extract, Transform, Load) Tool, developed by Microsoft to perform ETL operations; i.e. Extract, Transform, and consolidate data from different sources and load it to a single or multiple destinations. It provides a platform to build data integration and workflow applications. SSIS packages can be created using BIDS (Business Intelligence Development Studio).
Business Intelligence Development Studio is the environment that we will use to develop packages for data extraction, transformation, and loading (ETL) in Integration Services. Business Intelligence Development Studio is the Visual Studio 2008 environment with enhancements that are specific to business intelligence solutions. Business Intelligence Development Studio (BIDS) will be available as client tool when installing SQL Server Management Studio (SSMS).
Usage of SSIS:
    Populate data warehouses.
  • Manage, Clean and Standardize data.
  • Merge data from multiple data sources, either from relational data bases or from flat files etc.
  • Automating database administrative tasks.