1

Is it possible to send a list of IDs to a stored procedure from c#?

UPDATE Germs
SET Mutated = ~Mutated
WHERE (GermID IN (ids))
4

4 回答 4

2

这可能是一个肮脏的黑客,但您可以创建一个临时表,然后从您的存储过程中加入它(假设它们在同一连接期间被访问)。例如:

CREATE TABLE #ids (id int)
INSERT INTO #ids VALUES ('123') -- your C# code would generate all of the inserts

-- From within your stored procedure...
UPDATE g
SET Mutated = ~Mutated
FROM Germs g
JOIN #ids i ON g.GermID = i.id
于 2008-10-08T14:32:17.037 回答
2

你可以试试我所做的: -

创建一个名为 Split_String 的函数

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE FUNCTION [dbo].[Split_String] 
(   
    @MyString varchar(5000)
)
RETURNS @Results TABLE
(
    Value varchar(1000)
) 
AS
    BEGIN
        DECLARE @Pos int
        DECLARE @StrLen int
        DECLARE @MyLen int
        DECLARE @MyVal varchar
        SET @pos = 1
        SET @MyLen = 1
        WHILE @MyString <> ''
            BEGIN
                SET @MyLen = charindex(',',@MyString)   
                IF @MyLen = 0 SET @MyLen = Len(@MyString)
                INSERT @Results SELECT replace(substring(@MyString, @pos, @MyLen),',','')
                SET @MyString = SUBSTRING(@MyString,@MyLen+1,len(@MyString))
            END
        RETURN 
    END

然后,当您使用 IN() 时,请按以下方式使用逗号分隔的字符串:-

SELECT * FROM [youDataBase].[dbo].[Split_String] (<@MyString, varchar(5000),>)
于 2008-10-08T14:39:14.133 回答
1

According to This article, you could try the Table Value Parameter.

于 2008-10-08T14:26:46.000 回答
0

Yep, you can use a chunk of XML to build your list of ID's. Then you can use OPENXML and select from that record set.

Look up OPENXML, sp_preparexmldocument, sp_removexmldocument

于 2008-10-08T14:30:24.370 回答