Menu
Woocommerce Menu

–要显示的字段列表,status=’0’表示糸统帐户 2.获取所有数据库名

0 Comment

xtype=’pk’)goto 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)))

送货行政区 
nvarchar    30

送货城市    sysname    30

SELECT
sysobjects.name AS 表名称 , ————–as 的作用:为字段起一个别名
–sysproperties.[value] AS 表说明 , ———-[
]方括号的作用:为了避免和关键字冲突
syscolumns.name AS 字段名称 ,
–properties.[value] AS 字段说明 ,
systypes.name AS 字段类型 ,
syscolumns.length AS 字段长度 ,
ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name,’Scale’), 0) AS
小数位数 ,
–isnull—(待测数据,返回值).当待测数据为空时返回返回值,当大策数据为非null时,反回待测数据
CASE WHEN syscolumns.isnullable=0
THEN ”
ELSE ‘ √ ‘
END AS 是否为空 ,
CASE WHEN syscomments.text IS NULL
THEN ” ELSE syscomments.text
END AS 缺省值 ,
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, ‘IsIdentity’)=
1
THEN ‘ √ ‘ ELSE ”
END AS 递增字段 ,
CASE WHEN sysindexes.name IS NULL
THEN ”
ELSE sysindexes.name
END AS 索引名称 ,
CASE WHEN sysindexkeys.keyno IS NULL
THEN ”
ELSE CONVERT(VARCHAR(10),sysindexkeys.keyno )
END AS 索引位置 ,
CASE WHEN sysindexes.indid=1
THEN ‘ 聚集索引 ‘
WHEN sysindexes.indid>1 AND sysindexes.indid<>255
THEN ‘ 非聚集索引 ‘
WHEN sysindexes.indid IS NULL
THEN ”
ELSE
‘ 其他 ‘
END AS 索引类型 ,
CASE WHEN EXISTS
(SELECT 1
FROM sysobjects
WHERE xtype = ‘PK’ AND name IN
(SELECT name
FROM sysindexes
WHERE indid IN
(SELECT indid
FROM sysindexkeys
WHERE id = syscolumns.id AND colid = syscolumns.colid)))
THEN ‘ √ ‘ ELSE ”
END AS 主键 ,
CASE WHEN sysforeignkeys.constid IS NULL
THEN ”
ELSE ‘ √ ‘
END AS 外健
FROM syscolumns — 数据表字段
INNER JOIN sysobjects — 数据对象
ON sysobjects.id = syscolumns.id
INNER JOIN systypes — 数据类型
ON syscolumns.xtype = systypes.xtype

高效分页方法代码(sql百万级数据量分页代码)

订单日期   
datetime   8

 

LEFT OUTER JOIN syscomments — 注释信息
ON syscolumns.cdefault = syscomments.id
LEFT OUTER JOIN sysindexkeys — 索引中的键或列的信息
ON sysindexkeys.id = syscolumns.id
AND sysindexkeys.colid = syscolumns.colid
LEFT OUTER JOIN sysindexes — 数据库 索引表
ON sysindexes.id = sysindexkeys.id
AND sysindexes.indid = sysindexkeys.indid
LEFT OUTER JOIN sysforeignkeys
ON sysforeignkeys.fkeyid = syscolumns.id
AND sysforeignkeys.fkey = syscolumns.colid
WHERE (sysobjects.xtype = ‘U’)
order by sysobjects.id,syscolumns.colid

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)

 

送货地址    nvarchar   120

定此值,且不包含标识字段@fdorder nvarchar
(100)=”,–排序字段列表@wherestr nvarchar (200)=”, –内容是’ id=3 and
model_no like ‘%24%’

执行结果:(部分字段出现两次,数据类型不同)

北风贸易    dbo DepartmentSalaryInfo;1  
@minimum   4   2   0   NULL   1   6   NULL   NULL   19    NULL   NULL  
money  money

declare @strfd nvarchar(2000)–复合主键列表,@strjoin
nvarchar(4000)–连接字段,@strwhere
nvarchar(2000)–查询条件–检查输入参数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

运费    money  
8

go;

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

要货日期   
datetime    8

送货国家地区    nvarchar    30

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

送货行政区 
sysname    30

  where id=(select id from sysobjects
where name=’DepartmentSalaryInfo’);

@querystr nvarchar(300),–表名、视图名、查询语句@pagesize
int=10,–每页的大小(行数)@pagecurrent int=1,–要显示的页@fdshow nvarchar
(100)=”,–要显示的字段列表,如果查询结果有标识字段,需要指

送货城市   
nvarchar    30

运费    money   8

(‘+@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))exec(‘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

注意:这是在4张系统表中寻找的,关系比较复杂,大致可以表示为:
syscolumns中存有表中的列信息和表id,sysobjects表中存有主键名字(即PK_Table类似)和表id,sysindexes中存
有主键名字和表id和index编号,sysindexkeys中存有表id和index编号和列编号,一项一项对应起来后就能找到列名了。

北风贸易    dbo DepartmentSalaryInfo;1  
@maximum   3   2   0   NULL   1   6   NULL   NULL   19    NULL   NULL  
money  money

declare @fdname nvarchar(250)–表中的主键或表、临时表中的标识列名,@id1
varchar(20),@id2 varchar(20)–开始和结束的记录号,@obj_id int
–对象id,@temp nvarchar(300) –临时语句,@strparam nvarchar(100)
–临时参数

1:获取当前数据库中的所有用户表
select Name from sysobjects where xtype=’u’ and status>=0
2:获取某一个表的所有字段
select name from syscolumns where id=object_id(‘表名’)
3:查询用户创建的所有数据库
select * from master..sysdatabases D where sid not in(select sid from
master..syslogins where name=’sa’)
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
4:查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = ‘表名’
[n].[标题]:
Select * From TableName Order By CustomerName
[n].[标题]:
8.如何修改数据库的名称:
sp_renamedb ‘old_name’, ‘new_name’
9.只复制一个表结构,不复制数据
select top 0 * into [t1] from [t2]
10.连接远程数据库
select * from OPENDATASOURCE(‘SQLOLEDB’,’Data Source=远程ip;User
ID=sa;Password=密码’).库名.dbo.表名
11.获取当前oracle数据库中的所有表
select table_name from user_tables
12 .获取当前oracle表中所有字段的类型
SELECT
      COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE
FROM
         USER_TAB_COLS where TABLE_NAME=’teacher’;
 
 
 
查询数据库存储量大小 (Master)
DECLARE @tablespaceinfo TABLE (  
    nameinfo varchar(50),  
    rowsinfo int,  
    reserved varchar(20),  
    datainfo varchar(20),  
    index_size varchar(20),  
    unused varchar(20)  
)  
 
DECLARE @tablename varchar(255);  
 
DECLARE Info_cursor CURSOR FOR 
    SELECT [name] FROM sys.tables WHERE type=’U’;  
 
OPEN Info_cursor  
FETCH NEXT FROM Info_cursor INTO @tablename  
 
WHILE @@FETCH_STATUS = 0  
BEGIN 
    insert into @tablespaceinfo exec sp_spaceused @tablename  
    FETCH NEXT FROM Info_cursor  
    INTO @tablename  
END 
 
CLOSE Info_cursor  
DEALLOCATE Info_cursor  
 
SELECT * FROM @tablespaceinfo  
    ORDER BY Cast(Replace(reserved,’KB’,”) as INT) DESC 

 

–存储过程中的参数名,参数类型,参数长度

查询存储过程DepartmentSalaryInfo所有的信息,信息包含在系统视图syscolumns,systypes中

go;

北风贸易    dbo DepartmentSalaryInfo;1  
@department   1   1   0   NULL   1   129 10  10  NULL    NULL   NULL  
varchar    varchar

       注意点:
     (a)这里为了重点突出某些重要内容,选取了其中几项信息输出。
     (b)syscolumns表中只含有数据类型编号,要获取完整的名字需要从systypes表中找,一般用户使用的数据类型用xusertype对应比较好,不会出现一对多的情况。
     (c)syscolumns.length得到的是物理内存的长度,所以nvarchar和varchar等类型在数据库中的显示是这个的一半。

 

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

要货日期    datetime   8

运费   
money  8

 

客户编号   
nvarchar   10

 

送货邮政编码   
nvarchar    20

送货邮政编码    nvarchar    20

           注意:一般情况只需要type =
‘U’,但有时候会有系统表混在其中(不知道什么原因),加上后面一句后就能删除这些系统表了

1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype=’u’ and status>=0 2:获取某一个表的所有字段 select name from syscolumns where id=object_id(‘表名’) 3:查询用户创建的所有数据库 select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name=’sa’) 或者 select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01 4:查询某一个表的字段和数据类型 select column_name,data_type from information_schema.columns where table_name = ‘表名’ [n].[标题]: Select * From TableName Order By CustomerName [n].[标题]: 8.如何修改数据库的名称: sp_renamedb ‘old_name’, ‘new_name’ 9.只复制一个表结构,不复制数据 select top 0 * into [t1] from [t2] 10.连接远程数据库 select * from OPENDATASOURCE(‘SQLOLEDB’,’Data Source=远程ip;User ID=sa;Password=密码’).库名.dbo.表名
11.获取当前oracle数据库中的所有表 select table_name from user_tables
12 .获取当前oracle表中所有字段的类型
SELECT       COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE
FROM          USER_TAB_COLS where TABLE_NAME=’teacher’;

 

【MSN】jorden008@hotmail.com

运费   
薪水类型   
8

送货方式    int 4

  where id=(select
id from sysobjects where name=’DepartmentSalaryInfo’);

送货国家地区    nvarchar   30

送货国家地区   
sysname    30

订单号码    int 4

送货方式   
int4

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

送货城市   
sysname    30

送货地址    nvarchar    120

订单日期   
datetime    8

收货人  nvarchar    80

订单号码   
int4

原文地址:

执行结果:

执行结果:(部分字段出现两次,数据类型不同)

发表评论

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

相关文章

网站地图xml地图