Creating views in the Studio Source Map

Modified on Fri, 27 Jun at 9:30 PM

This is a detailed topic in our support portal in the Using Hopp series and assumes that you have some prior knowledge or experience using Hopp. 


Views in the Source Map is a powerful feature enabling Hopp to overcome all kinds of challenges when bridging the - sometimes large - structural gap between on one side the Business Object hierarchies required by the Target Interface and on the other side the structure of the legacy source data available to the Source Map.


A View in Hopp is similar to any Sql Server view as it is basically an Sql query. But there is one major difference: When the Source Engine that is generated from the Source Map executes, it will store the result set of the query in a table in the Staging Database. A View in the Source Map is available for use for the Extraction Map in the mapping, alongside the imported metadata and the Valuesets. 


Is is even possible to use Views inside the Sql query of other Views. When it comes to populating the staging tables for the Views in the Staging Database, the Hopp Runtime will automatically do this in the correct sequence. 


When a new View is created in the Source Map and opened in Studio, it looks like this:



1This is where you can enter the Sql query for the view. It is quite common to first write and test the query in Sql Server Management Studio (or any other suitable tool) and - when satisfactory - paste the Sql query into the Studio view 
2This is a list of the fields exposed by the View. These fields must correspond to the columns returned by the query. Studio will validate this in terms of column names, data types and sequence. More on this later
3The Synch... button provides an easy and automatic way to synchronize the columns returned by the view query with the fields exposed by the view
4You can pass the values of Constants defined in the Source Map as parameters to be used in the View query.
5The Objects... button will display a list of the objects (metadata, other views and valuesets) used by the view query
6The Validate button will validate the query and display a popup dialog with any errors encountered
7The query can be opened into a dialog to provide a larger text area to edit the query
8The query can be copied to the clipboard and pasted into Sql Server Management Studio to facilitate testing of the query



Writing the View query

The main task when creating a view is to write and test the Sql query for the view. This is commonly done in Sql Server Management Studio and - when satisfactory - the query is then pasted into the Studio view. 


The view query is just straight forward Sql. But there are a few details to be aware of:

  • The view can reference
    • Entities from imported Metadata
    • Valuesets
    • Other Views
    • Constants provided to the view a Query Parameters

  • If the view is referencing any entities from imported Metadata, these entities must be marked Active

  • The query does not have to be a single Sql statement (like a traditional Sql Server view). The view is in fact a Sql script that will be executed by the Hopp Runtime


You can write any Sql you like, as long as it is only referencing the allowed types of objects and variables. 


Query Parameters

You can pass Constants defined in the Source Map as parameters to the query:



1Add the Constant to the Query Parameters list
2Name the Query Parameter
3Use the Query Parameter in the Query itself by using the name of the parameter with the @ prefix


-->EntryPoint<--

At the end of the day, the Hopp Runtime will execute the View query in connection with an INSERT Sql statement in order to populate the table for the View in the staging database. It is important that the Runtime knows where to place this INSERT statement in the query.


If the query is just a simple SELECT statement, this is very straight forward and you don't need to do anything special. But if the query is more than that, you need to tell there Runtime where to put the INSERT statements with the -->EntryPoint<-- comment.


Here's a couple of samples using the -->EntryPoint<-- comment to clearly mark where to put the INSERT statement.


Query is a programmed Sql script

The query below is in fact a little Sql script that uses some table variables for intermediate results before producing the final result set for the View.  

declare @account table(
  [BankId] decimal(5)
,  [AccountNumber] varchar(10)
,  [ClientId] varchar(10)
)

declare @customer table(
  [BankId] decimal(5)
,  [ClientId] varchar(10)
,  [CustomerNumber] varchar(10)
)

insert @account
select
  [BankId]
,  [AccountNumber]
,  [ClientId]
from
  [Src].[Account]

insert @customer
select
  [BankId]
,  [ClientId]
,  [OfficialId]
from
  [Src].[Client]

-->EntryPoint<--
select
  A.[BankId]
,  A.[AccountNumber]
,  B.[CustomerNumber]
from
  @account as A
  inner join @customer as B
  on  
    A.[BankId] = B.[BankId]
  and A.[ClientId] = B.[ClientId]

Query contains a common table expression (CTE)

The query below contains a CTE:

with account as (
  select
    [BankId]
  ,  [AccountNumber]
  ,  [ClientId]
  from
    [Src].[Account]
)

-->EntryPoint<--
select
  A.[BankId]
,  A.[AccountNumber]
,  B.[OfficialId]
from
  account as A
  inner join [Src].[Client] as B
  on  
    A.[BankId] = B.[BankId]
  and A.[ClientId] = B.[ClientId]


Exposed Fields

Once you have the query sorted out for the View, it is then necessary to add all the columns returned by the query to the list of Exposed Fields. Quite often there a quite a lot of returned columns, so it is lucky that the Synch feature of Studio can help out.


For instance, if this sample Sql query is pasted into a brand new view:

select
  A.[BankId]
,  A.[AccountNumber]
,  A.[AccountType]
,  B.[TargetProductCode]
,  case B.[SourceProductType]
      when '' then null
      else B.[SourceProductType]
   end as [SourceProductType]
from
  Src.[Account] as A
  inner join Vs.[TranslateProductTypes] as B
  on
       A.[BankId] = B.[BankId]
  and  A.[AccountType] = B.[SourceProductType]

Note: The view query above is just meant for illustration. The last column (SourceProductType) is added just to have a nullable column returned by the view.


Synchronizing Exposed Fields

The Synch... button next to the Exposed Fields will compare the already defined Exposed Fields with the columns actually returned by the Sql Statement:



A click on the Synchronize button in the dialog will create the Exposed Fields on the view.



Cast

Both the list of Exposed Fields and the Synch dialog has a 'Cast' column. This is order for you to impose your knowledge of the actual data in the staging database.


Let's take for instance the column SourceProductType returned by the view:


case B.[SourceProductType]

   when '' then null

   else B.[SourceProductType]

end as [SourceProductType]


This case statement clearly means that the view may return a null value for the SourceProductType. That is also why the SourceProductType is marked as nullable in the Exposed Fields.


But say for instance that you know for sure there are no matching rows in the Valueset Vs.TranslateProductTypes that has an empty SourceProductType. So you know that the column SourceProductType returned by the view will never be null.


But if you remove the check mark for Nulls for the exposed Field SourceProductType, the Studio validation will complain:


By placing a check mark in the Cast column for SourceProductType, you tell Studio that you know what you are doing and that the query will never return Null for this column. 


So now the Studio validation will not complain anymore. On the other hand, if it turns out that you are wrong and the query actually does return null then the Hopp Runtime will fail when trying to populate the view.


Viewing the Referenced Objects

Clicking the Objects... button below the query will discover the objects actually referenced by the SQL query and open a dialog displaying a list of the Referenced Objects:




Updating query of an existing view

The synchronize feature can also significantly ease the work when updating the Sql Statement of an existing view. 


As an example, let's update the SQL query of the view above, so other objects a referenced and there are changes to the columns in the query result:

select
  A.[BankId]
,  A.[AccountType]                                           -- Moved
,  cast(A.[AccountNumber] as decimal(16)) as [AccountNumber] -- Datatype changed
--,  B.[TargetProductCode]                                   -- No longer returned
--,  case B.[SourceProductType]                              -- No longer returned
--      when '' then null
--      else B.[SourceProductType]
--   end as [SourceProductType]
,  B.[CustomerNumber]                                        -- Added
from
  Src.[Account] as A
  --inner join Vs.[TranslateProductTypes] as B	             -- No longer referenced
  --on
  --     A.[BankId] = B.[BankId]
  --and  A.[AccountType] = B.[SourceProductType]
  inner join Vw.[Customer] as B                              -- Added reference
  on
       A.[BankId] = B.[BankId]
  and  A.[ClientId] = B.[ClientId]

Synchronizing Exposed Fields

Again, synchronizing the Exposed Fields is easy and transparent, again making it clear what changes the synchronization will make to the Exposed Fields:



A click on the Synchronize button will automatically synchronize the list of Exposed Fields with the columns returned by the query.


Cast

The new query returns the AccountNumber column with a new data type, Num(16) instead of Num(10) and it proposes to update the data type of the Exposed Field accordingly.


The Cast column in the Synch dialog lets you lock in the existing Num(10) data type of the Exposed Field. This means that synchronize will not change the data type of the Exposed Field - but it will put a check mark in the Cast column for the field.


Referenced Objects

The Objects... button will now show the objects referenced by the modified query:



Testing the view

The query can be copied to the clipboard with the Copy button:



In addition to copying the query itself, Studio will also add a table variable containing the Exposed Fields defined by the view - including the data types defined on the exposed fields.


You can paste the query into Sql Server management Studio and test it there. The result of the query will be inserted into the table variable, and any data type discrepancies in columns you have Cast will be uncovered.



1The name of the view is clearly stated in a comment
2An Sql variable is declared for each Query Parameter used by the Query (id any)
3The testable query contains a table variable with the columns defined by the Exposed Fields
4The result of the query will be inserted into the table variable. This will uncover any data type discrepancies
5The lines that were added by Studio when copying to the clipboard are marked with a Token. You can paste all the query back into Studio and the marked lines will be removed on paste




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article