In postgresql- what is the equivalent of stored procedure with table valued paramater(MSSQL)?
Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.
Create a user-defined table type that corresponds to the table that you want to populate. Pass the user-defined table to the stored procedure as a parameter. Inside the stored procedure, select the data from the passed parameter and insert it into the table that you want to populate.
@HamidKhan -Is your development language Java or C #?
CREATE TABLE public.employee (
emp_id INTEGER NOT NULL,
emp_nm VARCHAR(40),
first_in_time TIMESTAMP WITH TIME ZONE DEFAULT clock_timestamp(),
last_chg_time TIMESTAMP WITH TIME ZONE DEFAULT clock_timestamp(),
CONSTRAINT employee_pkey PRIMARY KEY(emp_id)
)
WITH (oids = false);
CREATE TYPE public.employee_udt AS (
emp_id INTEGER,
emp_nm VARCHAR(40)
);
--c# code 1
public class EmployeeUdt
{
[PgName("emp_id")]
public int EmpId { get; set; }
[PgName("emp_nm")]
public string EmpNm { get; set; }
}
--c# code 2
List<EmployeeUdt> lst_param = new List<EmployeeUdt>();
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
lst_param.Add(
new EmployeeUdt
{
EmpId = Convert.ToInt32(this.dataGridView1[0, i].Value),
EmpNm = this.dataGridView1[1, i].Value.ToString()
}
);
}
var _param = new[] {
new NpgsqlParameter
{
ParameterName="p_employee",
NpgsqlDbType = NpgsqlDbType.Composite,
SpecificType = typeof(EmployeeUdt[]),
NpgsqlValue = lst_param
}
};
SqlHelper.ExecuteNonQuery<EmployeeUdt>(this.connstring, "usp_set_emp", _param);
PostgreSQL has no table variables. The most similar type is a composite array
:
create type foo_typ as (a int, b int);
do $$
declare
f foo_typ[];
r foo_typ;
begin
f := ARRAY(SELECT row(10, 20)::foo_typ from generate_series(1,10));
for r in select * from unnest(f) g(v)
loop
raise notice 'a:%, b:%', r.a, r.b;
end loop;
end;
$$;
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
NOTICE: a:10, b:20
DO
Writing stored procedures in PostgreSQL, you are practically starting from zero coming from T-SQL in terms of syntax familiarity. The PostgreSQL syntax is similar to Oracle and DB2 - but very different to SQL Server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With