快快分页方法代码(sql百万级数据量分页代码卡塔尔

综合互连网资料收拾]

查询多个数据库中具有表字段属性的sql语句

@querystr nvarchar(300卡塔尔(قطر‎,–表名、视图名、查询语句@pagesize
int=10,–每页的分寸(行数State of Qatar@pagecurrent int=1,–要出示的页@fdshow nvarchar
(100卡塔尔=”,–要突显的字段列表,若是查询结果有标志字段,必要指

1.到手具备数据库名:
   (1)、Select Name FROM
Master..SysDatabases order by Name
2.**获得具备表名:

 

定此值,且不分包标记字段@fdorder nvarchar
(100State of Qatar=”,–排序字段列表@wherestr nvarchar (200卡塔尔国=”, –内容是’ id=3 and
model_no like ‘%24%’

**   (1)、Select Name FROM SysObjects Where
XType=’U’ orDER BY Name
           XType=’U’:表示全数客商表;
           XType=’S’:表示具有系统表;

 

and ‘@rscount int=0 output asset @fdshow=’ ‘+@fdshow+’ ‘set @fdorder= ‘
‘+@fdorder+’ ‘set @wherestr= ‘ ‘+@wherestr+’ ‘

   (2)、SELECT name FROM sysobjects WHERE type = ‘U’ AND sysstat =
’83’

 

declare @fdname nvarchar(250卡塔尔(قطر‎–表中的主键或表、有时表中的标志列名,@id1
varchar(20State of Qatar,@id2 varchar(20卡塔尔–开首和结束的记录号,@obj_id int
–对象id,@temp nvarchar(300State of Qatar –不经常语句,@strparam nvarchar(100)–一时参数

           注意:经常情状只要求type =
‘U’,但不经常会有种类表混在个中(不知底怎么原因),加上后边一句后就能够去除那些类别表了

1.收获具有客商名:
SELECT name FROM Sysusers where status=’2′ and islogin=’1′
islogin=’1’意味着帐户 islogin=’0’表示剧中人物 status=’2’表示客商帐户
status=’0’表示糸统帐户 2.获得具备数据库名:
SELECT Name FROM Master..SysDatabases O凯雷德DEPAJERO BY Name 3.获得具备表名
SELECT Name FROM DatabaseName..SysObjects Where XType=’U’ OEnclaveDECR-V BY Name
XType=’U’:表示具有顾客表; XType=’S’:表示全系列统表; 4.赢得具有字段名:
SELECT Name FROM SysColumns WHERE id=Object_Id(‘TableName’卡塔尔(قطر‎5.获得数据库全部类别 select name from systypes  6.得到主键字段
SELECT  name FROM SysColumns WHERE id=Object_Id(‘表名’) and colid=(select top 1 keyno from sysindexkeys where id=Object_Id(‘表名’))
[回顾网络资料收拾]

declare @strfd nvarchar(二零零四)–复合主键列表,@strjoin
nvarchar(4000卡塔尔国–连接字段,@strwhere
nvarchar(二零零一State of Qatar–查询条件–检查输入参数set
@querystr=ltrim(rtrim(@querystr卡塔尔(قطر‎卡塔尔国select
@obj_id=object_id(@querystr),@fdshow=case isnull(@fdshow,”) when ”
then ‘ *’ else ‘ ‘+@fdshow end,@fdorder=case isnull(@fdorder,”) when
” then ” else ‘ order by

3.**获得具备字段名:**
(1)、Select Name FROM SysColumns Where
id=Object_Id(‘TableName’)

1.得到具备数据库名:    (1)、Select
Name FROM Master..SysDatabases order by Name 2.赢得具备表名:   
(1卡塔尔(قطر‎、Select Name FROM SysObjects Where XType=’U’ orDEEscort BY Name
           XType=’U’:表示具备客户表;           
XType=’S’:表示全类别统表;

‘+@fdorder end,@querystr=case when @obj_id is not null then ‘
‘+@querystr else ‘

(2)、SELECT syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length
FROM syscolumns, systypes WHERE syscolumns.xusertype =
systypes.xusertype AND “syscolumns.id =
object_id(‘tableName’)

   (2)、SELECT name FROM sysobjects
WHERE type = ‘U’ AND sysstat = ’83’

(‘+@querystr+’卡塔尔 a’ end–输出总记录数set @temp= ‘select
@rscount=count(*) from ‘ + @querystr+’ ‘+@wherestrset @strparam =
n’@rscount int out’execute sp_executesql @temp,@strparam,@rscount
out–要是展现第一页,能够一贯用top来成功if @pagecurrent=1beginselect
@id1=cast(@pagesize as varchar(20卡塔尔State of Qatarexec(‘select top ‘+@id1+@fdshow+’
from
‘+@querystr+@wherestr+@fdorder卡塔尔(قطر‎returnend–假若是表,则检查表中是或不是有标记更或主键if
@obj_id is not null and objectproperty(@obj_id,’istable’)=1beginselect
@id1=cast(@pagesize as
varchar(20)),@id2=cast((@pagecurrent-1)*@pagesize as varchar(20))select
@fdname=name from syscolumns where id=@obj_id and status=0x80if
@@rowcount=0–假若表中无标记列,则检查表中是不是有主键beginif not
exists(select 1 from sysobjects where parent_obj=@obj_id and

       注意点:
     (a)这里为了主要卓绝某些重大内容,采取了此中几项消息输出。
     (b)syscolumns表中只含有数据类型编号,要博取完整的名字必要从systypes表中找,平时客户采纳的数据类型用xusertype对应比较好,不会冷俊不禁有的多的图景。
     (c)syscolumns.length获得的是大意内部存款和储蓄器的长短,所以nvarchar和varchar等等级次序在数据库中的突显是其一的一半。

           注意:日常情形只须求type =
‘U’,但神跡会有系统表混在里面(不知情什么样来头),加上后边一句后就能够去除这一个种类表了

xtype=’pk’State of Qatargoto lbusetemp–假诺表中无主键,则用不常表管理select
@fdname=name from syscolumns where id=@obj_id and colid in(select colid
from sysindexkeys where @obj_id=id and indid in(select indid from
sysindexes where @obj_id=id and name in(select name from sysobjects
where xtype=’pk’ and parent_obj=@obj_id)))

 

3.获得具备字段名: (1)、Select Name
FROM SysColumns Where id=Object_Id(‘TableName’)

if @@rowcount1–检查表中的主键是还是不是为复合主键beginselect
@strfd=”,@strjoin=”,@strwhere=”select
@strfd=@strfd+’,[‘+name+’]’,@strjoin=@strjoin+’ and
a.[‘+name+’]=b.[‘+name+’]’,@strwhere=@strwhere+’ and b.[‘+name+’]
is null’from syscolumns where id=@obj_id and colid in(select colid from
sysindexkeys where @obj_id=id and indid in(select indid from sysindexes
where @obj_id=id and name in(select name from sysobjects where
xtype=’pk’ and parent_obj=@obj_id)))select
@strfd=substring(@strfd,2,2000),@strjoin=substring(@strjoin,5,4000),@strwhere=substring(@strwhere,5,4000)goto
lbusepkendendendelsegoto
lbusetemp/*–使用标识列或主键为单一字段的拍卖方法–*/lbuseidentity: if
len(@wherestr)10beginexec(‘select top ‘+@id1+@fdshow+’ from
‘+@querystr+@wherestr+’ and ‘+@fdname+’ not in(select top ‘+@id2+’
‘+@fdname+’ from
‘+@querystr+@wherestr+@fdorder+’)’+@fdorder)returnendelsebeginexec(‘select
top ‘+@id1+@fdshow+’ from ‘+@querystr+’ where ‘+@fdname+’ not in(select
top ‘+@id2+’ ‘+@fdname+’ from
‘+@querystr+@fdorder+’)’+@fdorder)returnend/*–表中有复合主键的拍卖格局–*/lbusepk:exec(‘select
‘+@fdshow+’ from(select top ‘+@id1+’ a.* from(select top 100 percent *
from ‘+@querystr+@fdorder+’) aleft join (select top ‘+@id2+’ ‘+@strfd+’
from ‘+@querystr+@fdorder+’) b on ‘+@strjoin+’where ‘+@strwhere+’)
a’)return/*–用有时表管理的办法–*/lbusetemp:select
@fdname='[id_’+cast(newid() as
varchar(40))+’]’,@id1=cast(@pagesize*(@pagecurrent-1) as
varchar(20)),@id2=cast(@pagesize*@pagecurrent-1 as
varchar(20))exec(‘select ‘+@fdname+’=identity(int,0,1),’+@fdshow+’into
#tb from’+@querystr+@fdorder+’select ‘+@fdshow+’ from #tb where
‘+@fdname+’ between ‘+@id1+’ and ‘+@id2)

4、取得表中主键所蕴藏的列名:

(2)、SELECT
syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length
FROM syscolumns, systypes WHERE syscolumns.xusertype =
systypes.xusertype AND “syscolumns.id = object_id(‘tableName’)

    SELECT syscolumns.name FROM
syscolumns,sysobjects,sysindexes,sysindexkeys WHERE syscolumns.id =
object_id(‘tablename’) AND sysobjects.xtype = ‘PK’ AND
sysobjects.parent_obj = syscolumns.id AND sysindexes.id = syscolumns.id
AND sysobjects.name = sysindexes.name AND sysindexkeys.id =
syscolumns.id AND sysindexkeys.indid = sysindexes.indid AND
syscolumns.colid = sysindexkeys.colid

       注意点:     
(a)这里为了首要优质某个器重内容,选用了此中几项新闻输出。     
(b)syscolumns表中只蕴含数据类型编号,要获取完整的名字需求从systypes表中找,常常客户接纳的数据类型用xusertype对应比较好,不会并发局地多的动静。
    
(c)syscolumns.length获得的是情理内部存储器的尺寸,所以nvarchar和varchar等体系在数据库中的显示是以此的二分一。

只顾:这是在4张系统表中寻觅的,关系相比复杂,差不离能够象征为:
syscolumns中存有表中的列音信和表id,sysobjects表中存有主键名字(即PK_Table近似)和表id,sysindexes中存
有主键名字和表id和index编号,sysindexkeys中存有表id和index编号和列编号,一项一项对应起来后就能够找到列名了。

 

 

4、取得表中主键所蕴含的列名:

另外的SQL代码

    SELECT syscolumns.name FROM
syscolumns,sysobjects,sysindexes,sysindexkeys WHERE syscolumns.id =
object_id(‘tablename’) AND sysobjects.xtype = ‘PK’ AND
sysobjects.parent_obj = syscolumns.id AND sysindexes.id = syscolumns.id
AND sysobjects.name = sysindexes.name AND sysindexkeys.id =
syscolumns.id AND sysindexkeys.indid = sysindexes.indid AND
syscolumns.colid = sysindexkeys.colid

select syscolumns.name,
systypes.name,
syscolumns.length from syscolumns   

瞩目:这是在4张系统表中搜寻的,关系相比复杂,差相当的少能够代表为:
syscolumns中存有表中的列新闻和表id,sysobjects表中存有主键名字(即PK_Table相符)和表id,sysindexes中存
有主键名字和表id和index编号,sysindexkeys中存有表id和index编号和列编号,一项一项对应起来后就会找到列名了。

  left join
systypes on syscolumns. xusertype
=systypes. xusertype
 

 

  where id=(select
id from sysobjects where name=’订货主档’卡塔尔国;

另外的SQL代码

go;

select syscolumns.name, systypes.name,
syscolumns.length from syscolumns   

抑或用如此的写法,执行结果相近:

  left join systypes on syscolumns.
xusertype =systypes. xusertype
 

select syscolumns.name,
systypes.name,
syscolumns.length from syscolumns,systypes

  where id=(select id from sysobjects
where name=’订货主档’State of Qatar;

where (syscolumns.id=object_id(‘订货主档’卡塔尔(قطر‎ and syscolumns.xusertype=systypes.xusertype)

go;

order
by syscolumns.colorder;

抑或用这么的写法,实施结果一致:

go

select syscolumns.name, systypes.name,
syscolumns.length from syscolumns,systypes

实行结果:(字段只现身二次,平常)

where
(syscolumns.id=object_id(‘订货主档’卡塔尔(قطر‎ and
syscolumns.xusertype=systypes.xusertype)

订单号码   
int4

order by syscolumns.colorder;

顾客编号   
nvarchar    10

go

职员和工人编号   
int4

推行结果:(字段只现身一回,符合规律)

订单日期   
datetime    8

订单号码    int 4

要货日期   
datetime    8

客商编号    nvarchar    10

送货日期   
datetime    8

工作者编号    int 4

送货情势   
int4

订单日期    datetime    8

运费    money  
8

要货日期    datetime    8

收货人  nvarchar   
80

送货日期    datetime    8

送货地址   
nvarchar    120

送货情势    int 4

送货城市   
nvarchar    30

运费    money   8

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注