How can I pass an “array” of values to my stored procedure?

后端 未结 7 717
悲哀的现实
悲哀的现实 2020-12-15 22:39

I want to be able to pass an \"array\" of values to my stored procedure, instead of calling \"Add value\" procedure serially.

Can anyone suggest a way to do it? am I

相关标签:
7条回答
  • 2020-12-15 22:55

    Incidently, here is how you would add the array to a function (stored-proc) call:

    CallableStatement proc = null;
    List<Integer> faultcd_array = Arrays.asList(1003, 1234, 5678);
    //conn - your connection manager
    conn = DriverManager.getConnection(connection string here);
    proc = conn.prepareCall("{ ? = call procedureName(?) }");
    proc.registerOutParameter(1, Types.OTHER);
    //This sets-up the array
    Integer[] dataFaults = faultcd_array.toArray(new Integer[faultcd_array.size()]);
    java.sql.Array sqlFaultsArray = conn.createArrayOf("int4", dataFaults);
    proc.setArray(2, sqlFaultsArray);
    //:
    //add code to retrieve cursor, use the data.
    //:
    
    0 讨论(0)
  • 2020-12-15 23:01

    If you plan to use MySQL 5.1, it is not possible to pass in an array.
    See the MySQL 5.1 faq
    If you plan to use PostgreSQL, it is possible look here

    0 讨论(0)
  • 2020-12-15 23:05

    Thanks to JSON support in MySQL you now actually have the ability to pass an array to your MySQL stored procedure. Create a JSON_ARRAY and simply pass it as a JSON argument to your stored procedure. Then in procedure, using MySQL's WHILE loop and MySQL's JSON "pathing" , access each of the elements in the JSON_ARRAY and do as you wish. An example here https://gist.githubusercontent.com/jonathanvx/513066eea8cb5919b648b2453db47890/raw/22f33fdf64a2f292688edbc67392ba2ccf8da47c/json.sql

    0 讨论(0)
  • 2020-12-15 23:06

    I don't know about passing an actual array into those engines (I work with sqlserver) but here's an idea for passing a delimited string and parsing it in your sproc with this function.

    CREATE FUNCTION [dbo].[Split]
    (
        @ItemList NVARCHAR(4000), 
        @delimiter CHAR(1)
    )
    RETURNS @IDTable TABLE (Item VARCHAR(50))  
    AS      
    
    BEGIN    
        DECLARE @tempItemList NVARCHAR(4000)
        SET @tempItemList = @ItemList
    
        DECLARE @i INT    
        DECLARE @Item NVARCHAR(4000)
    
        SET @tempItemList = REPLACE (@tempItemList, ' ', '')
        SET @i = CHARINDEX(@delimiter, @tempItemList)
    
        WHILE (LEN(@tempItemList) > 0)
        BEGIN
            IF @i = 0
                SET @Item = @tempItemList
            ELSE
                SET @Item = LEFT(@tempItemList, @i - 1)
            INSERT INTO @IDTable(Item) VALUES(@Item)
            IF @i = 0
                SET @tempItemList = ''
            ELSE
                SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
            SET @i = CHARINDEX(@delimiter, @tempItemList)
        END 
        RETURN
    END  
    
    0 讨论(0)
  • 2020-12-15 23:08

    You didn't indicate, but if you are referring to SQL server, here's one way.

    And the MS support ref.

    0 讨论(0)
  • 2020-12-15 23:15

    As Chris pointed, in PostgreSQL it's no problem - any base type (like int, text) has it's own array subtype, and you can also create custom types including composite ones. For example:

    CREATE TYPE test as (
        n int4,
        m int4
    );
    

    Now you can easily create array of test:

    select ARRAY[
        row(1,2)::test,
        row(3,4)::test,
        row(5,6)::test
    ];
    

    You can write a function that will multiply n*m for each item in array, and return sum of products:

    CREATE OR REPLACE FUNCTION test_test(IN work_array test[]) RETURNS INT4 as $$
    DECLARE
        i      INT4;
        result INT4 := 0;
    BEGIN
        FOR i IN SELECT generate_subscripts( work_array, 1 ) LOOP
            result := result + work_array[i].n * work_array[i].m;
        END LOOP;
        RETURN result;
    END;
    $$ language plpgsql;
    

    and run it:

    # SELECT test_test(
        ARRAY[
            row(1, 2)::test,
            row(3,4)::test,
            row(5,6)::test
        ]
    );
     test_test
    -----------
            44
    (1 row)
    
    0 讨论(0)
提交回复
热议问题