存储过程的参数和过程或函数的参数类似,存储过程的变量类似于过程或者函数的局部变量。
注意事项:
1, 存储过程参数不带取值范围,in表示传入,out表示输出
2, 变量带取值范围,后面接分号
3, 在判断语句前最好先用count(*)函数判断是否存在该条操作记录
4, 用select 。。。into。。。给变量赋值
5, 在代码中抛异常用 raise+异常名
注:易犯错误
--以上语句均在"SQL*Plus 工作单"上运行;
1.每一个小单元的语句后要加';'号;
2.不能将字符串赋值的单引号写成双引号;
如: err:='更新成功!'; 不能写成 err:="更新成功!";
以上错误系统将提示:"警告: 创建的过程带有编译错误。"
3.存储过程传递与赋值的参数名称,个数,类型(字段类型,返回的类型(in/out))要与调用的存储过程以及该存储过程
所访问的表中相应的字段类型严格对应.
还有一些约定的写法也需遵守.
如:其中的" M_KSCJ "对应表中的" KSCJ "字段, 应写成(M_KSCJ in integer)不能写成( M_KSCJ in number(3));
"M_SN"对应表中的 "SN" 字段,应写成 (M_SN in char) 不能写成 (M_SN in Varchar2)
4.在c#中进行调用时,还要注意它的输入/输出类型,如上例中的" out integer res " 为输出类型,应将其
OracleParameter[] parm = new OracleParameter[1];
parm[0] = new OracleParameter("RES", OracleType.Int16 );
parm[0].Direction = ParameterDirection.Output ; --将其设为输出类型;
具体调用方法将在稍后进行介绍;
5.定义存储过程时,其参数名称最好不要与字段名称同名(不区分大小写);
如上面的存储过程建议不要写成:
WRITE_SRC( SN in char , XM in varchar2,KSCJ in integer,
KSRQ in Date,RES out integer,ERR out varchar2)
存储过程如下:
1 create or replace procedure PEOPLE_INSERT( 2 senderid in NVARCHAR2, peoplename in NVARCHAR2, 3 idnumber in NVARCHAR2, rank_worktype in NVARCHAR2, 4 dept in NVARCHAR2, mainworkplace in NVARCHAR2, 5 birthday in DATE, gender in NVARCHAR2, 6 bloodtype in NVARCHAR2, allergy in NVARCHAR2, 7 ismarried in NVARCHAR2, phone in NVARCHAR2, 8 tel in NVARCHAR2, address in NVARCHAR2, 9 certificate in NVARCHAR2, valuedate in DATE) is 10 begin 11 insert into PEOPLE 12 (SENDER_ID,PEOPLE_NAME,ID_NUMBER, 13 RANK_WORKTYPE,DEPT,MAINWORKPLACE,BIRTHDAY , 14 GENDER,BLOOD_TYPE,ALLERGY,ISMARRIED, 15 PHONE , TEL, ADDRESS , CERTIFICATE , VALUEDATE ) 16 values 17 (senderid,peoplename,idnumber,rank_worktype,dept,mainworkplace,birthday,gender, 18 bloodtype,allergy,ismarried,phone,tel,address,certificate,valuedate); 19 end PEOPLE_INSERT;
Oracle调用(通过PLSQL Developer):
1 begin 2 people_insert('hsy06999','修','11111111111','无','无', 3 '全矿井',to_date('1900-01-01','yyyy-mm-dd'),'男','O','','','','','','',''); 4 end;
C#调用类,这里OracleParameter的时间类型采用OracleType.DateTime,将.Net的DateTime类型直接传入,由于自己的疏忽,将一个OracleParameter设置成了OracleType.String类型,这样将该参数的Value值赋值为DateTime变量是产生“月的日必须在1和当月最大天数之间”的错误,当然也可以转换到OracleDateTime类型(见注释):
1 // File: People.cs 2 // Author: yhlx 3 // Created: 2012年9月18日 8:57:44 4 // Purpose: Definition of Class People 5 6 using System; 7 using Transplant; 8 using System.Data.OracleClient; 9 using System.Data; 10 11 public class People 12 { 13 public string senderId; 14 public string peopleName; 15 public string idNumber; 16 public string rankWorktype; 17 public string dept; 18 public string mainworkplace; 19 public DateTime birthday; 20 public string gender; 21 public string bloodType; 22 public string allergy; 23 public string ismarried; 24 public string phone; 25 public string tel; 26 public string address; 27 public string certificate; 28 public DateTime valuedate; 29 30 public void Fetch(string row) 31 { 32 string[] cel= row.Split(';'); 33 senderId = Convert.ToString(cel[1]); 34 peopleName = Convert.ToString(cel[2]); 35 idNumber= Convert.ToString(cel[3]); 36 rankWorktype= Convert.ToString(cel[4]); 37 dept= Convert.ToString(cel[5]); 38 mainworkplace= Convert.ToString(cel[6]); 39 birthday= Convert.ToDateTime(cel[7]); 40 string bir = birthday.ToString(); 41 gender= Convert.ToString(cel[8]); 42 bloodType= Convert.ToString(cel[9]); 43 allergy= Convert.ToString(cel[10]); 44 ismarried = Convert.ToString(cel[11]); 45 phone= Convert.ToString(cel[12]); 46 tel= Convert.ToString(cel[13]); 47 address= Convert.ToString(cel[14]); 48 certificate= Convert.ToString(cel[15]); 49 if (cel[16]!="") 50 { 51 valuedate = Convert.ToDateTime(cel[16]); 52 } 53 54 } 55 public void insert() 56 { 57 OracleParameter[] parms ={ 58 new OracleParameter("senderid",OracleType.NVarChar), 59 new OracleParameter("peoplename",OracleType.NVarChar), 60 new OracleParameter("idnumber",OracleType.NVarChar), 61 new OracleParameter("rank_worktype",OracleType.NVarChar), 62 new OracleParameter("dept",OracleType.NVarChar), 63 new OracleParameter("mainworkplace",OracleType.NVarChar), 64 new OracleParameter("birthday",OracleType.DateTime), 65 new OracleParameter("gender",OracleType.NVarChar), 66 new OracleParameter("bloodtype",OracleType.NVarChar), 67 new OracleParameter("allergy",OracleType.NVarChar), 68 new OracleParameter("ismarried",OracleType.NVarChar), 69 new OracleParameter("phone",OracleType.NVarChar), 70 new OracleParameter("tel",OracleType.NVarChar), 71 new OracleParameter("address",OracleType.NVarChar), 72 new OracleParameter("certificate",OracleType.NVarChar), 73 new OracleParameter("valuedate",OracleType.DateTime) 74 }; 75 parms[0].Value = senderId; 76 parms[1].Value = peopleName; 77 parms[2].Value = idNumber; 78 parms[3].Value = rankWorktype; 79 parms[4].Value = dept; 80 parms[5].Value = mainworkplace; 81 parms[6].Value = birthday; 82 //parms[6].Value = OracleDateTime.Parse(birthday.ToShortDateString()); 83 parms[7].Value = gender; 84 parms[8].Value = bloodType; 85 parms[9].Value = allergy; 86 parms[10].Value = ismarried; 87 parms[11].Value = phone; 88 parms[12].Value = tel; 89 parms[13].Value = address; 90 parms[14].Value = certificate; 91 parms[15].Value = valuedate; 92 OracleHelper.ExecuteNonQuery(OracleHelper.connectionString,CommandType.StoredProcedure,"PEOPLE_INSERT",parms ); 93 } 94 }
参考文献:
http://www.cnblogs.com/chuncn/archive/2009/01/29/1381291.html
http://www.cnblogs.com/furenjun/archive/2007/06/18/orcaleprocedure.html重点参考