/*第几页必须大于1
select top 每页数量 * id
from @t a
where id not in
(select top (第几页-1)*每页数量 id
from @t b
)
*/
declare @lcSqlCommand nvarchar(100)
declare @t table (id int IDENTITY,orderDate datetime)
insert into @t
select orderDate
from Northwind..Orders
--返回第二页数据
select top 10 *
from @t a
where id not in
(select top 10 id
from @t b
) ......
''' <summary>
''' SQL文執行結果のEXCEL出力
''' </summary>
''' <param name="connString">OLEDB接続文字列</param>
''' <param name="sqlString">SQL文</param>
''' <param name="savePath">出力ファイルパス</param>
''' <remarks></remarks>
Public Shared Sub ExecSqlToExcel(ByVal connString As String, ByVal sqlString As String, _
ByVal savePath As String)
Dim mExcelApplication As Excel.Application = Nothing
Dim mWorkbook As Excel.Workbook = Nothing
Dim mWorkSheet As Excel.Worksheet = Nothing
Dim mQueryTable As Excel.QueryTable = Nothing
Try
mExcelApplication = New Excel.Application()
mWorkbook = mExcelApplication.Workbooks.Add
mWorkSheet = mWorkbook.Sheets(1)
mQueryTable = mWorkSheet.QueryTables.Add(connString, mWorkSheet.Range("A1"), sqlString)
......
假设你想找书中的某一个句子。你可以一页一页地逐页搜索,但这会花很多时间。而通过使用索引,你可以很快地找到你要搜索的主题。
表的索引与附在一本书后面的索引非常相似。它可以极大地提高查询的速度。对一个较大的表来说,通过加索引,一个通常要花费几个小时来完成的查询只要几分钟就可以完成。因此没有理由对需要频繁查询的表增加索引。
注意:
当你的内存容量或硬盘空间不足时,也许你不想给一个表增加索引。对于包含索引的数据库,SQL Sever需要一个可观的额外空间。例如,要建立一个聚簇索引,需要大约1.2倍于数据大小的空间。要看一看一个表的索引在数据库中所占的空间大小,你可以使用系统存储过程sp_spaceused,对象名指定为被索引的表名。
聚簇索引和非聚簇索引
假设你已经书的索引找到了一个句子所在的页码。一旦已经知道了页码后,你很可能漫无目的翻寻这本书,直至找到正确的页码。通过随机的翻寻,你最终可以到达正确的页码。但是,有一种找到页码的更有效的方法。
首先,把书翻到大概一半的地方,如果要找的页码比半本书处的页码小,就书翻到四分之一处,否则,就把书翻到四分之三的地方。通过这种方法,你可以继续把书分 ......
索引类型
唯一索引:唯一索引不允许两行具有相同的索引值
主键索引:为表定义一个主键将自动创建主键索引,主键索引是唯一索引的特殊类型。主键索引要求主键中的每个值是唯一的,并且不能为空
聚集索引(Clustered):表中各行的物理顺序与键值的逻辑(索引)顺序相同,每个表只能有一个
非聚集索引(Non-clustered):非聚集索引指定表的逻辑顺序。数据存储在一个位置,索引存储在另一个位置,索引中包含指向数据存储位置的指针。可以有多个,小于249个
优点
加快访问速度
加强行的唯一性
缺点
带索引的表在数据库中需要更多的存储空间
操纵数据的命令需要更长的处理时间,因为它们需要对索引进行更新
请按照下列标准选择建立索引的列。
该列用于频繁搜索
该列用于对数据进行排序
一、索引的概念
索引就是加快检索表中数据的方法。数据库的索引类似于书籍的索引。在书籍中,索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息。在数据库中,索引也允许数据库程序迅速地找到表中的数据,而不必扫描整个数据库。
二、索引的特点
1.索引可以加快数据库的检索速度
&n ......
--desc 表名 描述表的内容
desc emp;
--加上数学表达式和列名 ""保持格式
select ename "name space", sal*12 year_sal from emp;
select 2*3 from dual;
select sysdate from dual;
--空值的数学表达式 结果都是空值
select ename, sal*12 + comm from emp;
--"||"字符串连接 单引号中的是字符串,字符串中的单引号,''表示
select ename||sal from emp;
--distinct 修饰两个字段
select distinct deptno, job from emp;
select ename, sal from emp where sal between 800 and 1500;
select ename, sal from emp where sal >= 800 and sal <= 1500;
select ename, sal from emp where comm is null;
--一个事务开始于一条ddl语句,结束语rollback commit ddl数据定义语句 dcl数据控制语句语句
--正常断开连接 提交 非正常断开连接 回滚
select ename, sal from emp where sal not in (800 ,1500);
-- 模糊查询
select ename, sal from emp where ename like '_A%';
select ename, sal from emp where ename like '%\%%';
--转义字符
select ename, sal from emp ......
--sql structured query language
--DML--Data Manipulation Language--数据操作语言
query information (SELECT),
add new rows (INSERT),
modify existing rows (UPDATE),
delete existing rows (DELETE),
perform a conditional update or insert operation (MERGE),
see an execution plan of SQL (EXPLAIN PLAN),
and lock a table to restrict access (LOCK TABLE).
--DDL--Data Definition Language--数据定义语言
create, modify,drop, or rename objects (CREATE,ALTER,DROP,RENAME),
remove all rows from a database object without dropping the structure (TRUNCATE),
manage access privileges (GRANT,REVOKE),
audit database use (AUDIT,NOAUDIT)
and add a description about an object to the dictionary (COMMENT).
--Transaction Control事务控制语句
save the changes(COMMIT)
or discard the changes (ROLLBACK) made by DML statements.
Also included in the transaction-control statements are statements to set a point or marker in the transaction for possible rollback (SAVEPOINT)
and to define th ......