) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused )) declare sp cursor local for select '['+name+']' from sysobjects where type = 'u' open sp fetch sp into @name begin set @sql = 'insert into #space
原文:使用一个T-SQL语句批量查询数据表占用空间及其行数 要找到数据库中数据表占用的空间和存在的行数.可以使用sp_spaceused搭配数据表的名称.就可以产生该表耗用的空间和现有行数. 如: USE ADVENTUREWORKS GO EXEC sp_spaceused [Sales.SalesOrderHeader] GO 但如果数据库中包含数千的数据表,如何能利用一句SQL语句来实现? 解决方法: 一.动态SQL: 先用T-SQL动态产生表达式,然后放到一个查询中执行.如: USE A
SQL复制数据表 (select * into 与 insert into) select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名 以上两句都是将 源表 的数据插入到 目标表,但两句又有区别的: 第一句(select into from)要求目标表不存在,因为在插入时会自动创建. 第二句(insert into select from)要求目标表存在,由于目标表已经存在,所以我们除了插入源
--SQL查询每个表的字段数量select b.[name], count(*) As AllCount,ISNULL(ISNULL(sum(case when isnullable=0 then 1 end),null),null) as NotNullCountfrom syscolumns aINNER JOIN( select [id], [name] from [sysobjects] where [type] = 'u' ) AS b ON a.id = b.[id] GROUP b
SQL查询一个表中类别字段中Max()最大值对应的记录 SELECT A.id, A.name, A.version FROM DOC A, (SELECT id, MAX(version) AS version FROM DOC GROUP BY id) AS B WHERE A.id = B.id AND A.version = B.version
oracle:查询数据表是否存在 select count(*) as NUM from all_tables where table_name = '{$table}' 或者: select count(*) as NUM from all_tables where owner = '{$user}' and table_name = '{$table}' 某个用户是否拥有这张表
今天同事遇到一个很奇怪的问题,恢复了一个数据库,表明明存在,用PLSQL和sqlplus都试过了,SQL语句select * from 表名,查询数据,却提示表名不存在异常 然而,使用select * from 用户名.表名的方式,却可以查询出数据 网上有说是没有权限,但实际上已经有权限了 时间来不及,先想了个大招,使用create table 表名 as select * from 用户名.表名的方式新建了一个表,居然还创建表成功了,暂时解决燃眉之急,至于实际原因,还未找到~
--尽量少用触发器,否则数据库增长很快,特别是关于登陆的数据表字段不要用出发器,一周左右能使得数据库增长1G的空间. --数据库表空间大小查询脚本 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[#Data]') AND type in (N'U'))DROP TABLE [dbo].[#Data] create table #Data(name varchar(100),row varchar
查询数据库 select * From master.dbo.sysdatabases where name='数据库名' and status<>512 --读取库中的所有表名 (当前数据库)select name from sysobjects where xtype='u' --读取指定表的所有列名 select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name