在postgresql中 - 与表值参数(MSSQL)相同的存储过程是什么?
答案 0 :(得分:2)
@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)
);
- 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);
答案 1 :(得分:1)
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
在PostgreSQL中编写存储过程,在语法熟悉度方面,你几乎从零开始来自T-SQL。 PostgreSQL语法类似于Oracle和DB2 - 但与SQL Server非常不同。
答案 2 :(得分:0)
您可以将JSON数组用作jsonb_to_recordset()的输入。这是我找到的最简单的方法。只需创建一个充满对象的JSON数组即可。我不确定它是否与SQL Server中的真实表值参数一样快,但仅需要一个请求即可插入或更新许多值。
这是我使用Dapper的代码库中的示例。注意如何将数组转换为JSON字符串。然后,它使用jsonb_to_recordset将其转换为表格。
await cnTeam.ExecuteAsync(@"
update enterprisecontact
set
primarysource_contactid = x.primarysource_contactid,
primarysource_contact_recordtype = x.primarysource_contact_recordtype,
primarysource_accountid = x.primarysource_accountid,
primarysource_accountname = x.primarysource_accountname,
primarysource_createdate = x.primarysource_createdate,
primarysource_updatedate = x.primarysource_updatedate
from (
select * from jsonb_to_recordset(:jsoninput::jsonb) as y(
emailaddress text,
primarysource_contactid text,
primarysource_contact_recordtype text,
primarysource_accountid text,
primarysource_accountname text,
primarysource_createdate timestamp with time zone,
primarysource_updatedate timestamp with time zone
)
) x
where enterprisecontact.emailaddress = x.emailaddress
", new { jsoninput = JsonConvert.SerializeObject(chunk) });