[置顶] 存储过程 Row_number
自己之前一直是使用的通用的存储过程 ,也是封装好的只要传表名 然后 条件 等等 来到新环境 让自己写一个存储过程, 没办法 自己就需要写一个咯 之前写的比较多的是 按 top 来分页 现在公司要求是使用Row_number 当然 后者效率还是高一点 。至于索引什么的
自己之前一直是使用的通用的存储过程 ,也是封装好的只要传表名 然后 条件 等等
来到新环境 让自己写一个存储过程, 没办法 自己就需要写一个咯 之前写的比较多的是 按 top 来分页 现在公司要求是使用Row_number 当然 后者效率还是高一点 。至于索引什么的 暂时还没有用到 (有什么需求 现学也是可以的)其中也有 with(nolock) 但是会容易造成数据脏读。如果你有用到索引 或者你想看到你的语句查询开销 你可以使用(ctrl+M)键调出来。至于你看到这些占用啥的 懵了? 那就请你移驾 自行查找(我也不会你信吗?)
<code class=" hljs sql"> USE [JHMinGameDB] GO <span class="hljs-operator"><span class="hljs-keyword">SET</span> ANSI_NULLS <span class="hljs-keyword">ON</span> <span class="hljs-keyword">GO</span> <span class="hljs-keyword">SET</span> QUOTED_IDENTIFIER <span class="hljs-keyword">ON</span> <span class="hljs-keyword">GO</span> -- ============================================= -- Author: yanyunhai -- <span class="hljs-keyword">Create</span> <span class="hljs-keyword">date</span>: <span class="hljs-number">2016</span>-<span class="hljs-number">04</span>-<span class="hljs-number">13</span> -- Description: 新手送豆 -- ============================================= <span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> [dbo].[Web_Active_Buyu_CardLog_page] --创建该存储过程名字(如已经存在 要改的时候就把 <span class="hljs-keyword">create</span> 变成 <span class="hljs-keyword">Alter</span>) @State <span class="hljs-keyword">int</span>, @startTime datetime, @endTime datetime, @pageSize <span class="hljs-keyword">int</span>, @pageIndex <span class="hljs-keyword">int</span>, @recd <span class="hljs-keyword">int</span> <span class="hljs-keyword">output</span>,--输出参数 @totalpeas <span class="hljs-keyword">int</span> <span class="hljs-keyword">output</span> --输出参数 <span class="hljs-keyword">AS</span> <span class="hljs-keyword">set</span> @recd=<span class="hljs-number">0</span> --赋值为<span class="hljs-number">0</span>是避免查询结果为<span class="hljs-number">0</span> 时 显示为<span class="hljs-keyword">null</span> <span class="hljs-keyword">set</span> @totalpeas=<span class="hljs-number">0</span> <span class="hljs-keyword">Declare</span> @recdst <span class="hljs-keyword">int</span>=<span class="hljs-number">0</span>,@recdend <span class="hljs-keyword">int</span>=<span class="hljs-number">0</span> --@recdst起始条数 <span class="hljs-keyword">Set</span> @recdst=@pageSize * (@pageIndex-<span class="hljs-number">1</span>)+<span class="hljs-number">1</span> -- @recdend 结束条数 <span class="hljs-keyword">Set</span> @recdend=@pageSize + @recdst-<span class="hljs-number">1</span> <span class="hljs-keyword">BEGIN</span> --在对于时间判断时建议 少用 between <span class="hljs-keyword">and</span> 因为 <span class="hljs-number">0</span>:<span class="hljs-number">00</span>-<span class="hljs-number">23</span>:<span class="hljs-number">59</span> <span class="hljs-keyword">declare</span> @SumNum1 <span class="hljs-keyword">int</span>,@SumNum2 <span class="hljs-keyword">int</span>,@SumNum3 <span class="hljs-keyword">int</span> <span class="hljs-keyword">select</span> @SumNum1=<span class="hljs-aggregate">COUNT</span>(*) <span class="hljs-keyword">from</span> Active_CardLog <span class="hljs-keyword">with</span>(nolock) <span class="hljs-keyword">where</span> [State]=<span class="hljs-number">0</span> <span class="hljs-keyword">select</span> @SumNum2=<span class="hljs-aggregate">COUNT</span>(*) <span class="hljs-keyword">from</span> Active_CardLog <span class="hljs-keyword">with</span>(nolock) <span class="hljs-keyword">where</span> [State]=<span class="hljs-number">1</span> <span class="hljs-keyword">select</span> @SumNum3=<span class="hljs-aggregate">COUNT</span>(*) <span class="hljs-keyword">from</span> Active_CardLog <span class="hljs-keyword">with</span>(nolock) <span class="hljs-keyword">where</span> [State]=<span class="hljs-number">1</span> <span class="hljs-keyword">and</span> UpTime>=@startTime <span class="hljs-keyword">and</span> UpTime<@endTime <span class="hljs-keyword">if</span> @State>=<span class="hljs-number">0</span> <span class="hljs-keyword">begin</span> <span class="hljs-keyword">select</span> row_number() over(<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> UpTime <span class="hljs-keyword">desc</span>) <span class="hljs-keyword">as</span> rowid,a.id,CardID, CardPwd, CardNum, State, UserID, IP, UpTime, CreateTime,b.myname <span class="hljs-keyword">into</span> #tmp <span class="hljs-keyword">from</span> Active_CardLog a <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> JH_member b <span class="hljs-keyword">on</span> b.idx=a.UserID <span class="hljs-keyword">where</span> State=@State --<span class="hljs-keyword">and</span> UpTime>=@startTime <span class="hljs-keyword">and</span> UpTime<@endTime <span class="hljs-keyword">select</span> @totalpeas=isnull(<span class="hljs-aggregate">SUM</span>(CardNum),<span class="hljs-number">0</span>),@recd=<span class="hljs-aggregate">count</span>(<span class="hljs-number">1</span>) <span class="hljs-keyword">from</span> #tmp <span class="hljs-keyword">select</span> rowid,CardID, CardPwd, CardNum, State, UserID, IP, UpTime, CreateTime,myname,@totalpeas sumnum,@recd sumRowID,@SumNum1 SumNum1,@SumNum2 SumNum2,@SumNum3 SumNum3 <span class="hljs-keyword">from</span> #tmp <span class="hljs-keyword">where</span> rowid between @recdst <span class="hljs-keyword">and</span> @recdend --根据rowid 来确定显示区间 <span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> #tmp <span class="hljs-keyword">end</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @State<<span class="hljs-number">0</span> <span class="hljs-keyword">begin</span> <span class="hljs-keyword">select</span> row_number() over(<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> UpTime <span class="hljs-keyword">desc</span>) <span class="hljs-keyword">as</span> rowid,a.id,CardID, CardPwd, CardNum, State, UserID, IP, UpTime, CreateTime,b.myname <span class="hljs-keyword">into</span> #temp <span class="hljs-keyword">from</span> Active_CardLog a <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> JH_member b <span class="hljs-keyword">on</span> b.idx=a.UserID <span class="hljs-keyword">where</span> UpTime>=@startTime <span class="hljs-keyword">and</span> UpTime<@endTime <span class="hljs-keyword">select</span> @totalpeas=isnull(<span class="hljs-aggregate">SUM</span>(CardNum),<span class="hljs-number">0</span>),@recd=<span class="hljs-aggregate">count</span>(<span class="hljs-number">1</span>) <span class="hljs-keyword">from</span> #temp <span class="hljs-keyword">where</span> rowid between @recdst <span class="hljs-keyword">and</span> @recdend --根据rowid 来确定显示区间 <span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> #temp <span class="hljs-keyword">end</span> <span class="hljs-keyword">END</span> - -其实上问可以用不用判断也可以解决这个问题 那就是用 <span class="hljs-keyword">where</span>(([State]=<span class="hljs-number">2</span>)<span class="hljs-keyword">or</span>([State]=@state)) - -有人在使用时 会出现 Rowid 报错?自己找找子查询 <span class="hljs-keyword">GO</span> </span></code>
——————– 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() 开窗函数
其中的一些东西也是看前一任 程序员写的, 然后就有在园子里面看看介绍。 自己摘了一部分 因为看别人的东西的时候也没有做到用怀疑的态度去看 去分析 所以贴出来 。如果你看到以后有错误 有误区 欢迎指正!!
对你的能力是一次证明, 对我是一次帮助。 虚心求学。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

What is cache? A cache (pronounced ka·shay) is a specialized, high-speed hardware or software component used to store frequently requested data and instructions, which in turn can be used to load websites, applications, services, and other aspects of the system faster part. Caching makes the most frequently accessed data readily available. Cache files are not the same as cache memory. Cache files refer to frequently needed files such as PNGs, icons, logos, shaders, etc., which may be required by multiple programs. These files are stored in your physical drive space and are usually hidden. Cache memory, on the other hand, is a type of memory that is faster than main memory and/or RAM. It greatly reduces data access time since it is closer to the CPU and faster compared to RAM

There are users on Douyin who shoot a lot of video works. Once there are too many video works, all kinds of excellent video works will be buried. The pinning function is very useful. So how do we pin our own video works? Next, the editor will bring you a graphic tutorial on how to pin your own videos on Douyin. Users who don’t know how to pin videos to the top should take a look. Tiktok usage tutorial: How to pin your own videos on Tiktok 1. First, we open Tiktok and click on me as shown in the picture in the lower right corner of the main interface. 2. Then after we enter the personal interface, find the video work you want to pin to the top, and click to play it. 3. Then in the video interface, click on the three-dot option in the lower right corner as shown in the picture. 4. Finally, we click on the top button in the new pop-up window to return to the personal world.

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

How to correctly use sessionStorage to store sensitive information requires specific code examples. Whether in web development or mobile application development, we often need to store and process sensitive information, such as user login credentials, ID numbers, etc. In front-end development, using sessionStorage is a common storage solution. However, since sessionStorage is browser-based storage, some security issues need to be paid attention to to ensure that the stored sensitive information is not maliciously accessed and used.

How do PHP and swoole achieve efficient data caching and storage? Overview: In web application development, data caching and storage are a very important part. PHP and swoole provide an efficient method to cache and store data. This article will introduce how to use PHP and swoole to achieve efficient data caching and storage, and give corresponding code examples. 1. Introduction to swoole: swoole is a high-performance asynchronous network communication engine developed for PHP language. It can

This article is reprinted from the WeChat public account "Living in the Information Age". The author lives in the information age. To reprint this article, please contact the Living in the Information Age public account. For students who are familiar with database operations, writing beautiful SQL statements and finding ways to find the data they need from the database is a routine operation. For students who are familiar with machine learning, it is also a routine operation to obtain data, preprocess the data, build a model, determine the training set and test set, and use the trained model to make a series of predictions about the future. So, can we combine the two technologies? We see that data is stored in the database, and predictions need to be based on past data. If we query future data through the existing data in the database, then it is
