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 ...
随机推荐
- C++学习4
在C++中,定义函数时可以给参数指定一个默认的初始值.调用函数时,可以省略有默认值的参数.也就是说,如果用户指定了参数的值,那么就使用用户指定的值,否则使用参数的默认值. C++规定,默认参数只能放在 ...
- springmvc对请求执行流程
doService-->getHandlerMapping-->handlerMapping-->getHandler-->HandlerExecutionChain--> ...
- Java SE 第十六讲----面向对象特征之继承
1.继承(inheritance):Java是单继承的,意味着一个类只能从另一个类继承(被继承的类叫做父类也叫[基类 baseclass]),继承的类叫做子类,java中的继承使用extends关键字 ...
- sqlite 时间排序
select * from tb_QuantifyResult where iSamplingOrCalibration = 1 and cComponentName <> ' + Quo ...
- esriSRProjCSType Constants
ArcGIS Developer Help (Geometry) esriSRProjCSType Constants See Also esriSRProjCS2Type Constant ...
- nyoj 86 找球号(一)
点击打开链接 找球号(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i ...
- addAll()报NullPointer原因
如下代码在注释行会报错,原因是:getSeatTravelerInfo()时值为空,对空对象addAll会报错 SeatBookingInfo b=new SeatBookingInfo(); b.s ...
- Linux学习小结(转)
linux目录架构 / 根目录/bin 常用的命令 binary file 的目錄/boot 存放系统启动时必须读取的档案,包括核心 (kernel) 在内/boot/grub/menu.l ...
- 使用appium模拟用户发送短信
一段简单粗糙的代码.主要是实现的功能是模拟用户发送短信的功能. python版本3.5.2 appium版本1.4.16.1 from appium import webdriver desired_ ...
- 软件测试入门——测试模型(V型 W型 H型)
软件测试工程师称为“QA”,质量保证者——这是入门的第一点要学习的. 首先看基本的测试模型 1.“V”型 特点:[活动串行]这是一种古老的瀑布模型,反映了实际和测试之间的关系. 局限:仅仅把测试过程作 ...