0

I am using the directions for speeding up an SSRS report shown in a question Here. But the difference on this query is that one of the parameters from the SSRS report results in a list of strings, example:

'100031', '100131', '114647', '115101'

Here is the query I'm trying(With the parameters hardcoded) but the VarChar doesn;t work. is there a data type i can use to accomplish what is in the above mentioned link with a list of strings?

Declare @cust as VarChar(Max)
set @cust = 
'100031',
'100131',
'114647',
'115101'
SELECT
    DELIVERYNAME + ' ' + DELIVERYADDRESS AS 'Deliverly'
FROM
    dbo.SALESLINE INNER JOIN 
    dbo.CUSTPACKINGSLIPTRANS ON ORIGSALESID = dbo.SALESLINE.SALESID AND dbo.SALESLINE.ITEMID = dbo.CUSTPACKINGSLIPTRANS.ITEMID
WHERE
    CUSTACCOUNT in (@cust)
    AND
    dbo.CUSTPACKINGSLIPTRANS.CREATEDDATE BETWEEN '08/03/2015' AND '08/07/2015'
    AND
    dbo.SALESLINE.DIMENSION2_ IN ('08')
3

3 Answers 3

1

You can create a table-type variable and pass it in as READ ONLY. It can be useful when you need array-like behavior from inputs. Unfortunately, you can't have a table-type output parameter.

create type dbo.CustomerIdArray as table (CustAccount int);

You could pass that into a procedure as READONLY and join to it as if it were a table. You'll need to alias it, because you can't use @ in the join syntax.

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't like the Table-type, and don't have access to a Tally/Number table, here's a solution that uses a common-table-expression to give you a Tally table which you can quickly use to separate a comma-separated string into rows on a table variable. You can then use the table variable in a join.

-- Here's your original string:
Declare @cust as VarChar(Max)
set @cust = '100031, 100131, 114647, 115101'

-- Declare Table variable to hold the rows of @cust
declare @TableOfCustAccount Table (CUSTACCOUNT int not null);

-- comma-separated values must begin and end with commas:
set @cust = ',' + @cust + ',';

-- Tally table CTE: add more joins if you don't have enough N values
;with Tally
as (
    select row_number() over (order by t1.column_id) as 'N'
      from sys.columns t1
      join sys.columns t2
        on 1=1
      -- You can get more N values by adding joins like this:
      -- join sys.columns t3
      --   on 1=1
   )
insert @TableOfCustAccount (CUSTACCOUNT)
select substring(@cust,N+1,charindex(',',@cust,N+1)-N-1)
  from Tally
 where N < len(@cust)
   and substring(@cust,N,1) = ',';

Now you can use @TableOfCustAccount as a join in your query.

Comments

1

There isn't a "list" data type in SQL, but what you can do is use varchar (or nvarchar) and dynamic SQL to accomplish what you are trying to do. For example:

Declare @cust as VarChar(Max)
set @cust = '100031, 100131, 114647, 115101'

DECLARE @sql as nvarchar(max) = '
SELECT
    DELIVERYNAME + '' '' + DELIVERYADDRESS AS ''Deliverly''
FROM
    dbo.SALESLINE INNER JOIN 
    dbo.CUSTPACKINGSLIPTRANS ON ORIGSALESID = dbo.SALESLINE.SALESID AND dbo.SALESLINE.ITEMID = dbo.CUSTPACKINGSLIPTRANS.ITEMID
WHERE
    CUSTACCOUNT in (' + @cust + ')
    AND
    dbo.CUSTPACKINGSLIPTRANS.CREATEDDATE BETWEEN ''08/03/2015'' AND ''08/07/2015''
    AND
    dbo.SALESLINE.DIMENSION2_ IN (''08'')
'

-- this is the query that was generated
SELECT @sql

-- this executes the query
EXEC sp_executesql @sql

I left the items in @cust as integers, but if you wanted them to be strings, you would have to include the quote literals when you set @cust.

There may be some concerns with SQL injection if this is something that is public-facing and gets the @cust parameters provided via user input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.