简体   繁体   中英

SQL Server stored procedure unkown number of variables

I have a SQL Server 2008 database with multiple tables. I am writing a stored procedure for the user to get some market values from the tables.

Eg. I have a table which has columns Currency , [Market Value] , [Hedging] and [Customer ID] .

Now the user can choose to see the Currency, Market Value and Hedging by specifying a Customer ID.

However I would like the user to be able to set multiple customer id's and see a table with aggregated values.

So for instance I would like to see the Currency, Market Value and Hedging for Customer ID 2, 3 and 7.

The Select statement would look something like this:

Select 
    [Currency], 
    sum([Market Value]), 
    sum([Hedging]) 
from 
    myDatabase
where 
    ([Customer ID] = 2 or [Customer ID] = 3 or [Customer ID] = 7)
Group by 
    [Currency], [Customer ID]

I could then specify a parameter and let the user decide which customer ID to use. The problem is, I do not always know which combination of customer id's that the user would like to see.

If I knew that there would always be three customer id's I could just set three parameters, but sometimes it might be only one customer id, other times it might be 5.

Is there a smooth way to come around this?

I was thinking about setting my select statement as a string and then executing it by the exec command - however my procedure is quite long with a couple of inner and left joins, temporary tables etc, so that solution might not be the best option.

Does anyone have a bright idea on how to solve this?

Since you are using SQL 2008 I would suggest a better alternative that is both strongly typed and avoids the need of creating dynamic SQL: User Defined Table Types (UDTT)

You can create a generic table type that holds integer values like so:

CREATE TYPE IntegerList AS TABLE 
(
    Value int NOT NULL  
)
GO

Then in your stored procedure you would specify a parameter of this type:

CREATE PROCEDURE [dbo].[MyProcedure]
    @customerIds IntegerList READONLY
AS
BEGIN

    -- here you can use the @customerIds variable as a (read only) table:
    SELECT Value FROM @customerIds

END
where ([Customer ID] IN ({ids placeholder})

您可以使用“ 1,3,5,... n”字符串构建SELECTreplacing {ids placeholder}

Use a parameter in your stored procedure and pass in the comma separated list as a parameter, for example:

Declare @customerIds nvarchar(100)
select @customerIds = '12,44,3,345'

Select [Currency], [Customer ID], sum([Market Value]), sum([Hedging]) from myDatabase
    where [Customer ID] ,in (@customerIds)
    Group by [Currency], [Customer ID]

Also if you want to see total aggregates, remove the [Customer ID] from your group by

Declare @customerIds nvarchar(100)
select @customerIds = '12,44,3,345'

Select [Currency], sum([Market Value]), sum([Hedging]) from myDatabase
    where [Customer ID] ,in (@customerIds)
    Group by [Currency]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM