Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I wants to pass parameter to stored procedure which have multiple values.
means i have one parameter post and its value may be "inspector","police","Talati".
I wish to select all that member ,how can i do it!
Plz help

Thanks,
astika
Posted
Comments
Dasaradhi_r 5-Sep-12 2:07am    
Do you mean to say that you need single parameter to pass multiple values to stored procedure?

insert all these values in one table, say "temp", and say column "post"..

And then use following query:

SQL
select NameOfSamitiMember,Mobile,Post
from tblSamiti
where tblSamiti.VillageId=@VillageId
    and Post in (select post from temp)
 
Share this answer
 
 
Share this answer
 
u can then pass multiple values comma separated, for e.g. inspector,police,Talati and then u can split it in your SP..
 
Share this answer
 
Comments
astika 5-Sep-12 2:14am    
Sir,I have more than 10 values!is there any other way to give it in stroed procedure!
Hi,

Convert your list into CommaSeprated value and pass it to your parameter. On StoredProcedure side you can split the value.

But Why you need all information in StoredProcedure ? Instead you can create one master table and you can use that in your StoredProcedure.

Thanks
-Amit Gajjar
 
Share this answer
 
Comments
astika 5-Sep-12 2:19am    
<pre lang="sql">
select NameOfSamitiMember,Mobile,Post
from tblSamiti
where tblSamiti.VillageId=@VillageId
and Post ='saBaapati'
or Post='UpasaBaapataI'
or Post='sadsya'
or Post='UpasaBaapatI'
or Post='maajaI saBaapataI'
or Post='maajaI {pasaBaapataI'
or Post='maajaI sadsya'</pre>
now i wish to add other 5 post have can i do it?
AmitGajjar 5-Sep-12 2:21am    
As i told you, create one table for Post and use this all values from that table.
madhuri@mumbai 5-Sep-12 9:52am    
yes i agree with above solution,
i think you have diffrent post for same villageID,
so create table for post and map all post to vilageID(in another table) in diffrent table and take common id(i.e. mapped ID) for that and compare your post in SP.
AmitGajjar 5-Sep-12 10:06am    
Thank you.
Hi if you have lists of data(parameters) into dataset then you have to convert it into XMLstring using dataset.WriteXMl method and then pass that xmlstring into procedure as created below,

SQL
CREATE PROC [dbo].[USP_READXMLFILE] (@PXMLdata Text)
AS

  BEGIN
        SET NOCOUNT ON
        DECLARE @HANDLE INT
        DECLARE @SQUERY VARCHAR(1000)


        EXEC SP_XML_PREPAREDOCUMENT  @HANDLE OUTPUT,@PXMLdata
        SELECT * FROM OPENXML(@HANDLE, '/XMLData/tblparamter', 2)
        WITH (name VARCHAR(50),designation VARCHAR(50))

  END
 
Share this answer
 
If you are using SQL2008 or later, you could think about using 'Table Value Parameters'

C# and Table Value Parameters[^]

So, you create a Table type to hold your keys in SQL Server, e.g.

SQL
CREATE TYPE [dbo].[ItemKeyList] AS TABLE(
    ItemId [varchar](50) NULL
)


In SQL, you could write a stored procedure that uses this data type - eg, inserts some data into some table

SQL
CREATE PROCEDURE [dbo].[something_AddKeysToTable]
	(
		@MainId				BIGINT,
		@KeyList			AS KeyList READONLY
	)
AS
BEGIN
	SET NOCOUNT ON;
	
		INSERT INTO [SomeLookupTable]
			   ([MainId]
			   ,[KeyId])
		SELECT
			@MainId, ItemId 
		FROM
		    @KeyList
		WHERE
			ItemId 
		NOT IN 
		(
			SELECT [KeyId] FROM  [SomeLookupTable] WHERE  MainId = @MainId
		)
			            
END



In c#, you would create a List type that represents your table data

C#
    public class ItemList : List<string>, IEnumerable<sqldatarecord>
    {
        IEnumerator<sqldatarecord> IEnumerable<sqldatarecord>.GetEnumerator()
        {
            var sdr = new SqlDataRecord(new SqlMetaData("ItemId", SqlDbType.VarChar, 50));
            foreach (string item in this)
            {
                sdr.SetString(0, item);
                yield return sdr;
            }
        }
    }
</sqldatarecord></sqldatarecord></sqldatarecord></string>


In c#, when you want to send this data, you just use as per normal, we just need to inform that the SQL Parameter is a Structured datatype. This example converts a c# array into the SQL List type, then sends to the server

C#
public void AssignItemsToMain(long mainId, string[] itemIds)
{
    var list = new ItemList();
    list.AddRange(itemIds);

    using (var cmd = connection.CreateCommand() as SqlCommand)
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "something_AddKeysToTable";
        cmd.Parameters.AddWithValue("@MainId", mainId);

        var p = cmd.Parameters.Add("@ItemList", SqlDbType.Structured);
        p.Direction = ParameterDirection.Input;
        p.TypeName = "ItemKeyList";
        p.Value = list;

        cmd.ExecuteNonQuery();
    }
}


It's a bit of code to write up front, but obviously you can make this quite generic and reuse for all your list data :)
 
Share this answer
 
It depends on stored procedure.
If it accepts string parameters and is able to split string to particular values then you don't have much of a problem, you simply add concatenated values as your parameter value. For example:
C#
command.Parameters.AddWithValue("parameterName", concatenatedValues);


On the other hand you must repeat execution of your command for each value.
C#
//repeat for every value
foreach(string val in Values)
{
   //prepare command if necessary 

   //add value
   command.Parameters.AddWithValue("parameterName", val);

   //then execute command and something with data (if any) it returns
}


If you have means to alter stored procedure's code I advise you to alter it to accept strings with multiple strings (concatenated string).

I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900