Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I supply a List<int> to a SQL parameter? [duplicate]

Tags:

I have a SQL statement like the following:

... const string sql = @"UPDATE PLATYPUS SET DUCKBILLID = :NEWDUCKBILLID WHERE PLATYPUSID IN (:ListOfInts)"; ... ocmd.Parameters.Add("ListOfInts", ??WhatNow??); 

How can I provide the comma separated list of ints, which could be any (reasonable*) number of values

  • By "reasonable" in this case I mean between one and a couple dozen.
like image 593
B. Clay Shannon-B. Crow Raven Avatar asked Jul 16 '12 16:07

B. Clay Shannon-B. Crow Raven


People also ask

How do I pass a list as parameter in SQL stored procedure?

CREATE FUNCTION dbo. SplitInts ( @List VARCHAR(MAX), @Delimiter VARCHAR(255) ) RETURNS TABLE AS RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM ( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') + '</i>'). query('.

Can we use list in SQL?

You can create lists of SQL Query or Fixed Data values . In the Data Model components pane, click List of Values and then click Create new List of Values. Enter a Name for the list and select a Type.


1 Answers

You can't, you have to create some helper function that replaces :ListOfInts with ( for example ) :I0,:I1,:I2... and pass the parameter by adding one by one in code. The function you want is gracefully emulated by Dapper.Net in its list support.

like image 66
Felice Pollano Avatar answered Nov 28 '22 10:11

Felice Pollano