我如何在程序中放入多个值? vb.net

时间:2015-12-03 10:30:18

标签: sql-server vb.net stored-procedures

我在vb.net中使用@values代码如下:

Dim con As New SqlConnection
                            Dim cmd As New SqlCommand

                            con = FunctionConnection()
                            cmd.Connection = con

                            cmd.CommandText = "GetVerification"
                            cmd.CommandType = CommandType.StoredProcedure
                            cmd.Parameters.AddWithValue("@Value", "1,2,3,4")

                            Dim Header_DataReader As SqlDataReader = Nothing

                            con.Open()
                            Header_DataReader = cmd.ExecuteReader

我的程序就是这样:

ALTER PROCEDURE GetVerification (@Value varchar(255))
as
BEGIN

SELECT        id, ControlPlanID, ParagraphID, ParagraphOrder, Specification, SpecificationParagraph, Dimension, Description, Samples, Frequency, ActivityResource, 
                         ActivityName, Observation, RequrementNom, RequrementPlus, RequrementMinus, Units
FROM            CP_Sequence
WHERE        (ParagraphOrder IN (@Value))
End

如何在我的程序中放入多个值,我想为数据库的4个不同行输入@values 1,2,3,4

如果我像这样的工作输入Sql代码而不是我的程序:

SELECT        id, ControlPlanID, ParagraphID, ParagraphOrder, Specification, SpecificationParagraph, Dimension, Description, Samples, Frequency, ActivityResource, 
                         ActivityName, Observation, RequrementNom, RequrementPlus, RequrementMinus, Units
FROM            CP_Sequence
WHERE        (ParagraphOrder IN (1, 2, 3, 4))

非常感谢

1 个答案:

答案 0 :(得分:1)

ALTER PROCEDURE dbo.GetVerification (@Value VARCHAR(255))
AS
BEGIN

    SET NOCOUNT ON

    DECLARE @a TABLE (ID INT PRIMARY KEY WITH (IGNORE_DUP_KEY=ON))
    INSERT INTO @a
    SELECT t.c.value('.', 'INT')
    FROM (SELECT x = CAST('<t>' + REPLACE(@Value, ',', '</t><t>') + '</t>' AS XML)) r
    CROSS APPLY r.x.nodes('/t') t(c)

    SELECT *
    FROM dbo.CP_Sequence
    WHERE ParagraphOrder IN (SELECT * FROM @a)
    OPTION(RECOMPILE)

END