DSharp.fi
  • Home
  • Solutions
    • DSharp PathFinder
    • DSharp Studio
    • DSharp Studio Modeler
    • Pricing and Licenses
  • Services
    • Professional services
    • DSharp Training Program and Certifications
    • Developer Support
  • Customers
    • Our Customers
    • Customer Success Stories
      • Pirte’s Data Platform Modernization Boosts Data-Driven Healthcare
      • A City Expanded Its Data Warehouse with Library Data
      • Varha – a wellbeing services county – built a data-driven management system
      • From Double Checks to Smooth Automation – DSharp Studio Simplified Early Childhood Education Data Management
      • Productivity Leap improved Metsähallitus’ data management with DSharp Studio
  • Partners
    • Our Partners
    • Partner Stories
    • Become a Partner
  • Resources
  • Articles
    • Data warehousing
      • Data Warehouse concepts and data models
      • DSharp Studio Release: Extended Data Catalog and New Find Command
    • Reporting automation
      • Automate data pipelines
      • Making Data Management Easier with Automation
      • Metsähallitus enhanced its data management with DSharp’s data platform tool
    • Data modeling and mapping
      • Data Warehouse concepts and data models
      • Data vs Business Driven Modeling
  • About
    • About us
    • Contact us
    • Recruiting
  • Book a Demo
  • Start a Trial
  • Menu Menu
  • Features
    • DSharp Studio Features
    • DSharp Studio Modeler Features
    • Command Reference
    • Release Notes
    • Installation Guide
  • Modeling
    • Design Considerations
      • Data vs Business Driven Modeling
      • The Business Key
    • Basics
      • Model Types
      • Archetypes
      • Datatypes
    • Raw Model
      • Source Mappings
      • Metadata
    • Business Model
      • Metadata
  • Course Material
    • DSharp Studio Professional Course
    • DSharp Studio Expert Course
  • Legacy
    • DSharp Engine Features
      • Mapping Source Data in DSharp Engine
    • DSharp Engine Command Reference
    • DSharp Engine Installation Guide
    • 3rd Party Modeling Tools
      • Visual Paradigm
        • Modeling With Visual Paradigm
        • Configuring Data Vault 2.0 And Servers
        • Implementing Mappings
        • Setting Up
          • Import Stereotypes
          • Configure UI
      • Ellie
        • Modeling With Ellie
        • Ellie Metadata
    • Course Material
      • Intro Course
      • Advanced Course
  • Community
  • FAQ

What We Learn

How to change load behaviour by changing script code.

Scenario

We are not happy about the fact that loading transactional data fails if the incoming data contains already loaded data, and we want to change that. Since this is a feature that is not parameterized (optimally, it should be), it is defined directly in the source code.

Stored procedures that perform the loading of the tables are based on templates. The template is read by a C# script that “fills in the blanks” in the template file. Changing default behaviour can be achieved by either editing the template file, the script file, or both.

Using Visual Studio Code, open the Workspace/Scripting folder in the D♯ Engine installation directory. Using Visual Studio Code’s Search function, search for the word WHEREIFNOTTRANSACTION to locate the files sqlserver pbody link.sql and CreateLoadLinkProcedureBody_Generic.cs. Open the two files in the Visual Studio Code editor.

The link tempate file sqlserver pbody link.sql looks like this:

alter procedure [%ETLSCHEMA%].[%LOADPROCEDURE%](@pLoadTime as datetime = null) as
%PROCHEADER%
begin
    set nocount on
	
	declare @taskGUID as uniqueidentifier = newid()
	declare @success as int = 0
	declare @fail as int = 0
	declare @msg as nvarchar(max)

	if (@pLoadTime is null)
		set @pLoadTime = getDate()

	exec [%ETLDATABASE%].[%ETLSCHEMA%].ETLTryTruncate @pLoadTime, '[%TARGETDATABASE%].[%TARGETSCHEMA%].[%TARGETOBJECT%]'
	
	exec [%LOGDATABASE%].[%LOGSCHEMA%].StartTask
		@taskGUID,
		@pLoadTime,
		'Link load', 
		'[%ETLDATABASE%].[%ETLSCHEMA%].[%WORKINGTABLE%]', 
		'[%TARGETDATABASE%].[%TARGETSCHEMA%].[%TARGETOBJECT%]',
		'[%ETLSCHEMA%].[%LOADPROCEDURE%]', 
		null, 
		'Loading %TARGETOBJECT% from %WORKINGTABLE%'

	begin try
		begin transaction

			insert into [%TARGETDATABASE%].[%TARGETSCHEMA%].[%TARGETOBJECT%]
			(
%TARGETFIELDS%
			)
			select %DISTINCT%
%SOURCEFIELDS%
			from
				[%ETLDATABASE%].[%ETLSCHEMA%].[%WORKINGTABLE%] as inData
%WHEREIFNOTTRANSACTION%

			set @success = @@ROWCOUNT

		commit
		set @msg = '%TARGETOBJECT% loaded from %WORKINGTABLE%'
	
	end try
	begin catch
		set @fail = 1
		rollback transaction
	
		set @msg = ERROR_MESSAGE()
	
	end catch

	exec [%LOGDATABASE%].[%LOGSCHEMA%].EndTask
	  @taskGUID,
	  @success,
	  @fail,
	  @msg
	
end

Notice the placeholder %WHEREIFNOTTRANSACTION%: it will be replaced by a where-clause, unless the link is a non-historized link. A section of the script file CreateLoadLinkProcedureBody_SQLServer.cs looks like this:

class CreateLoadLinkProcedureBody_SQLServer
{
    ...

    private String whereIfNotTransaction = 
	"where not exists (select 1 from [%TARGETDATABASE%].[%TARGETSCHEMA%].[%TARGETOBJECT%] where [%HASHNAME%] = inData.[%HASHNAME%])";
	
    ...

private String ReplacePlaceholders(String pBody, StoredProcedure pProcedure)
{  
        Tuple<string, string=""> columns = BuildLists();
      
        DataObject aTbl = _ProcFacade.GetTargetTable();
        Class aClass = new BITableFacade(aTbl).ImplementedObject as Class;
        String aWhere = whereIfNotTransaction;
        String aCheckTransaction = "";     
        
        if ((new BIClassFacade(aClass).IsTransaction))
        {
                aWhere = "";
                if (!App.SQLSyntax.EnforcesReferentialIntegrity)
                    aCheckTransaction = @"declare @existingRowCount as int
            select @existingRowCount = count(%HASHNAME%)
                from %ETLSCHEMA%.%WORKINGTABLE% as inData
                where exists (SELECT %HASHNAME% from %TARGETSCHEMA%.%TARGETOBJECT%)
           
            if @existingRowCount > 0
                RAISERROR ('ERROR: %ETLSCHEMA%.%WORKINGTABLE% contains duplicate hashes with %TARGETSCHEMA%.%TARGETOBJECT%, transaction data not loaded',11,1);
            ";
            }
       
        pBody = pBody.Replace("%WHEREIFNOTTRANSACTION%", aWhere);

    ...

</string,>

The red code lines are the ones handling this placeholder. If you want the procedure to “let in” rows that have not been loaded previously, and skip those that have, just comment out the following line like this:

        if ((new BIClassFacade(aClass).IsTransaction))
        //     aWhere = "";

In future deployments, all link loads will have the exists-check implemented in their load procedures.

So: make this change, save the .cs file, and restart D# Engine (script files will be read and compiled each time D# Engine is started). Then do this:

  • Open the current load procedure etl.Load_lnk_AccountingEntry_from_work_FinMaster_RAW_ACCOUNTINGENTRY in an editor.
  • Deploy the Accounting entry class.
  • Open the newly installed version of etl.Load_lnk_AccountingEntry_from_work_FinMaster_RAW_ACCOUNTINGENTRY.
  • Compare the two procedure versions.

Last, but not least, change these back:

        if ((new BIClassFacade(aClass).IsTransaction))
            aWhere = "";

Run Tutorial Scripts

Run the following tutorial script commands fron the Help -> Tutorials -> Intro Course -> Changing Load Behaviour menu, and inspect the results.

Script Source data Main points of interest
Step 1: Reload corrected data An error has been fixed on one row, and a new row has been added. The link table load succeeds technically.
The new row is loaded, but the corrected one isn’t.

Next tutorial

  • Developer Content
    • No Access
    • Model Types
    • Archetypes
    • Datatypes
    • Source Mappings
    • Metadata
    • Metadata
    • Data vs Business Driven Modeling
    • The Business Key
    • DSharp Studio Professional Course
      • Tutorial 01: Person Tutorial
      • Tutorial 02: Add Attributes
      • Tutorial 03: Project Tutorial
      • Tutorial 04: Hash Error Handling
      • Tutorial 05: Simple Hierarchy
      • Tutorial 06: Transactions
      • Tutorial 07: Filtering Data
      • Tutorial 08: Loading Multiple Classes From One Source
      • Tutorial 09: Handling Legitimate Duplicates
      • Tutorial 10: Multiple Attribute Values
    • Intro Course
      • Intro Course – Before You Begin
      • Tutorial 01 – Visual Paradigm
      • Tutorial 01 – Ellie
      • Tutorial 01: Person Tutorial
      • Tutorial 02: Add Attributes
      • Tutorial 03: Project Tutorial
      • Tutorial 04: Hash Error Handling
      • Tutorial 05: Simple Hierarchy
      • Tutorial 06: Transactions
      • Tutorial 07: Changing Load Behaviour
      • Tutorial 08: Loading Multiple Classes From One Source
      • Tutorial 09: Handling Legitimate Duplicates
      • Tutorial 10: Multiple Attribute Values
    • Advanced BDP Developer Course
      • Tutorial 01: Key Groups
      • Tutorial 02: Implementing the Participation Design Pattern
      • Tutorial 03: Versioned Source Data
      • Tutorial 04: Advanced State Handling
      • Tutorial 05: Hierarchy Alternatives
      • Tutorial 06: Hierarchy Depth Changes
      • Tutorial 07: Partial Load Deletion Detection
      • Tutorial 08: Merge Hashes Non-Destructively
      • Tutorial 09: Implementing Business Objects
      • Tutorial 10: Custom Value Transformations
    • DSharp Studio Expert Course
      • Tutorial 01: Key Groups
      • Tutorial 02: Implementing the Participation Design Pattern
      • Tutorial 03: Versioned Source Data
      • Tutorial 04: Advanced State Handling
      • Tutorial 05: Hierarchy Alternatives
      • Tutorial 06: Hierarchy Depth Changes
      • Tutorial 07: Partial Load Deletion Detection
      • Tutorial 08: Merge Hashes Non-Destructively
      • Tutorial 09: Derived Classes
    • Mapping Source Data
    • Command Reference
    • Release Notes And Downloads
    • DSharp Studio Command Reference
    • DSharp Studio Command Reference
    • DSharp Studio Features
    • DSharp Studio Modeler Features
    • DSharp Studio Modeler Installation Guide
    • Installation Guide
    • Configuring Data Vault 2.0 And Servers
    • Modeling With Visual Paradigm
    • Implementing Mappings
    • Import Stereotypes
    • Configure Visual Paradigm UI
    • Modeling With Ellie
    • Ellie Metadata
  • Frequently Asked Questions

Finland
Yliopistonkatu 31, 20100, Turku
Bertel Jungin aukio 5, 02600 Espoo

Germany
Podbielskistrasse 333,
5th floor, 30659, Hannover

Solutions

  • DSharp Studio
  • DSharp Studio Modeler
  • Pricing and Licenses

FAQ

  • About us
  • Contact us
  • Recruiting

Legal

  • Privacy policy
  • DSharp Studio License Subscription Agreement

Contact us!

    © Copyright DSharp Oy
    • Link to LinkedIn
    • Link to Mail
    Scroll to top Scroll to top Scroll to top