应一个朋友的要求,贴上收藏的SQL常用分页的办法~~
表中主键必须为标识列,[ID] int IDENTITY (1,1)
1.分页方案一:(利用Not In和SELECT TOP分页)
语句形式:
SELECT TOP 页记录数量 *
from 表名
WHERE (ID NOT IN
(SELECT TOP (每页行数*(页数-1)) ID
from 表名
ORDER BY ID))
ORDER BY ID
//自己还可以加上一些查询条件
例:
select top 2 *
from Sys_Material_Type
where (MT_ID not in
(select top (2*(3-1)) MT_ID from Sys_Material_Type order by MT_ID))
order by MT_ID
2.分页方案二:(利用ID大于多少和SELECT TOP分页)
语句形式:
SELECT TOP 每页记录数量 *
from 表名
WHERE (ID >
(SELECT MAX(id)
from (SELECT TOP 每页行数*页数 id from 表
ORDER BY id) AS T)
)
ORDER BY ID
例:
SELECT TOP 2 *
from Sys_Material_Type
WHERE (MT_ID >
(SELECT MAX(MT_ID)
& ......
--显示版本号,当前日期
SELECT VERSION(),CURRENT_DATE(),NOW();
--免费的计算器
SELECT (20+5)*4 AS RESULT,SIN(PI()/3);
--创建数据库
CREATE DATABASE databasename;
--删除数据库
DROP DATABASE databasename;
--显示当前存在的数据库
SHOW DATABASES;
--选择数据库
USE databasename;
--显示当前选择的数据库
SELECT DATABASE(), USER();
--显示当前数据库中的表
SHOW TABLES;
--插入表
CREATE TABLE tablename
(
id varchar(32) not null primary key,
name varchar(20) not null,
password varchar(20) not null
);
--插入数据
INSERT INTO tablename VALUES ('CZX','Christen','litejava');
--导入脚本
SOURCE filepath; -- eg SOURCE C:/blog.sql
--删除表
DROP TABLE tablename;
--删除表中的全部数据
DELETE from tablename;
--更新数据
UPDATE tablename SET password='123456';
--显示表的内容
SELECT * from tablename;
--描述表的结构
DESCRIBE tablename;
--从文本中导入数据
......
Suppose we have a recursive hierarchy stored in a relational database and we want to write it to XML. This might be for a variety of reasons – e.g. as a pre-cached input to a UI control, to export to another system using a pre-defined format, etc.
In SQL Server 2000, in order to get it straight to XML using FOR XML EXPLICIT, we would have to know the depth of the lowest node beforehand (without doing some very ugly dynamic SQL), so this does not help us.
It would be useful to access the data in the same order that it will appear in the XML. I.e.
Node1
Node2
Node3
Node4
Node5
Getting at the data in this order will allow us to iterate through the nodes in sequential order. This avoids using the DOM and is significantly quicker and more efficient as it avoids loading the whole structure into memory.
We could achieve this in SQL Server 2000 using a recursive table-valued UDF. In SQL Server 2005, we also have the op ......
SQL Server 得到行号的SQL
使用临时表:
select id=identity(int,1,1),value into #temp from YourTable
select * from #temp
drop table #temp
取得第11到20行记录:
select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp where ID_Num>10 and ID_Num<=20
或
SELECT Top @PageSize *
from T
WHERE SortField NOT IN (SELECT TOP @PageSize * @Pagei SortField
......
一:Select语句:
select 字段名 from 表名 where 条件 order by 排序
see:select distinct 从多个相同字段中抓唯一值
see:查找ld_det_andy表中有数据但ld_det_temp表中没数据的数据
select * from ld_det_andy a where
(not exists (select * from ld_det_temp where ld_loc = a.ld_loc and ld_part = a.ld_part))
see:选出累计访问量最大的10个ip地址,并按访问量降序排列。
select top 10 ip, countip from
(select ip,count(*) as countip from records group by ip) a
order by countip desc
see:表student(id,name,score)根据分数列(score)每10分为一段,查询每段分数的人数
select ScoreRank,count(*) from
(select ScoreRank = case
  ......
DB2 SQL PL
SQL PL是DB2所支持的过程化语言,它是SQL/PSM标准的一个子集。其根据应用范围不同,又分为Inline SQL PL,Embeded SQL PL和Compiled SQL PL。
Inline SQL PL
适用范围:触发器、函数和方法,支持部分SQL PL,使用时要注意一些限制
语法规则:BEGIN ATOMIC ... END
Embeded SQL PL
适用范围:嵌入式,配合宿主语言一起使用,有点类似于PowerBuilder编程形式
语法规则:BEGIN COMPOUND [ATOMIC|NOT ATOMIC] STATIC ... END
Compiled SQL PL
适用范围:支持所有的SQL PL
语法规则:BEGIN ... END
SQL Routine
包括过程、函数和方法(method),所谓的方法就是按照目的不同而创建出的概念,用于操作某个自定义类型(CREATE TYPE ...)的方法,有些类似于PL/SQL的面向对象概念。
DB2对PL/SQL的支持
DB2支持Oracle的PL/SQL,但在默认情况下这个功能是被关闭的,只有通过手动打开。
启动步骤:
db2start
db2set DB2_COMPATIBILITY_VECTOR=ORA
db2set DB2_DEFERRED_PREPARE_SEMANTICS=YES
db2stop
db2start
db2 CREATE DATABASE DB
测试结果:
CONNECT TO DB;
SET SQLCOMPAT PLSQL;
-- Semicolon is used to terminate
-- the CREATE TABLE statem ......