Extensibility and Integration
  • 🔌Extensibility and Integration
  • 📦Products
    • Active Directory
      • Microsoft Graph
        • Overview
        • Tutorials
        • How to Guides
          • Create App Registration
          • Limit Permissions to a Specific Mailbox
      • OpenID
        • UPN Transformation
    • Cognitive Services
      • Form Recognizer
        • Overview
        • Tutorials
        • How to Guides
          • Set Up Form Recognizer
      • Anomaly Detector
        • Overview
        • Tutorials
        • How to Guides
    • Database
      • MS SQL Server
        • Overview
        • Tutorial
          • Flow on MS SQL - Basics
          • Flow on MS SQL - Extended
        • How to Guides
          • Query SQL in odd ways
    • ERP
      • Infor M3 Cloud
        • Overview
        • Tutorials
        • How to Guides
      • IFS Cloud
        • Overview
        • Tutorials
        • How to Guides
          • Import Application Configuration Packages
          • Add a new projection to the connector
          • Add a new server to the connector
      • IFS App 10
        • Overview
        • Tutorials
        • How to Guides
          • Import Application Configuration Packages
    • Storage
      • Azure Storage Account
        • Overview
        • Tutorials
        • How to Guides
          • Set Up Azure Storage Account
    • Studio
      • Applications
        • Workflow
          • Overview
          • Tutorials
          • How to Guides
            • Create Global Properties
            • Import Workflows
            • Setting Menu Roles
            • Publishing Workflows
            • Bluetooth Low Energy Quick Start Guide
      • Environment
        • Import Flow Properties
      • Connectors
        • Database
          • IFS Apps 10
            • Overview
            • Tutorials
            • How To Guides
              • Configure IFS Apps 10 Connector
        • Email
          • How to Guides
            • Email Example
        • File System
          • How to Guides
            • File System Example
            • Importing data from flat files (or other sources)
              • Scheduled Flows
        • HTML to PDF
          • How to Guides
            • HTML to PDF Example
        • Table Operation
          • How To Guides
            • Table Operations Example
        • REST Connector
          • How To Guides
            • Import a REST Configuration
            • Authenticate
              • OAUTH2
              • Cookie
          • Known Issues
            • HTTP Method: Delete - with body - not supported
            • HTTP Method: POST- Header parameters are not passed if no Body is sent
          • Tutorial
            • REST Basic training using Flow and Postman
        • OPC UA Client
          • How to Guides
            • Configure OPC UA Client Connector
        • OData Connector
        • OData Connector
    • Portal 2
      • Portlet
        • Basic
          • Data Tree
            • Overview
            • Tutorial
            • How to Guides
        • Visual Planning
          • Scheduler
            • Overview
            • Tutorial
              • Configure Scheduler
            • How to Guides
      • How To Guides
        • Start browser and auto login
    • Clients
      • Mobile Clients
        • Preference file
  • 📚Resources
    • Flow Help
    • Flow Forum
Powered by GitBook
On this page
  • Query by building a string for execution
  • Select from a Procedure
  • Building a Where in-clause from rows in a column from a different data source

Was this helpful?

  1. Products
  2. Database
  3. MS SQL Server
  4. How to Guides

Query SQL in odd ways

Query by building a string for execution

Sometimes you need to build a query that include flow variables, like for example @userid. But if you would just add select * from table where userId like '@userid%' you would get errors.

To get around that issue you can build a string and then execute it in this way:

Declare @SQL nvarchar(max);

Set @SQL = '
Declare @UserId nvarchar (max);
SELECT sol.[SalesOrderNo]                                                 "SalesOrderNo",
       sol.[SalesOrderLineNo]                                             "SalesOrderLineNo",
       sol.[PartNo]                                                       "PartNo",
       sol.[Qty]                                                          "Qty"
FROM   SalesOrderLine sol
WHERE  sol.SalesStatusId = 10
       AND so.CreatedBy like ''' + @userId + '%'''
       
Exec (@SQL);

Select from a Procedure

Assume you have a stored procedure that executes a query that you would like to execute and get the values into Flow from. Say you have a stored procedure GetCustomers:

Create or Alter PROCEDURE dbo.GetCustomers AS
SELECT top 10 CustomerId, CustomerName 
FROM Customer

Declare a table to pass the results to, and then select from your table variable:

Declare @T Table (CustomerId nvarchar(50), CustomerName nvarchar(500)) 
Insert @T Exec GetCustomers 
Select CustomerId "CustomerId",CustomerName "CustomerName" from @T

Building a Where in-clause from rows in a column from a different data source

This time we have a customer list in another system that we are using Flow to extract from:

SELECT TOP 10 CustomerId   "CustomerId"
FROM   Customer sql

By using flow script's "Join" function (not to be confused with SQL join) we can put values of a table column into a single textual value with a given separator.

With our result now in a string, we will replace the double dots with a single quote, a comma and another single quotes (',') by using char(xx). Note how the first and last char(39) is outside of the replace function to put single quotes before the first value and after the last. So our string now looks like 'Customer1', 'Customer2'..etc. And is now ready to put in our @SQL variable and to execute.

Declare @ReplaceVar nvarchar(max);
Declare @SQL nvarchar(max);

set @ReplaceVar = char(39) + replace(@SelList, '..', char(39) + char(44)+ char(39)) +char(39)

set @SQL = '
select top 10 SalesOrderNo, CustomerId
from salesorder so
WHERE so.CustomerId in (' + @ReplaceVar + ')'

exec (@SQL) 
PreviousHow to GuidesNextERP

Last updated 3 years ago

Was this helpful?

📦
This gives us a string with: Customer1..Customer2.. etc.