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();
//此處可以寫上顯示代碼
}