Postgres integer arrays as parameters?

前端 未结 3 920
灰色年华
灰色年华 2020-12-13 02:13

I understand that in Postgres pure, you can pass an integer array into a function but that this isn\'t supported in the .NET data provider Npgsql.

I currently have

3条回答
  •  有刺的猬
    2020-12-13 02:27

    See: http://www.postgresql.org/docs/9.1/static/arrays.html

    If your non-native driver still does not allow you to pass arrays, then you can:

    • pass a string representation of an array (which your stored procedure can then parse into an array -- see string_to_array)

      CREATE FUNCTION my_method(TEXT) RETURNS VOID AS $$ 
      DECLARE
             ids INT[];
      BEGIN
             ids = string_to_array($1,',');
             ...
      END $$ LANGUAGE plpgsql;
      

      then

      SELECT my_method(:1)
      

      with :1 = '1,2,3,4'

    • rely on Postgres itself to cast from a string to an array

      CREATE FUNCTION my_method(INT[]) RETURNS VOID AS $$ 
             ...
      END $$ LANGUAGE plpgsql;
      

      then

      SELECT my_method('{1,2,3,4}')
      
    • choose not to use bind variables and issue an explicit command string with all parameters spelled out instead (make sure to validate or escape all parameters coming from outside to avoid SQL injection attacks.)

      CREATE FUNCTION my_method(INT[]) RETURNS VOID AS $$ 
             ...
      END $$ LANGUAGE plpgsql;
      

      then

      SELECT my_method(ARRAY [1,2,3,4])
      

提交回复
热议问题