PostgreSQL没有像SqlServer一样的存储过程。他们的标准也不一样,Sqlserver是T-SQL标准,而PostgreSQL有很多过程语言,其中以 PL/pgSQL最热。由于我也是初学,就不在这里跟大家讨论语法了。进入主题,模拟实现Sqlserver中的存储过程
首先,在建好PostgreSQL中的函数(PostgreSQL中过程用函数来实现),代码如下:
CREATE OR REPLACE FUNCTION "public"."userreg" (p_passport varchar, p_nickname varchar, out p_password varchar, out p_userid integer) RETURNS record AS
$body$
declare
tmpstr varchar(32);
begin
$3 := NULL;
$4 := NULL;
if exists(select 1 from public.t_user where passport = $1) then
begin
$4:=-1;
end;
else
begin
$3 := cast ((round((random()+1) * 9999999)) as varchar(20));
tmpstr := md5($3);
insert into public.t_user (passport,password,nickname) values($1,tmpstr,$2);
$4:=(select userid from public.t_user where passport = $1);
end;
end if;
EXCEPTION
WHEN others THEN
$4:=-2;
$3:=-2;
en d;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
以上示例代码,模拟SqlServer,所以故意加入了两个In参数和两OUT参数。
下面是在.net下的调用代码,就跟SQL中的存储过程调用一样。ODBC方式貌似不行。
view plaincopy to clipboardprint?
public void UserRegiste(T_User user)
{
NpgsqlConnection conn = new NpgsqlConnection();
conn.ConnectionString = "Server=127.0.0.1;Port=5432;Userid=postgres;database=jtnet;password=123456;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=60;SslMode=Disable";
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "userreg";
NpgsqlParameter p1 = new NpgsqlParameter("p_passport", DbType.String, 50);
NpgsqlParameter p2 = new NpgsqlParameter("p_nickname", DbType.String, 50);
NpgsqlParameter p3 = new NpgsqlParameter("p_nickname", DbType.Int32);
NpgsqlParameter p4 = new NpgsqlParameter("p_userid", DbType.Int32);
p1.Value = user.Passport;
p2.Value = user.UserNickName;
p3.Direction = ParameterDirection.Output;
p4.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
conn.Open();
NpgsqlDataReader dr = cmd.ExecuteReader();
dr.Read();
//此处可以写上显示代码
}
public void UserRegiste(T_User user)
{
NpgsqlConnection conn = new NpgsqlConnection();
conn.ConnectionString = "Server=127.0.0.1;Port=5432;Userid=postgres;database=jtnet;password=123456;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=60;SslMode=Disable";
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "userreg";
NpgsqlParameter p1 = new NpgsqlParameter("p_passport", DbType.String, 50);
NpgsqlParameter p2 = new NpgsqlParameter("p_nickname", DbType.String, 50);
NpgsqlParameter p3 = new NpgsqlParameter("p_nickname", DbType.Int32);
NpgsqlParameter p4 = new NpgsqlParameter("p_userid", DbType.Int32);
p1.Value = user.Passport;
p2.Value = user.UserNickName;
p3.Direction = ParameterDirection.Output;
p4.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
conn.Open();
NpgsqlDataReader dr = cmd.ExecuteReader();
dr.Read();
//此处可以写上显示代码
}