您好,欢迎访问一九零五行业门户网

[置顶] 存储过程 Row_number

自己之前一直是使用的通用的存储过程 ,也是封装好的只要传表名 然后 条件 等等 来到新环境 让自己写一个存储过程, 没办法 自己就需要写一个咯 之前写的比较多的是 按 top 来分页 现在公司要求是使用row_number 当然 后者效率还是高一点 。至于索引什么的
自己之前一直是使用的通用的存储过程 ,也是封装好的只要传表名 然后 条件 等等
来到新环境 让自己写一个存储过程, 没办法 自己就需要写一个咯 之前写的比较多的是 按 top 来分页 现在公司要求是使用row_number 当然 后者效率还是高一点 。至于索引什么的 暂时还没有用到 (有什么需求 现学也是可以的)其中也有 with(nolock) 但是会容易造成数据脏读。如果你有用到索引 或者你想看到你的语句查询开销 你可以使用(ctrl+m)键调出来。至于你看到这些占用啥的 懵了? 那就请你移驾 自行查找(我也不会你信吗?)
use [jhmingamedb]goset ansi_nulls ongoset quoted_identifier ongo-- =============================================-- author: yanyunhai-- create date: 2016-04-13-- description: 新手送豆-- =============================================create procedure [dbo].[web_active_buyu_cardlog_page] --创建该存储过程名字(如已经存在 要改的时候就把 create 变成 alter)@state int, @starttime datetime,@endtime datetime,@pagesize int, @pageindex int, @recd int output,--输出参数@totalpeas int output --输出参数asset @recd=0 --赋值为0是避免查询结果为0 时 显示为null set @totalpeas=0declare @recdst int=0,@recdend int=0 --@recdst起始条数set @recdst=@pagesize * (@pageindex-1)+1 -- @recdend 结束条数set @recdend=@pagesize + @recdst-1begin--在对于时间判断时建议 少用 between and 因为 0:00-23:59declare @sumnum1 int,@sumnum2 int,@sumnum3 intselect @sumnum1=count(*) from active_cardlog with(nolock) where [state]=0select @sumnum2=count(*) from active_cardlog with(nolock) where [state]=1select @sumnum3=count(*) from active_cardlog with(nolock) where [state]=1 and uptime>=@starttime and uptime=0begin select row_number() over(order by uptime desc) as rowid,a.id,cardid, cardpwd, cardnum, state, userid, ip, uptime, createtime,b.myname into #tmp from active_cardlog a left join jh_member b on b.idx=a.userid where state=@state --and uptime>=@starttime and uptime<@endtime select @totalpeas=isnull(sum(cardnum),0),@recd=count(1) from #tmp select rowid,cardid, cardpwd, cardnum, state, userid, ip, uptime, createtime,myname,@totalpeas sumnum,@recd sumrowid,@sumnum1 sumnum1,@sumnum2 sumnum2,@sumnum3 sumnum3 from #tmp where rowid between @recdst and @recdend --根据rowid 来确定显示区间 drop table #tmpend else if @state=@starttime and uptime<@endtime select @totalpeas=isnull(sum(cardnum),0),@recd=count(1) from #temp where rowid between @recdst and @recdend --根据rowid 来确定显示区间 drop table #temp endend- -其实上问可以用不用判断也可以解决这个问题 那就是用 where(([state]=2)or([state]=@state))- -有人在使用时 会出现 rowid 报错?自己找找子查询go
——————– with (nolock)—-数据多的时候可以《索引之后的选择》—————–
使用情况: 当我们在下sql command时,在语法中加一段with (nolock)可以改善在线大量查询的环境中数据集被lock的现象藉此改善查询的效能。
不过有一点千万要注意的就是,with (nolock)的sql select有可能会造成数据脏读。
用法:select * from table with(nolock) left join table with(nolock) 表名后面接上 with (nolock)
注意事项:①也就是说当使用nolock时,它允许阅读那些已经修改但是还没有交易完成的数据。因此如果有需要考虑transaction事务数据的实时完整性时,使用with (nolock)就要好好考虑一下
②:with(nolock)的写法非常容易再指定索引。
跨服务器查询语句时 不能用with (nolock) 只能用nolock
同一个服务器查询时 则with (nolock)和nolock都可以用
比如:
select * from [ip].a.dbo.table1 with (nolock) 这样会提示用错误
select * from a.dbo.table1 with (nolock) 这样就可以
当然 你也可以看看 over() 开窗函数
其中的一些东西也是看前一任 程序员写的, 然后就有在园子里面看看介绍。 自己摘了一部分 因为看别人的东西的时候也没有做到用怀疑的态度去看 去分析 所以贴出来 。如果你看到以后有错误 有误区 欢迎指正!!
对你的能力是一次证明, 对我是一次帮助。 虚心求学。
其它类似信息

推荐信息