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 ...
随机推荐
- [Java] File类的常用操作
package test.file; import java.io.File; import java.io.IOException; public class TestFile { public s ...
- 关于在页面总嵌入iframe,ifram中发起请求,服务器端的session为空问题解决
本文抄袭:http://blog.csdn.net/ray_adon/article/details/6960724 在做项目是 是用了iframe,iframe发起ajax请求,服务器端报sessi ...
- What is Split Brain in Oracle Clusterware and Real Application Cluster (文档 ID 1425586.1)
In this Document Purpose Scope Details 1. Clusterware layer 2. Real Application Cluster (d ...
- Spark ThriftServer使用的大坑
当用beeline连接default后,通过use xxx切换到其他数据库,再退出, 再次使用beeline -u jdbc:hive2://hadoop000:10000/default -n sp ...
- Fiddler抓包工具的使用
下载 自行去官网下载 http://www.telerik.com/fiddler 配置Fiddler 1.打开Fiddler, Tools-> Fiddler Options -> HT ...
- java基础问题 (转)
原文地址:http://blog.csdn.net/free0sky/article/details/7927275 一.String,StringBuffer, StringBuilder 的区别是 ...
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- linux 将foo制定n, m之间行的内容, 追加到bar文件
sed -ne '196, 207 p' foo >> bar;把文件foo 196-行207行的内容追加到 bar文件
- eclipse中logcat偶尔不显示log的问题解决办法
Android开发过程中 eclipse 经常会出现 logcat突然就是不现实log的情况.经常遇到,一直没有解决.后来解决了,记录一下. 默认的设置是error 改成verbos 问题解决.
- ios项目记录
1,如何隐藏状态栏 在基类中重载UIViewController.h中的这个方法 - (BOOL)prefersStatusBarHidden { // iOS7后,[[UIApplication s ...