简体   繁体   中英

How to pass a string array like to stored procedure?

I am using asp mvc c# and i want to know if it is possible to pass my string like array to IN operated in stored procedure. I have an example below:

C#

string temp="123,234,234";

SQL:

@temp varchar(50)
Select * from table where column in @temp

It returns

Conversion failed when converting the nvarchar value '123,234,234' to data type int.

I wanted to pass is as stored procedure but got lost where to start now.

Things I've tried:

  1. Try using Replace function but stil does not work.

As you've found, you can't do this, because a string containing numeric character sequences separated by commas is not the same as an SQL program that contains constant numbers separated by commas. For example, you wouldn't expect this c# to work either:

int[] a =  new [] { 1,2,3 }; //works
int[] b =  "new [] { 1,2,3 }"; //doesn't work

Nor would you expect this IN to work (which is really what you're doing:

SELECT * FROM t WHERE x IN('1,2,3')

If your column were a string, you wouldn't get an error but it wouldn't work like you expect either

SELECT * FROM t WHERE x IN ('a','b'). --works
SELECT * FROM t WHERE x IN ('''a'',''b'''). --valid, but doesn't select x that are "a" or "b"

So, all in, IN is like a function that takes N arguments of various types. It doesn't look at a string you pass and see it has commas and split them


As such you either pass as many parameters as you have items:

SELECT * FROM t WHERE x IN (@p1, @p2, @p3)

Your sproc takes 3 parameters, you put one value per parameter, if you only seek two numbers you just repeat the last number because IN(1,2,2) is the same as IN(1,2)


Or you create a type:

CREATE TYPE dbo.IntList AS TABLE 
(
      X INT NOT NULL
)

You declare your sproc to take a variable of that type

@numList x

You declare a datatable in c# that represents that type

var dt = new DataTable();
dt.Columns.Add("X", typeof(int));

You fill it with one int per row

You declare your param as Structured

command.Parameters.AddWithValue("@numList", dt).SqlDbType = SqlDbType.Structured;

And you execute it. Remember that your @numList is a table like any other so you can't say SELECT * FROM t WHERE x IN (@numlist) , you either do SELECT * FROM t JOIN @numList n ON tx = nx or SELECT * FROM t WHERE x IN (SELECT x FROM @numlist) etc

I am using asp mvc c# and i want to know if it is possible to pass my string like array to IN operated in stored procedure. I have an example below:

C#

string temp="123,234,234";

SQL:

@temp varchar(50)
Select * from table where column in @temp

It returns

Conversion failed when converting the nvarchar value '123,234,234' to data type int.

I wanted to pass is as stored procedure but got lost where to start now.

Things I've tried:

  1. Try using Replace function but stil does not work.

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