T—SQL用法剪辑,方便以后查看
一、用T-SQL查询表中第n行到第m行数据的写法示例
假设这里的n=6,m=10则有以下两种写法,qusID可以不连续,如下:
select top 5 * from tb_wenti where qusID not in(select top 5 qusID from tb_wenti);
select top 5 * from tb_wenti where qusID in(select top 10 qusID from tb_wenti) order by qusID desc;
一般的写法为
select top m-n+1 * from tablename where id not in(select top n-1 id from tablename);
select top m-n+1 * from tablename where id in(select top m id from tablename) order by id desc;
二、从学生表(Student)里分别统计男生人数和女生人数(用一条SQL语句)
select distinct (select count(*) from Student where 性别='男') 男生数,(select count(*) from Student where 性别='女') 女生数 from Student;
其结果如图

三、查询数据库的逻辑文件名
(1)对象为数据库
select name, filename, * from dbo.sysfiles
*需要指定查询的数据库
(2)对象为数据库备份文件
restore filelistonly from disk =‘完整备份文件路径’
*使用master数据库
四、断开数据库链接的存储过程
第一种方式
create proc killspid
@dbname sysname
as
declare @s nvarchar(1000)
declare tb cursor local for
select N'kill '+cast(spid as varchar)
from master..sysprocesses
where dbid=db_id(@dbname) open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go
第二种方式(在程序中使用)
string sqltext = " declare @sql varchar(100) \n"
+ "while 1=1 \n"
+ "begin \n"
+ "select top 1 @sql = 'kill '+cast(spid as varchar(3)) from master..sysprocesses where spid > 50 and spid <> @@spid and dbid=db_id('"+dbname+"') \n"
+ "if @@rowcount = 0 \n" + "break \n"
+ "exec(@sql) \n" + "end \n";
五、分离数据库
sp_detach_db '数据库名称','true'
六、判断指定数据库是否已附加在SQL Server中
select count(*) From master.dbo.sysdatabases where [name]='dbname'
T—SQL用法剪辑,方便以后查看的更多相关文章
- SQL用法操作合集
SQL用法操作合集 一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...
- SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)
原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...
- Elasticsearch SQL用法详解
Elasticsearch SQL用法详解 mp.weixin.qq.com 本文详细介绍了不同版本中Elasticsearch SQL的使用方法,总结了实际中常用的方法和操作,并给出了几个具体例子 ...
- SQL Server中授予用户查看对象定义的权限
SQL Server中授予用户查看对象定义的权限 在SQL Server中,有时候需要给一些登录名(用户)授予查看所有或部分对象(存储过程.函数.视图.表)的定义权限存.如果是部分存储过程.函数. ...
- 【MSSQL】SQL Server 设置用户只能查看并访问特定数据库
#背景 SQL Server实例上有多个服务商的数据库,每个数据库要由各自的服务商进行维护, 为了限定不同服务商商的维护人员只能访问自己的数据库,且不能看到其他服务商的数据库,现需要给各个服务商商限定 ...
- 在SQL中有时候我们需要查看现在正在SQL Server执行的命令
在SQL中有时候我们需要查看现在正在SQL Server执行的命令.在分析管理器或者Microsoft SQL Server Management Studio中,我们可以在"管理-SQL ...
- (2.6)Mysql之SQL基础——存储引擎的查看与修改
(2.6)Mysql之SQL基础——存储引擎的查看与修改 可以使用 show engines; 查看数据库支持的所有的存储引擎: 目录: 1.数据库级别存储引擎 1.1查看现在默认的存储引擎 1.2 ...
- sqlserver 出现sql被锁时,查看加锁和被锁的sql
原文:sqlserver 出现sql被锁时,查看加锁和被锁的sql DECLARE @spid INT DECLARE @blk INT DECLARE @count INT DECLARE @ind ...
随机推荐
- sql server中的锁 事务锁 更新锁 保持锁 共享锁 你知道吗?
锁定数据库的一个表 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其 ...
- [Java] 读写字符串数据
package test.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- MemoryStream 的GetBuffer() 和 ToArray()的区别
GetBuffer(): Note that the buffer contains allocated bytes which might be unused. For example, if th ...
- iOS UIButton 设置图片文字垂直排列
后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现, ...
- Xcode自动注释插件
开源xcode插件:规范注释生成器VVDocumenter 1.类似eclipse 和 vs studio 在前面输入/// 后触发,自动生成代码注释,如图 2.GitHub工程文件地址:https: ...
- Java中1000==1000为false而100==100为true
public static void main(String[] args) { int z1 = 0; int z2 = 0; System.out.println(z1==z2);//TRUE I ...
- SmartWiki开发日记之Laravel缓存扩展
SmartWiki简介请阅读: http://www.cnblogs.com/lifeil/p/6113323.html 因为SmartWiki的演示站点部署在阿里云上,阿里云有一个128M免费的Me ...
- Xcode去除某种类型的警告
首先来看下当有警告时,怎么找到警告类型,在某条警告上,右键—>Reveal in Log 下面 [ ] 中间就是警告信息 去除警告信息的几种方式: 一.使用编译器提供的宏 ...
- jQuery datepicker
<script type="text/javascript" src="/assets/datepicker/jquery-ui-1.9.1.min.js" ...
- extern “C”调用测试与验证-2016.01.06
1 调用情形说明 在上一篇关于extern “c”原理以及用法中,详细的说明了为什么需要extern “c”以及如何使用它解决c与c++混合编程时遇到的问题.接下来,使用示例验证方式验证c与c++函数 ...