http://www.cnblogs.com/vic_lu/archive/2011/06/24/2088883.html
http://imdbt.blog.51cto.com/903896/203613
--利用SQL未公开的存储过程实现分页
if
exists (
select *
from dbo.
sysobjects
where id =
object_id (N
'[dbo].[p_splitpage]' )
and
OBJECTPROPERTY (id, N
'IsProcedure' ) = 1)
drop
procedure [dbo].[p_splitpage]
GO
create
procedure p_splitpage
@sql
nvarchar (4000),
--要执行的sql语句
@currentpage
int =2,
--要显示的页码
@pagesize
int =10,
--每页的大小
@recordcount
int =0 out,
--记录数
@pagecount
int =0 out
--总页数
as
set nocount
on
declare @p1
int
exec
sp_cursoropen @p1 output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount output
select @recordcount=@pagecount,@pagecount=
ceiling (1.0*@pagecount/@pagesize)
,@currentpage=(@currentpage-1)*@pagesize+1
select @recordcount recordcount ,@pagecount pagecount,@currentpage currentpage
exec
sp_cursorfetch @p1,16,@currentpage,@pagesize
exec
sp_cursorclose @p1
go
一、定义变量
declare
@user1 nvarchar(50)
declare
@user2 nvarchar(50)
select
@user2 =
Name
from
ST_User
where
ID=1
declare
@user3 nvarchar(50)
update
ST_User
set
@user3 =
Name
where
ID=1
二、表、临时表、表变量
[Login] [nvarchar](50)
NOT
NULL
,
[Rtx] [nvarchar](4)
NOT
NULL
,
[
Name
] [nvarchar](5)
NOT
NULL
,
[
Password
] [nvarchar](
max
)
NULL
,
[State] [nvarchar](8)
NOT
NULL
insert
into
#DU_User1 (ID,Oid,[Login],Rtx,
Name
,[
Password
],State)
values
(100,2,
'LS'
,
'0000'
,
'临时'
,
'321'
,
'特殊'
);
select
*
into
#DU_User2
from
ST_User
where
ID<8
select
*
from
#DU_User2
where
ID<3
union
select
*
from
#DU_User1
[Login] [nvarchar](50)
NOT
NULL
,
[Rtx] [nvarchar](4)
NOT
NULL
,
[
Name
] [nvarchar](5)
NOT
NULL
,
[
Password
] [nvarchar](
max
)
NULL
,
[State] [nvarchar](8)
NOT
NULL
,
insert
into
#t
select
*
from
ST_User
alter
table
#t
add
[myid]
int
NOT
NULL
IDENTITY(1,1)
alter
table
#t
add
[myid1] uniqueidentifier
NOT
NULL
default
(newid())
select
IDENTITY(
int
,1,1)
as
ID,
Name
,[Login],[
Password
]
into
#t
from
ST_User
select
(
select
SUM
(1)
from
ST_User
where
ID<= a.ID)
as
myID,*
from
ST_User a
order
by
myID
insert
into
@t
values
(1,
'1'
)
insert
into
@t
values
(2,
'2'
)
三、循环
四、条件语句
declare
@week nvarchar(3)
五、游标
declare
@Login
varchar
(50)
declare
user_cur
cursor
for
select
ID,Oid,[Login]
from
ST_User
fetch
next
from
user_cur
into
@ID,@Oid,@Login
六、触发器
触发器中的临时表:
Inserted 存放进行insert和update 操作后的数据 Deleted 存放进行delete 和update操作前的数据
Create
trigger
User_OnUpdate
select
@msg = N
'姓名从“'
+ Deleted.
Name
+ N
'”修改为“'
+ Inserted.
Name
+
'”'
from
Inserted,Deleted
insert
into
[LOG](MSG)
values
(@msg)
drop
trigger
User_OnUpdate
七、存储过程
execute
PR_Sum 1,2,@mysum
output
execute
@mysum2= PR_Sum2 1,2
八、自定义函数
函数的分类:
1)标量值函数
2)表值函数
a:内联表值函数
b:多语句表值函数
3)系统函数
create
function
FUNC_Sum1
create
function
FUNC_UserTab_1
return
(
select
*
from
ST_User
where
ID<@myId)
create
function
FUNC_UserTab_2
[Login] [nvarchar](50)
NOT
NULL
,
[Rtx] [nvarchar](4)
NOT
NULL
,
[
Name
] [nvarchar](5)
NOT
NULL
,
[
Password
] [nvarchar](
max
)
NULL
,
[State] [nvarchar](8)
NOT
NULL
insert
into
@t
select
*
from
ST_User
where
ID<@myId
select
*
from
dbo.FUNC_UserTab_1(15)
set
@s=dbo.FUNC_Sum1(100,50)
谈谈自定义函数与存储过程的区别:
一、自定义函数:
1. 可以返回表变量
2. 限制颇多,包括
不能使用output参数;
不能用临时表;
函数内部的操作不能影响到外部环境;
不能通过select返回结果集;
不能update,delete,数据库表;
3. 必须return 一个标量值或表变量
自定义函数一般用在复用度高,功能简单单一,争对性强的地方。
二、存储过程
1. 不能返回表变量
2. 限制少,可以执行对数据库表的操作,可以返回数据集
3. 可以return一个标量值,也可以省略return
存储过程一般用在实现复杂的功能,数据操纵方面。