資料庫的查詢與儲存過程

2021-09-26 02:31:49 字數 1647 閱讀 5475

範圍查詢:

是一張表,admin_id 主鍵id

//第一種top查詢

select top 10 * from dbo.administratorsinfo where admin_id not in(select top 10 admin_id from dbo.administratorsinfo)

//第二種max()查詢

select top 10 * from dbo.administratorsinfo where admin_id>(select max(admin_id) from dbo.administratorsinfo where admin_id in(select top 10 admin_id from dbo.administratorsinfo))

//第三種between and查詢

select * from dbo.administratorsinfo where admin_id between 1 and 20

//第四種row_number()over()查詢

select * from (select *,row_number() over(order by admin_id)as number from dbo.administratorsinfo)t where t.number between 10 and 20

儲存過程

//aebs表名,declare 定義,set賦值,while()begin end迴圈,exec儲存

//儲存過程:迴圈

create procedure aebs

as declare @count int;

set @count=10;

while(@count>0)

begin

print @count;

set @count=@count-1

endexec aebs

//儲存過程:分頁查詢

create proc [dbo].[pagelistproc] (@pageindex int,

@pagesize int,@tablename varchar(200),@columnname varchar(500),@orderby varchar(50),@sort varchar(50)

)asdeclare @sql nvarchar(2000);

set @sql='select '+@columnname+' from(select '+@columnname+' ,row_number()over(order by '+@orderby+' )as number from '+@tablename+' )t where t.number between '+cast(((@pageindex-1)*@pagesize)as varchar(200))+' and '+ cast((@pagesize*@pageindex) as varchar(200))

exec(@sql)

//呼叫

//在新建查詢中呼叫pagelistproc 引數(第幾頁,多少條記錄,表名,需查詢的字段,排序的字段,公升序/倒序,)

exec pagelistproc 1,5,'dbo.administratorsinfo','*','admin_id','desc'

sqlserver查詢資料庫死鎖儲存過程 sql

use xx庫 go object storedprocedure dbo sp who lock script date 01 01 2014 14 52 27 set ansi nulls on goset quoted identifier on gocreate procedure dbo ...

查詢資料庫死鎖語句儲存過程

use master go object storedprocedure dbo p lockinfo script date 2016 11 23 16 29 01 set ansi nulls on goset quoted identifier on go 處理死鎖 檢視當前程序,或死鎖程序,...

mysql 查詢資料庫中的儲存過程與函式的語句

mysql 查詢資料庫中的儲存過程與函式的語句,需要的朋友可以參考下。方法一 select name from mysql.proc where db your db name and type procedure 儲存過程 select name from mysql.proc where db ...