1. 使用%type
sql
在许多状况下,pl/sql中变量用来存储在数据库表中的数据,这时候,咱们就要求变量应该与数据库表中的字段拥有相同的数据类型,例如:t_user表中的user_name字段类型为varchar2(20),这时候咱们能够申明一个变量:数据库
declare v_userNme varchar2(20);
可是,若是有一天咱们表中字段的类型发生了改变,例如将t_user表中的user_name改成varchar2(25),那么就会致使全部用到该字段的存储过程的pl/sql代码都必须进行修改,这是一项费时耗力的事情。code
如今,咱们可使用 %type 属性来动态定义变量的类型,以下:it
declare v_userName t_user.user_name%type;
经过使用 %type ,user_name变量的类型与t_user表中user_name的字段类型动态的绑定了。io
2. 使用%rowtypefunction
假设咱们须要定义以下几个变量,class
declare v_userName t_user.user_name%type; v_userGender t_user.gender%type; v_userAge t_user.age%type;
仔细查看下他们之间的关系,咱们就会发现他们同属于一张表中的不一样字段,若是为这些变量声明一个记录类型,那么就能够将他们做为一个单元来进行处理。登录
declare v_userTable t_user%rowtype;
这里变量
v_userName 就能够用v_userTable.user_name来代替了。select
具体例子
create or replace procedure sp_checkAccount(i_account in t_account.acc_id%type, --登陆帐号 o_errNumber out integer) --错误代码 is --帐号表数据 l_rtyAccount t_account%rowtype; --区域表数据 l_zoneData t_zone%rowtype; --经销商数据 l_fchsData t_franchiser%rowtype; --门店数据 l_storeData t_store%rowtype; --销售员数据 l_selrData t_seller%rowtype; --异常 l_exception exception; --错误消息 l_error_msg varchar2(100); /********** *creater: lxl *craeateTime: 2015-07-09 *function: checkAccount **********/ /********* o_errorNumber 0:帐号可用 1:异常 11:帐号不存在 12:帐号无效 13:区域状态无效 14:经销商状态无效 15:门店状态无效 *********/ /********* l_error_msg 0:帐号可用时返回空 11:帐号不存在 12:登陆帐号无效 13:区域状态不可用 14:经销商状态不可用 15:门店状态不可用 *********/ begin --初始化 o_errNumber := PKG_CONSTANTS.C_RTN_SUCCESS; l_error_msg := ''; --判断帐号是否存在且启用 begin select * into l_rtyAccount from t_account where acc_id = trim(i_Account); exception when no_data_found then --帐号不存在 o_errNumber := 11; l_error_msg := '帐号不存在'; raise l_exception; end; --判断帐号是否已启用 if l_rtyAccount.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 12; l_error_msg := '帐号无效'; raise l_exception; end if; --备注:若是是总部登陆进来,上述检查就能够知足要求 --下面只须要检查角色为经销商、门店和销售员 --经销商 if l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_FCHS then begin --1.根据角色代码去经销商表取数据 select * into l_fchsData from t_franchiser tf where tf.fchs_id = trim(l_rtyAccount.Role_Id); --2.判断经销商状态是否可用 if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 12; l_error_msg := '经销商状态不可用'; raise l_exception; else --查询区域全部数据 begin select * into l_zoneData from t_zone tz where tz.zone_id = l_fchsData.Zone_Id; --判断区域状态是否可用 if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 13; l_error_msg := '区域状态不可用'; raise l_exception; else o_errNumber := 0; l_error_msg := ''; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '经销商查询区域数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '经销商查询经销商数据出错'; end; --门店 elsif l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_STORE then begin --1.根据角色代码去门店表取数据 select * into l_storeData from t_store ts where ts.store_id = l_rtyAccount.Role_Id; --2.判断门店状态是否可用 if l_storeData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 12; l_error_msg := '门店状态不可用'; raise l_exception; else begin --3.查询经销商数据 select * into l_fchsData from t_franchiser tf where tf.fchs_id = l_storeData.Fchs_Id; --判断经销商状态 if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 14; l_error_msg := '经销商状态不可用'; raise l_exception; else --查询区域全部数据 begin select * into l_zoneData from t_zone tz where tz.zone_id = l_fchsData.Zone_Id; --判断区域状态是否可用 if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 13; l_error_msg := '区域状态不可用'; raise l_exception; else o_errNumber := 0; l_error_msg := ''; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '门店查询区域数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '门店查询经销商数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '门店查询门店数据出错'; end; --销售员 elsif l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_SELLER then begin --1.根据销售员代码去门店表取数据 select * into l_selrData from t_seller tsr where tsr.selr_id = l_rtyAccount.Role_Id; --2.判断销售员状态是否可用 if l_selrData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 12; l_error_msg := '销售员状态不可用'; raise l_exception; else begin --查询门店数据 --1.根据角色代码去门店表取数据 select * into l_storeData from t_store ts where ts.store_id = l_selrData.Store_Id; --2.判断门店状态是否可用 if l_storeData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 15; l_error_msg := '门店状态不可用'; raise l_exception; else begin --3.查询经销商数据 select * into l_fchsData from t_franchiser tf where tf.fchs_id = l_storeData.Fchs_Id; --判断经销商状态 if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 14; l_error_msg := '经销商状态不可用'; raise l_exception; else --查询区域全部数据 begin select * into l_zoneData from t_zone tz where tz.zone_id = l_fchsData.Zone_Id; --判断区域状态是否可用 if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then o_errNumber := 13; l_error_msg := '区域状态不可用'; raise l_exception; else o_errNumber := 0; l_error_msg := ''; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '销售员查询区域数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '销售员查询经销商数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '销售员查询门店数据出错'; end; end if; exception when no_data_found then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; l_error_msg := '销售员查询销售员数据出错'; end; end if; --end经销商 exception when l_exception then pkg_utility.sp_writeLog(i_log_type => 'ERROR', i_acc_id => i_account, i_sp_name => 'sp_checkAccount', i_log_desc => l_error_msg); when others then o_errNumber := PKG_CONSTANTS.C_RTN_FAILURE; pkg_utility.sp_writeLog(i_log_type => 'ERROR', i_acc_id => i_account, i_sp_name => 'sp_checkAccount', i_log_desc => l_error_msg); end sp_checkAccount;