create proc [名字]
{
@参数 数据类型,
@参数 数据类型
output[输入]
}
as
begin
select insert update (sql)
end
--基本语句快
–以上是语句库
–先看看不带参数的吧 他跟方法一样 可以带参数也可以不带参数(当然我没用过几次不带参数的)
–获取一个表吧这种都感觉像视图了
if(select * from sysobjects where name ='proc_table') drop proc proc_table go create proc proc_table as select * from users where s_id='' go exec proc_table
–带参数的吧–就看看登录的sql吧
if(select * from sysobjects where name ='p_log') drop proc p_log go create proc p_log @acctount varchar(50), @accountpwd varchar(50) as begin select count(*) from users where u_loginname=@acctount and u_password=@accountpwd; end exec p_log'1','123456'
--c#orjava 调了之后直接判断有没有值即可
--这是返回单行单列。需要用返回单行单列的放方法去接收
–再看看输出参数的存储过程
在java中我们需要调用带参的方法时需要传参给形参,列入比较两个大小的方法 int compare( int first ,int second),比较10 和20的大小,则调用形式:tempcompare(10,20),方法compare返回值赋值给变量为tmp.
存储过程中也有与很像是,有两种类型的参数的参数
~输入参数:调用是像存储过程传实参,用来向proc传值
~输出参数: 同java 如果希望参数可以带出方法,则可以使用输出参数值带出方法,则可以输出参数,通过定义参数 “output”标记 ,表明该参数是输出参数 ,执行存储过程后吧 返回值存放在输出中-
可以给其他t-sql 语句访问,
create proc [dbo].[p_getconsumeorderpaged]
@pagesize int,
@pageindex int,
@count int output,
@mc_cradid varchar(20),
@mc_mobile varchar(20),
@begindate varchar(20),
@enddate varchar(20),
@co_ordertype int,
@s_id int
as
begin
select top(@pagesize) * from consumeorders m inner join memcards a on m.mc_id=a.mc_id inner join categoryitems b on m.co_ordertype=b.ci_id
where a.s_id=@s_id and m.co_id not in(
select top(@pagesize*(@pageindex-1)) m.co_id from consumeorders m inner join memcards a on m.mc_id=a.mc_id inner join categoryitems b on m.co_ordertype=b.ci_id
and a.s_id=@s_id and b.c_category='co_ordertype' and ((a.mc_cardid=@mc_cradid or a.mc_mobile=@mc_mobile) or (@mc_cradid='' and @mc_mobile='')) and ((@begindate='' or @enddate='') or (m.co_createtime between @begindate and @enddate)) and ((@co_ordertype=0) or (m.co_ordertype=@co_ordertype))
)
and b.c_category='co_ordertype' and ((a.mc_cardid=@mc_cradid or a.mc_mobile=@mc_mobile) or (@mc_cradid='' and @mc_mobile='')) and ((@begindate='' or @enddate='') or (m.co_createtime between @begindate and @enddate)) and ((@co_ordertype=0) or (m.co_ordertype=@co_ordertype))
select @count=count(*) from consumeorders m inner join memcards a on m.mc_id=a.mc_id inner join categoryitems b on m.co_ordertype=b.ci_id where a.s_id=@s_id and b.c_category='co_ordertype' and ((a.mc_cardid=@mc_cradid or a.mc_mobile=@mc_mobile) or (@mc_cradid='' and @mc_mobile='')) and ((@begindate='' or @enddate='') or (m.co_createtime between @begindate and @enddate)) and ((@co_ordertype=0) or (m.co_ordertype=@co_ordertype))
end
go
— 创建参数带有默认值的proc
在调proc时,有些参数变化很少,这时,可以给这些参数一个默认值,即使调用时不输入值,也会在存储过程中使用默认值,在很大程度上方便调。
if(select * from sysobjects where name ='proc_insertstu') drop proc proc_insertstu go create pro proc_insertstu @stuname varchar(20), @stusex char(2)='男', @classid int =2 as begin insert into stuinfoo(stuname,stusexmclassid) values(@stuname ,@stusex,@classid ) end go exec proc_insertstu'唐胜' exec proc_insertstu'‘‘zhubajie’@classid=1
`调用时可以传值也可以不传