English 中文(简体)
如何通过储存程序单一参数中的多重价值?
原标题:How to pass multiple values in a single parameter for a stored procedures?

我想通过一个参数的多个数值。 2005年

问题回答

您可以选择xml类输入变量,然后不包装这些元素和 gr。 例如:

DECLARE @XMLData xml

DECLARE 
    @Code varchar(10),
    @Description varchar(10)

SET @XMLData = 
 
    <SomeCollection>
      <SomeItem>
        <Code>ABCD1234</Code>
        <Description>Widget</Description>
      </SomeItem>
    </SomeCollection>
 

SELECT 
    @Code = SomeItems.SomeItem.value( Code[1] ,  varchar(10) ),
    @Description = SomeItems.SomeItem.value( Description[1] ,  varchar(100) )
FROM @XMLDATA.nodes( //SomeItem ) SomeItems (SomeItem)

SELECT @Code AS Code, @Description AS Description

结果:

Code        Description
==========  ===========
ABCD1234    Widget

页: 1 发挥职能:

ALTER  FUNCTION [dbo].[CSVStringsToTable_fn] ( @array VARCHAR(8000) )
RETURNS @Table TABLE ( value VARCHAR(100) )
AS 
    BEGIN
        DECLARE @separator_position INTEGER,
            @array_value VARCHAR(8000)  

        SET @array = @array +  , 

        WHILE PATINDEX( %,% , @array) <> 0 
            BEGIN
                SELECT  @separator_position = PATINDEX( %,% , @array)
                SELECT  @array_value = LEFT(@array, @separator_position - 1)

                INSERT  @Table
                VALUES  ( @array_value )

                SELECT  @array = STUFF(@array, 1, @separator_position,   )
            END
        RETURN
    END

摘自:

DECLARE @LocationList VARCHAR(1000)
SET @LocationList =  1,32 

SELECT  Locations 
FROM    table
WHERE   LocationID IN ( SELECT   CAST(value AS INT)
                           FROM     dbo.CSVStringsToTable_fn(@LocationList) )

页: 1

SELECT  Locations
FROM    table loc
        INNER JOIN dbo.CSVStringsToTable_fn(@LocationList) list
            ON CAST(list.value AS INT) = loc.LocationID

如果你试图将一个从SSRS到PROC的多价值清单,那是非常有益的。

<>strong>Edited: to show that You may need to CAST - However, to control what is sent in the CSV list

只是建议。 你在2005年服务器中确实能够这样做。 至少没有直截了当的方法。 你们必须使用CSV或XML或64或JSON基地。 然而,我强烈劝阻你这样做,因为所有这些错误都容易发生,并产生真正的大问题。

如果您能够转换到服务器2008年,你可以使用表价值的参数(Reference1 ,

If you cannot I d suggest you to consider the necessity of doing it in stored procedure, i.e. do you really want (should/must) to perform the sql action using SP. If you are solving a problem just use Ad hoc query. If you want to do so in education purposes, you might try don t even try the above mentioned things.





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签