T-SQL query to add multiple valued report parameter

  • SELECT

    Persons.P_id

    ,Persons.LastName

    ,Persons.FirstName

    ,Persons.Address

    ,Persons.City

    FROM

    Persons

    where Persons.P_id IN (@P_id)

    the above query consider only one parameter when I try to pass two parameters say '1,2' error: conversion failed. how can i pass multiple values by adding simple T-SQL query as,I am directly working on dataset I can not work with ssrs reports and allow multiple values or stored procedure. Is their any other way Just to write any simple t-sql query to solve this. Any help would be appreciated.

  • first create function after call that function in your query...

    Text

    CREATE FUNCTION SPLIT(@VAL VARCHAR(MAX))

    RETURNS @T1 TABLE(COL1 VARCHAR(MAX))

    AS

    BEGIN

    WHILE CHARINDEX(',',@VAL)>0

    BEGIN

    INSERT INTO @T1 VALUES(SUBSTRING(@VAL,1,(CHARINDEX(',',@VAL))-1))

    SET @val=SUBSTRING(@VAL,(CHARINDEX(',',@VAL))+1,LEN(@VAL))

    END

    INSERT INTO @T1 VALUES(@VAL)

    RETURN

    END

    SELECT

    Persons.P_id

    ,Persons.LastName

    ,Persons.FirstName

    ,Persons.Address

    ,Persons.City

    FROM

    Persons

    where Persons.P_id IN (select * FROM split(@P_id))

  • Thank you so much. Thant worked for my code

  • Hi,

    This code works good when my souce is SQL. Now when my datasource is not SQL and it is Oracle then it is not working. how should I implement this code to work when choose Oracle datasource for shared dataset query designer.

    Any suggestions.

    Thanks

  • Your original requirement was a T-SQL query which, if I understand correctly, isn't going to work against an Oracle database. You'll need to go to an Oracle forum to get help on that. There may be a way of writing code that works on both Oracle and SQL Server.

    John

  • i executed the the below function

    CREATE FUNCTION SPLIT(@VAL VARCHAR(MAX))

    RETURNS @T1 TABLE(COL1 VARCHAR(MAX))

    AS

    BEGIN

    WHILE CHARINDEX(',',@VAL)>0

    BEGIN

    INSERT INTO @T1 VALUES(SUBSTRING(@VAL,1,(CHARINDEX(',',@VAL))-1))

    SET @val=SUBSTRING(@VAL,(CHARINDEX(',',@VAL))+1,LEN(@VAL))

    END

    INSERT INTO @T1 VALUES(@VAL)

    RETURN

    END

    Below mentioned is my SP

    --exec Fms_Subject 'M001,L001,D004,B001'

    Alter Proc Fms_Subject

    @Subject_No Varchar(25)

    As

    Begin

    Set NoCount ON

    select Subj_SCode,Subj_SName from ERP_Subj

    WHERE (Subj_SCode IN (select Subj_SCode from split(@Subject_No)))

    order by Subj_SCode

    End

    when i executed this SP its showing all the records,it is supposed to show the records pertained to passed values i.e: 'M001,L001,D004,B001'

    can you tell me how to get the data pertained to only specified values.

  • SSRS will automatically properly expand an "IN (@multi_valued_report_parameter)" if it's in an SSRS dataset and follows some basic rules. You may want to verify that you have a proper SSRS dataset.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply