Ham*_*han 9 postgresql table-valued-parameters
在 postgresql 中 - 什么是具有表值参数(MSSQL)的存储过程的等价物?
PostgreSQL 没有表变量。最相似的类型是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
Run Code Online (Sandbox Code Playgroud)
在 PostgreSQL 中编写存储过程,在语法熟悉度方面,您实际上是从 T-SQL 零开始的。PostgreSQL 语法与 Oracle 和 DB2 类似,但与 SQL Server 非常不同。
@HamidKhan - 你的开发语言是Java还是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)
);
Run Code Online (Sandbox Code Playgroud)
--c# 代码 1
public class EmployeeUdt
{
[PgName("emp_id")]
public int EmpId { get; set; }
[PgName("emp_nm")]
public string EmpNm { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
--c# 代码 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5606 次 |
| 最近记录: |