欢迎来到天天文库
浏览记录
ID:27522115
大小:52.40 KB
页数:7页
时间:2018-12-03
《sql数据库中的存储过程的参数问题》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、sql数据库中的存储过程的参数问题1、sql数据库中的存储过程的参数问题怎么将sql数据库中的存储过程中的参数既作为输出变量又作为输出变量?[sql]viewplaincopy—dropprocproc_test—gocreateprocdbo.proc_testinint,outintout,inoutintoutputasselectout=in+in_out,—1+2=3in_out=out+l——3+1=4godeclareinpintdeclareout_pintdeclarein_out
2、_pintsetin_p=l;setin_out_p=2execdbo.proc_testin_p,out—pout,in_out_poutputselectin_p,—输入参数out_p,―输出参数in_out_p—输入,输出参数/*(无列名)(无列名)(无列名)134*/2、在存储过程中的参数问题。下面是问题:[sql]viewplaincopycreatetabletttabletest(idintidentity,namevarchar(20),ageint,)goinsertintottt
3、abletestselect’小明’,23unionallselect’小红’,28unionallselect’小军’,27goselectoromtttabletestgocreateprocproctestnamevarchar(20),ageint,idsvarchar(30)asbeginselect氺fromtttabletestwherel=lend—当我传入name参数等于小明,23岁,还有id在(1,3)的时候-我怎么可以弄成可选的参数--比如,name不为空时候select氺fr
4、omtttabletestwherel=landnamelike’小明’—如果name参数为空的时候,ids参数不为空的时候select氺from#tabletestwherel=landidin(l,3)-请问一下,就有参数不为空的时候存储过程中的sq1追加条件,为空的时候就不追加,这样带可选参数的存储过程怎么写,以及怎么调用,请帮小弟写一个实例这种问题,本质上就是根据传入的参数不同,进行不同的查询,也就是where后面的查询条件是动态的。一般有2中处理方式,一种就是写动态语句,但动态语句由于是动
5、态拼接字符串,所以比较难维护,而且如果存储过程需要执行多次,那么每次都需要重新编译,但每次生成的执行计划,应该是比较优化的。但如果拼接字符串部分,只是少量的话,还是可以用动态语句的,下面我的解法就是用动态语句来实现的,结构清晰,易于维护。另一种,就是通过在where语句后面写casewhen来进行判断,这种方法的好处是不用动态拼接语句,但不易于理解,也不易于修改,因为别人不一定能理解你这么写的意思。另一个问题就是性能的问题,因为在原来的公司就用过这种方法,一段时间后,查询非常慢,本来几秒就能出结果,
6、后来几分钟都出不了结果。说实在的,这种方法要求较高的技巧性,也容易出错,不建议使用。下面是我的解法,用了动态语句来实现,但考虑了维护、测试方面的要求:[sql]viewplaincopy--droptablefttabletestereatetable#tabletest(idintidentity,namevarchar(20),ageint,)goinsert!ntotttabletestselect’小明’,23unionallselect’小红’,28unionailselect’小军’,2
7、7goselect氺fromfttabletestgocreateprocproctestnamevarchar(2O)=null,ageint=null,idsvarchar(30)=nul1asdeclaresqlnvarchar(max):setsql=’’;setsql=’select*from#tabletestwherel=l,:setsql=sql+casewhennameisnotnullthen’andnamelike’+quotename(name+’’whenageisnotn
8、ullthen’andage=’+cast(ageasvarchar)whenidsisnotnullthen’andidin(’+ids+’)’else’’end--打印出语句selectsqlas’语句’__执行语句—exec(sql)goexecproctest/*语句select^fromtttabletestwherel=lexecproctest’小明’,23/*语句select氺fromtttabletestwherel=landnamelike’小明%
此文档下载收益归作者所有