Developers need to call the uspErrorHandling stored procedure in their code with the optional parameter list.
ALTER PROCEDURE [dbo].[uspErrorLogTest] (@date as datetime)
AS
--sample exec [uspErrorLogTest] '11/02/2018'
BEGIN TRY
declare @paramList varchar(4000) = '@date=' + cast( @date as varchar)
-- SELECT 1/0
select cast(@date as int) /0
END TRY
BEGIN CATCH
begin
EXEC dbo.uspErrorHandling @paramList
end
END CATCH
if object_id('uspErrorHandling','P') is not null
drop procedure uspErrorHandling
GO
CREATE procedure dbo.uspErrorHandling (@ErrorProcParameterList varchar(4000) = null)
AS
-- Declaration statements
DECLARE @ErrorNumber int
DECLARE @ErrorMessage varchar(4000)
DECLARE @ErrorSeverity int
DECLARE @ErrorState int
DECLARE @ErrorProcedure varchar(200)
DECLARE @ErrorLine int
DECLARE @UserName varchar(200)
DECLARE @HostName varchar(200)
DECLARE @TransactionDateTime datetime
-- Initialize variables
SELECT @ErrorNumber = isnull(error_number(),0),
@ErrorMessage = isnull(error_message(),'NULL Message'),
@ErrorSeverity = isnull(error_severity(),0),
@ErrorState = isnull(error_state(),1),
@ErrorLine = isnull(error_line(), 0),
@ErrorProcedure = isnull(error_procedure(),''),
@UserName = SUSER_SNAME(),
@HostName = HOST_NAME(),
@TransactionDateTime = GETDATE();
select @ErrorMessage
-- Insert into the dbo.ErrorLog table
INSERT INTO dbo.ErrorLog (ErrorNumber, ErrorMessage, ErrorSeverity, ErrorState, ErrorLine,
ErrorProcedure, ErrorParameterList,UserName, HostName,TransactionDateTime)
SELECT @ErrorNumber, @ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorLine,
@ErrorProcedure, @ErrorProcParameterList,@UserName, @HostName, @TransactionDateTime
GO
--Table where we log the errors
CREATE TABLE [dbo].[ErrorHandling](
[ErrorHandlingID] [int] IDENTITY(1,1) NOT NULL,
[Error_Number] [int] NOT NULL,
[Error_Message] [varchar](4000) NULL,
[Error_Severity] [smallint] NOT NULL,
[Error_State] [smallint] NOT NULL,
[Error_Procedure] [varchar](200) NOT NULL,
[Error_Line] [int] NOT NULL,
[UserName] [varchar](128) NULL,
[HostName] [varchar](128) NULL,
[TransactionDateTime] [datetime] NOT NULL,
[CreatedDateTime] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[ErrorHandlingID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
This blog is created to add all the information and experiences that I have learnt while working on MS sql server as a DBA. The blog is also updated frequently with the real world problem that I encounter hands on at work and the resolutions to resolve them. I hope this blog will be of some use to you and you will revisit. Thank you for stopping by and you are welcome to leave comments.
Wednesday, May 2, 2018
Monday, April 2, 2018
How to read mixed data type from excel file in SSIS
To fix the issue where SSIS was reading mixed data type wrong from an EXCEL file I had to change the registry information
I had a file where the source file had account numbers where some rows had value as integer and some had alpha numeric character. When the account number had first few rows as integer the remaining rows with the alphanumeric account number value was not getting read and the value was getting imported as NULL. I followed the following steps to fix this issue in EXCEL 2012 .
I had a file where the source file had account numbers where some rows had value as integer and some had alpha numeric character. When the account number had first few rows as integer the remaining rows with the alphanumeric account number value was not getting read and the value was getting imported as NULL. I followed the following steps to fix this issue in EXCEL 2012 .
- In Windows environments, select Start ► Run and type REGEDIT to display the Registry Editor.
- In the registry tree, select HKEY_LOCAL_MACHINE ► Software ► Wow6432Node ► Microsoft ► Office ► 14.0 ► Access Connectivity Engine ► Engines.
- Double-click the Excel node.
- In the right panel, double-click the TypeGuessRows entry.
- Change the value data from 8 to 0.
- Click OK.
- Select File ► Exit to exit the Registry Editor window.
Thursday, February 15, 2018
How to add the source file row number to the destination table.
https://www.hansmichiels.com/2015/11/13/how-to-add-a-rownumber-to-a-data-flow-task-in-ssis-2012/#comment-205
The above link has all the explanation. It was very helpful.
Friday, December 1, 2017
Generate XML data
--XML data sample to be genereated
<?xml version="1.0"?>
-<serialization SerializationRoot="SBOInboundBatchCO">
-<SBOInboundBatchCO ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchCO">
-<sboInboundBatchIO ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchIO">
<creationDate Type="DATE">2017/05/02</creationDate>
<externalReferenceDate Type="DATE">2017/05/02</externalReferenceDate>
-<sboInboundMessageIOs ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO" IsIndexed="1">
-<element ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO">
<billingPeriodsequenceNumber>2</billingPeriodsequenceNumber>
<messageCodeBehaviourCode Type="STRING">CACISBOACKGMENT</messageCodeBehaviourCode>
<messageText Type="STRING"/>
<receivableGrpId Type="STRING">615466</receivableGrpId>
<responsiblePartyIdentifier Type="STRING">Y</responsiblePartyIdentifier>
</element>
</sboInboundMessageIOs>
<sboName Type="STRING">CACi BD Collections</sboName>
<totalMessageCount Type="NUMERIC">284</totalMessageCount>
</sboInboundBatchIO>
</SBOInboundBatchCO>
</serialization>
Used the following SQL script to get this output
declare @intTotMsgCount int , @xmlstr xml
set @inttotMsgCount = (Select count(Account_number) from Imp_staging_referrals_claims_FollowUp where imp_fileId = @intfileId
and uploaded = 1 and deleted = 0)
set @xmlstr=
(select 'SBOInboundBatchCO' as '@SerializationROOT',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchCO' as '@ClassName',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchIO' as '@ClassName',
'DATE' as 'creationDate/@Type',
convert(varchar(10),cast(getdate() as date) ,111) as 'creationDate',
'DATE' as 'externalReferenceDate/@Type',
convert(varchar(10),cast(getdate() as date) ,111) as 'externalReferenceDate',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO' as '@ClassName', '1' as '@IsIndexed',
(
select ' com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO' as '@ClassName',
'1' as 'billingPeriodsequenceNumber',
'STRING' as 'messageCodeBehaviourCode/@Type',
'CACISBOACKGMENT' as 'messageCodeBehaviourCode',
'STRING' as 'messageText/@Type',
'' as 'messageText',
'STRING' as 'receivableGrpId/@Type',
substring(Account_Number,2,len(Account_Number)-4) as 'receivableGrpId',
'STRING' as 'responsiblePartyIdentifier/@Type',
'Y' as 'responsiblePartyIdentifier'
from Imp_staging_referrals_claims_FollowUp
where imp_fileId = @intFileId
and Facility_Code = @intClientCode
for xml path ('element') , elements,type
)
for xml path ('sboInboundMessageIOs') , type
)
,'STRING' as 'sboName/@Type',
'CACi BD Collections' as 'sboName',
'NUMERIC' AS 'totalMessageCount/@type',
@intTotMsgCount as 'totalMessageCount'
for xml path('sboInboundBatchIO'), elements,type
)
for xml path('SBOInboundBatchCO'), type
)
for xml path ('serialization') , type
)
--AS COL_XML
select '<?xml version="1.0"?>' + cast(@xmlstr as varchar(max)) as COL_XML
<?xml version="1.0"?>
-<serialization SerializationRoot="SBOInboundBatchCO">
-<SBOInboundBatchCO ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchCO">
-<sboInboundBatchIO ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchIO">
<creationDate Type="DATE">2017/05/02</creationDate>
<externalReferenceDate Type="DATE">2017/05/02</externalReferenceDate>
-<sboInboundMessageIOs ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO" IsIndexed="1">
-<element ClassName="com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO">
<billingPeriodsequenceNumber>2</billingPeriodsequenceNumber>
<messageCodeBehaviourCode Type="STRING">CACISBOACKGMENT</messageCodeBehaviourCode>
<messageText Type="STRING"/>
<receivableGrpId Type="STRING">615466</receivableGrpId>
<responsiblePartyIdentifier Type="STRING">Y</responsiblePartyIdentifier>
</element>
</sboInboundMessageIOs>
<sboName Type="STRING">CACi BD Collections</sboName>
<totalMessageCount Type="NUMERIC">284</totalMessageCount>
</sboInboundBatchIO>
</SBOInboundBatchCO>
</serialization>
Used the following SQL script to get this output
declare @intTotMsgCount int , @xmlstr xml
set @inttotMsgCount = (Select count(Account_number) from Imp_staging_referrals_claims_FollowUp where imp_fileId = @intfileId
and uploaded = 1 and deleted = 0)
set @xmlstr=
(select 'SBOInboundBatchCO' as '@SerializationROOT',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchCO' as '@ClassName',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundBatchIO' as '@ClassName',
'DATE' as 'creationDate/@Type',
convert(varchar(10),cast(getdate() as date) ,111) as 'creationDate',
'DATE' as 'externalReferenceDate/@Type',
convert(varchar(10),cast(getdate() as date) ,111) as 'externalReferenceDate',
(
select 'com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO' as '@ClassName', '1' as '@IsIndexed',
(
select ' com.siemens.med.hs.sf.thirdpartycollection.inbound.SBOInboundMessageIO' as '@ClassName',
'1' as 'billingPeriodsequenceNumber',
'STRING' as 'messageCodeBehaviourCode/@Type',
'CACISBOACKGMENT' as 'messageCodeBehaviourCode',
'STRING' as 'messageText/@Type',
'' as 'messageText',
'STRING' as 'receivableGrpId/@Type',
substring(Account_Number,2,len(Account_Number)-4) as 'receivableGrpId',
'STRING' as 'responsiblePartyIdentifier/@Type',
'Y' as 'responsiblePartyIdentifier'
from Imp_staging_referrals_claims_FollowUp
where imp_fileId = @intFileId
and Facility_Code = @intClientCode
for xml path ('element') , elements,type
)
for xml path ('sboInboundMessageIOs') , type
)
,'STRING' as 'sboName/@Type',
'CACi BD Collections' as 'sboName',
'NUMERIC' AS 'totalMessageCount/@type',
@intTotMsgCount as 'totalMessageCount'
for xml path('sboInboundBatchIO'), elements,type
)
for xml path('SBOInboundBatchCO'), type
)
for xml path ('serialization') , type
)
--AS COL_XML
select '<?xml version="1.0"?>' + cast(@xmlstr as varchar(max)) as COL_XML
How to export data from database tables to an XML file using SSIS
I had a project where I had to generate a xml file with data from database tables using SSIS.
The components involved were
Data Flow Task
OLE DB Source - which had the SQL command at the stored proc that called the XML data
Flat File Destination that used a Flat File Connection Manager.
The important thing to remember here was the FFCM set up .
Here are the screen shots to show I set it up .
The DFT consisted of OLEDB Sour Editor calling a procedure that generated XML data.
Below is the Flat File Connection Manager used. The format was DELIMITED.
The final important part was the below where we set up the data type which had to be Unicode text stream
The components involved were
Data Flow Task
OLE DB Source - which had the SQL command at the stored proc that called the XML data
Flat File Destination that used a Flat File Connection Manager.
The important thing to remember here was the FFCM set up .
Here are the screen shots to show I set it up .
The DFT consisted of OLEDB Sour Editor calling a procedure that generated XML data.
Below is the Flat File Connection Manager used. The format was DELIMITED.
In the Columns section a new column COL_XML is added which is the ONLY column returned by the stored procedure that has the XML data.
The final important part was the below where we set up the data type which had to be Unicode text stream
Subscribe to:
Posts (Atom)