RETURNS @retArray TABLE ( value VARCHAR(100) ) AS BEGIN DECLARE @posStart BIGINT, @posNext BIGINT, @valLen BIGINT, @sValue NVARCHAR(100);
IF @sDelim IS NULL BEGIN IF LEN(@sText)>100 SET @sText = SUBSTRING(@sText, 1, 100)
INSERT @retArray (value) VALUES (@sText); END ELSE BEGIN SET @posStart = 1;
WHILE @posStart <= LEN(@sText) BEGIN SET @posNext = CHARINDEX(@sDelim, @sText, @posStart);
IF @posNext <= 0 SET @valLen = LEN(@sText) - @posStart + 1; ELSE SET @valLen = @posNext - @posStart;
SET @sValue = SUBSTRING(@sText, @posStart, @valLen); SET @posStart = @posStart + @valLen + 1;
IF LEN(@sValue) > 0 BEGIN IF LEN(@sValue)>100 SET @sValue = SUBSTRING(@sValue, 1, 100)
INSERT @retArray (value) VALUES (@sValue); END END END RETURN END --测试 /*DECLARE @t VARCHAR(50) SET @t ='1,2' SELECT @t SELECT id, ArtNum FROM TB_Article_Info WHERE id IN (SELECT * from dbo.intstringtoint(@t,',')) ------ --程序里边直接把类似 delete from tb1 where id in (SELECT * from dbo.intstringtoint(tb2,','))的语句写进去就行了,希望对你有用
------解决方案-------------------- 直接把字符串替换一下就行了 例文本框输入 2,4,9 string a,b; a = TextBox1.Text.Replace(",","','");//这里把,替换成',' //于是文本框的值就变成 2','4','9 b = "'" + a + "'"; //这里再前后加上' 就变成'2','4','9' 下面简单啦,直接上sql
------解决方案-------------------- 首先你要把2,4,9变成int的啊, 定义个数组 arr[]=num.split(',') 这样arr[0]=2,arr[1]=4,arr[2]=9. 在遍历数组,删除每一项 for(int i=0;i<3;i++){ int number=arr[i]; new SqlParameter("@num", number)}
------解决方案--------------------
There are at least a dozen ways to pass an array or list to a query; each has their own unique pros and cons.
?Table-Valued Parameters. SQL 2008 and higher only, and probably the closest to a universal "best" approach. ?The Iterative Method. Pass a delimited string and loop through it. ?Using the CLR. SQL 2005 and higher from .NET languages only. ?XML. Very good for inserting many rows; may be overkill for SELECTs. ?Table of Numbers. Higher performance/complexity than simple iterative method.