简体   繁体   中英

SSRS Report Works when the Parameter uses the “=” Operator but not “IN”

I will try to keep this short.

I have a report in SSRS where the @Position parameter needs to be able to accept multiple values (see the parameter in the WHERE clause below).

DECLARE @multiplier INT;

SET @multiplier = 30

SELECT p.Fname, 
       p.Lname,
       p.Position,
       (SUM(plg.PTS)/SUM(plg.MP))*@multiplier AS PTS,
       (SUM(plg.TRB)/SUM(plg.MP))*@multiplier AS TRB,
       (SUM(plg.AST)/SUM(plg.MP))*@multiplier AS AST,
       (SUM(plg.BLK)/SUM(plg.MP))*@multiplier AS BLK,
       (SUM(plg.STL)/SUM(plg.MP))*@multiplier AS STL,      
       (SUM(plg.TOV)/SUM(plg.MP))*@multiplier AS TOV,
       (SUM(plg.FT)/SUM(plg.MP))*@multiplier AS FTs,
       --SUM(plg.FTA)/SUM(plg.MP))*@multiplier AS FTAs,
       SUM(plg.FT)/SUM(plg.FTA) AS FT_Percentage,
       (SUM(plg.FG)/SUM(plg.MP))*@multiplier AS FGs,
       --SUM(plg.FGA)/SUM(plg.MP))*@multiplier AS FGAs,
       SUM(FG)/SUM(FGA) as Field_Percentage,
       (SUM(plg.[3P])/SUM(plg.MP))*@multiplier AS Threes,
       --SUM(plg.[3PA])/SUM(plg.MP))*@multiplier AS TP%
       SUM([3P])/SUM([3PA]) AS Three_Point_Percentage
FROM PlayerGameLog plg
INNER JOIN Players p
ON p.PlayerID = plg.PlayerID
WHERE plg.PlayerID IN (SELECT PlayerID
            FROM Players
            WHERE lname != 'westbrook')
    AND p.TeamID = 'OKC'
    AND p.Position = @Position
GROUP BY p.Fname, p.Lname, p.Position
ORDER BY PTS DESC;

If I change the following line:

AND p.Position = @Position

To:

AND p.Position IN @Position

I get a message that prompts me to Define Query Paramters . No matter what I set those to, I encounter a message that says there is an error. I've changed the parameter to allow multiple values, and still no dice. Any ideas?

Try using a dynamic SQL

Something like this

DECLARE @SQLString varchar(8000)
SET SQLString = 'SELECT p.Fname, 
       p.Lname,
       p.Position,
       (SUM(plg.PTS)/SUM(plg.MP))*@multiplier AS PTS,
       (SUM(plg.TRB)/SUM(plg.MP))*@multiplier AS TRB,
       (SUM(plg.AST)/SUM(plg.MP))*@multiplier AS AST,
       (SUM(plg.BLK)/SUM(plg.MP))*@multiplier AS BLK,
       (SUM(plg.STL)/SUM(plg.MP))*@multiplier AS STL,      
       (SUM(plg.TOV)/SUM(plg.MP))*@multiplier AS TOV,
       (SUM(plg.FT)/SUM(plg.MP))*@multiplier AS FTs,
       --SUM(plg.FTA)/SUM(plg.MP))*@multiplier AS FTAs,
       SUM(plg.FT)/SUM(plg.FTA) AS FT_Percentage,
       (SUM(plg.FG)/SUM(plg.MP))*@multiplier AS FGs,
       --SUM(plg.FGA)/SUM(plg.MP))*@multiplier AS FGAs,
       SUM(FG)/SUM(FGA) as Field_Percentage,
       (SUM(plg.[3P])/SUM(plg.MP))*@multiplier AS Threes,
       --SUM(plg.[3PA])/SUM(plg.MP))*@multiplier AS TP%
       SUM([3P])/SUM([3PA]) AS Three_Point_Percentage
FROM PlayerGameLog plg
INNER JOIN Players p
ON p.PlayerID = plg.PlayerID
WHERE plg.PlayerID IN (SELECT PlayerID
            FROM Players
            WHERE lname != '''westbrook''')
    AND p.TeamID = '''OKC'''
    AND p.Position IN( ' + @Position + ')
GROUP BY p.Fname, p.Lname, p.Position
ORDER BY PTS DESC;'

EXEC @SQLString

You're getting confused between the scope of the different parts of an SQL query.

Consider these two clauses:

WHERE Position IN ('1', '2', '3')
WHERE Position IN ('1, 2, 3')

Can you see that these are not equivalent? The first one is looking for any one of three single-digit values. The second one is looking for a SINGLE value, a string 7 characters long having 3 digits, 2 spaces, and 2 commas.

When you do Position IN @Position , you're performing something like the second expression. The problem, of course, is that the string contents don't somehow "break out" of their value and become SQL so that they represent three values. It's one value, and one value it will stay.

I encourage you first to stop using a stored procedure and write your code in an application layer where you can compose the SQL statement you want. You can then build a proper SQL statement using IN . Or you could insert to a temp table and join to that.

Alternately, if you absolutely must continue using a stored procedure, then parse the contents of @Position into a temp table or table variable. Look up SQL string splitting.

Finally, as mentioned in another answer, you could use dynamic SQL, but this is best avoided. Don't go there if you can help it. I promise you, it's not good to go down this route if you can at ALL avoid it.

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