WHERE IN (array of IDs)

后端 未结 9 1267
暖寄归人
暖寄归人 2020-11-28 11:16

I have webservice which is passed an array of ints. I\'d like to do the select statement as follows but keep getting errors. Do I need to change the array to a string?

9条回答
  •  醉酒成梦
    2020-11-28 11:50

    You can use this. Execute in SQLServer to create a function on your DB (Only once):

    IF EXISTS(
        SELECT *
        FROM sysobjects
        WHERE name = 'FN_RETORNA_ID_FROM_VARCHAR_TO_TABLE_INT')
    BEGIN
        DROP FUNCTION FN_RETORNA_ID_FROM_VARCHAR_TO_TABLE_INT
    END
    GO
    
    CREATE FUNCTION [dbo].FN_RETORNA_ID_FROM_VARCHAR_TO_TABLE_INT (@IDList VARCHAR(8000))
    RETURNS
        @IDListTable TABLE (ID INT)
    AS
    BEGIN
    
        DECLARE
            --@IDList VARCHAR(100),
            @LastCommaPosition INT,
            @NextCommaPosition INT,
            @EndOfStringPosition INT,
            @StartOfStringPosition INT,
            @LengthOfString INT,
            @IDString VARCHAR(100),
            @IDValue INT
    
        --SET @IDList = '11,12,113'
    
        SET @LastCommaPosition = 0
        SET @NextCommaPosition = -1
    
        IF LTRIM(RTRIM(@IDList)) <> ''
        BEGIN
    
            WHILE(@NextCommaPosition <> 0)
            BEGIN
    
                SET @NextCommaPosition = CHARINDEX(',',@IDList,@LastCommaPosition + 1)
    
                IF @NextCommaPosition = 0
                    SET @EndOfStringPosition = LEN(@IDList)
                ELSE
                    SET @EndOfStringPosition = @NextCommaPosition - 1
    
                SET @StartOfStringPosition  = @LastCommaPosition + 1
                SET @LengthOfString = (@EndOfStringPosition + 1) - @StartOfStringPosition
    
                SET @IDString =  SUBSTRING(@IDList,@StartOfStringPosition,@LengthOfString)                  
    
                IF @IDString <> ''
                    INSERT @IDListTable VALUES(@IDString)
    
                SET @LastCommaPosition = @NextCommaPosition
    
            END --WHILE(@NextCommaPosition <> 0)
    
        END --IF LTRIM(RTRIM(@IDList)) <> ''
    
        RETURN
    
    ErrorBlock:
    
        RETURN
    
    END --FUNCTION
    

    After create the function you have to call this on your code:

    command.CommandText = @"SELECT id,
                            startDateTime, endDateTime From
                            tb_bookings WHERE buildingID IN
                            (SELECT ID FROM FN_RETORNA_ID_FROM_VARCHAR_TO_TABLE_INT(@buildingIDs))) AND startDateTime <=
                            @fromDate";
    
    command.Parameters.Add(new SqlParameter(){
                               DbType = DbType.String,
                               ParameterName = "@buildingIDs",
                               Value = "1,2,3,4,5" //Enter the parameters here separated with commas
                           });
    

    This function get the text inner commas on "array" and make an table with this values as int, called ID. When this function is on you DB you can use in any project.


    Thanks to Microsoft MSDN.

    Igo S Ventura

    Microsoft MVA

    Sistema Ari de Sá

    igo1-2@hotmail.com

    P.S.: I'm from Brazil. Apologize my english... XD

提交回复
热议问题