> For the complete documentation index, see [llms.txt](https://docs.novacura.com/extensibility-and-integration/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.novacura.com/extensibility-and-integration/products/database/ms-sql-server/how-to-guides/query-sql-in-odd-ways.md).

# 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.&#x20;

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

```sql
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:&#x20;

```sql
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:&#x20;

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

![](/files/TIlIkaVHqukpqjjzDpF2)

### 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:&#x20;

```sql
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.

![This gives us a string with: Customer1..Customer2.. etc.](/files/FIfHcapOosSubTr9yMLR)

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.&#x20;

```sql
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) 
```

![](/files/4lznIiLVBeBQoEzOo6EG)<img src="/files/LiPkuFpapXlLsk3QLyGP" alt="" data-size="original">![](/files/jk3tkWtNLqQJZSIw9pS4)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.novacura.com/extensibility-and-integration/products/database/ms-sql-server/how-to-guides/query-sql-in-odd-ways.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
