SQL Server 存储过程的分页
posted @ 2006-01-15 10:24 Royman.Chen 阅读(267) 评论(1) 编辑
My DBA + Programmer 成长路 ...
2006年1月15日 #
posted @ 2006-01-15 10:24 Royman.Chen 阅读(267) 评论(1) 编辑
Sql-Server 主要提供了两种机制来强制业务规则和数据完整性:约束constrain 和 触发器 trigger ..
触发器在制定的表中发生变化时被调用以响应insert,update,delete事件。
trigger可以查询其他表,包含复杂的T-sql语句,并支持回滚.
每个触发器都会创建两个逻辑专用表:inserted 和 deleted表,表结构与被触发作用的表结构相同 执行完毕后 与触发器相关的两个表都会被删除。
当执行insert语句时,inserted表存放要向表中插入的所有行
当执行delete语句时,deleted表存放要向表中删除的所有行
当执行update语句时,相当于执行一个delete操作,再执行一个insert操作,旧的行先被移动到deleted表,然后再在新值插入到inserted表和目标表。
一般的创建trigger : (for / after 语句 触发)
use DB_name
go
--
create trigger trigger_name
on table_Name // On 关键字
[with Encryption] //加密
for [/ after] {[insert [,]/ update [,] /delete]} --指定触发器只有在触发SQL语句中制定的所有操作都已经成功执行后才激发。(包括所有的级联和约束都检查成功后才能激发)
AS
[SQL-statements]
go 

利用触发器instead of 语句代替其他语句操作 (就不执行instead of 后面指定的SQL指令而执行Trigger体 [sql-statements])
use Db_Name
go
create trigger Trigger_Name
on Table_name
instead of [insert,update,delete]
as
[sql_statements]
go例如在系统查询分析器执行 update t1 set column_1='aaa' where id=123 可以看到t1下123号的column_1值没有预期改变 而是按照Trigger中 [sql-statements]中的格式走了
如何利用inserted 和 deleted表:
例: 创建一个触发器 要求当插入 删除 更新 T1表数据时 能更新 T2表中相应的C2字段值 (C2是统筹t1的字段)
use DB_Name
go
create trigger T_name
on T1
for update,insert,delete
as
update T2 set C2=C2+1 where id=(select id from inserted)
update T2 set C2=C2-1 where id=(select id from deleted)
go
//
use DB_Name
go
Alter Trigger Test1
on T1
for update
as
if update(PWD) print 'password has changed..'
go
用来检测修改该字段的操作
ps:允许多个事件insert delete update创建多个触发器 ,且相互不影响
-----
系统操作
改名trigger : sp_remane Old_Trigger_name,NewTrigger_Name
禁用: Alter Table T1 DISABLE trigger Trigger_Name
查询: select * from sysObjects where Type='TR'
posted @ 2006-01-15 07:58 Royman.Chen 阅读(542) 评论(0) 编辑
2006年1月14日 #

We'd like you to be among the first to try the beta version of Windows LiveTM Messenger.
If you use MSN?Messenger now, we think you're really going to enjoy Windows Live Messenger. It's as fun and reliable as the Messenger you already know, and you won't lose your contact list or any other personal information when you try this new version. Check it out to experience new ways to connect to your friends via text, voice, video, and more.
Download now
And in case you were wondering about the name change, Windows Live Messenger is part of a new family of services from Microsoft?that includes Windows Live Mail (now a simpler, safer, faster mail), and Live.com (a new personalized homepage experience), to name just a few.
Thanks,
The Windows Live Messenger team
You are receiving this message from Microsoft because you are a preferred Microsoft customer. We value our relationship with you, however, if you would prefer to no longer receive future research e-mails from Microsoft please follow this link: http://privacy.msn.com/choice/default.asp
Once your request is received, we will take prompt action to ensure you do not receive future research e-mails from Microsoft. Keep in mind that opting out of Microsoft "special offer" or research e-mails does not affect any newsletters you may have requested nor restrict important customer communications concerning your Microsoft products. If you have questions about Microsoft privacy policies, please read our privacy statement at: http://privacy.msn.com/.
Microsoft Corporation, One Microsoft Way, Redmond, WA 98052 ?2005 Microsoft Corporation. All Rights Reserved.
posted @ 2006-01-14 02:12 Royman.Chen 阅读(1002) 评论(53) 编辑
2006年1月10日 #


/**//****** Object: Stored Procedure dbo.pro_CheckAdminLogin Script Date: 2004-6-4 16:37:25 ******/
CREATE PROCEDURE pro_CheckAdminLogin
(
@username nvarchar(20),
@password char(32),
@lastloginip char(15),
@output int output
)
AS
if exists(Select id from admin where username=@username and password=@password)
begin
update admin set lastLoginIP=@lastloginip,lastlogintime=getdate() where username=@username
set @output=0 --验证通过
end
else
begin
set @output=-1 --帐号密码不正确
end
GO
-----------
//
//Alter
Alter procedure Pro_procedureName
as
[SQL statements]
//Drop
Drop procedure pro_ProcedureName
Create procedure P_XXX
with encryption
as
[SQL statements]
go
Procedure的好处:
系统有预编译 即编译1次,大大提高效益
某些业务规则可在procedure里面完成, 方便修改(不需改源前后台程序)
当然,还有我才知道的加密--安全性提高 Encryption
SQL-server还提供系统Procedure(sp_)和扩展Procedure(xp_)
利用系统存储过程新添用户的例子:
EXEC sp_addlogin 'LALA_ConnectName','PWD','DB_Name'
go
----
相对也明白了,具有SA权限后 利用扩展Procedure可以操作windows命令行解释器,
Use Master [<--Master数据库是 SQL-server存放系统procedure的数据库]
go
EXEC xp_cmdShell 'dir C:\*.exe'
这个XP_CmdShell就是在SQLserver系统执行命令行的方法
估计前段时间Hack最多的SQL injected,多半是成功注入后利用这里进行下一步权限探讨..
大概的 , Procedure掌握的就这些 需要反复练习 :)
posted @ 2006-01-10 03:59 Royman.Chen 阅读(92) 评论(0) 编辑
2006年1月9日 #
use DB_name
go
//Create
//
create view v_ViewName
as
select Column_Name_1 '别名_1', Column_Name_2 '别名_2' from t1,t2
where t1.XXid=t2.XXid and t2.yyId=t1.yyId
Group by Column_nameX
//Alter
Alter view v_ViewName
as
[select _statements
]
//Drop
Drop view v_ViewName
//查看指定视图sql语句信息
Exec sp_helptext 'v_ViewName'
//查看指定视图的基表信息等
Exec sp_depends 'v_ViewName'
PS: View不支持Order by ,but Group by ...
posted @ 2006-01-09 08:28 Royman.Chen 阅读(62) 评论(0) 编辑
27/12/05 by Royman
posted @ 2006-01-09 07:23 Royman.Chen 阅读(93) 评论(0) 编辑
27/12/05 by Royman
posted @ 2006-01-09 07:21 Royman.Chen 阅读(48) 评论(0) 编辑
Presentation : like HTML / XML , CSS or JS for web
Business Tier : security (like SQL injured), business logic control , validation (like regular expressions) ...
Data Access Tier : supply some interfaces between BT and DB (like decide to use procedure or SQL connecting string in the programme,use ado.net code to reat/write DB, to control the BT table object or class, CRUD-- create,read,update,delete )
DB Tier : Manage the database, write the procedure in order to improve the performance.
by Royman 29/12/05
posted @ 2006-01-09 07:18 Royman.Chen 阅读(62) 评论(0) 编辑
288RMB = = D-Link DI-624+A 802.11b/g 2.4GHz Wifi Router
Of course need the password to access my apartment Wifi service!!!![]()
posted @ 2006-01-09 07:16 Royman.Chen 阅读(154) 评论(0) 编辑
2006年1月8日 #
规则与check不同于:
可使用于多列,但每列只允许定义一次
check只用于一列 但一列可定义多个check
可用系统控制面板建立规则 用@x 来定义规则文本
Function:
例
//定义相减的两列值函数 求Remain
CREATE FUNCTION CalcRemain (@x decimal(6,0),@y decimal(6,0))
RETURNS decimal (6,0) AS
BEGIN
return (@x-@y)
END 规则和函数都必须要绑定列才能生效
Function 绑定:
Alter table Table_name
ADD RemainNum [<-This is the Column name I make]
AS
dbo.CalcRemain(Column_1,Column_2)[数据类型应保持一致] 
go
-----
测试
-----
Select * from Table_name
--返回RemainNum的结果就是Column_1 - Column_2posted @ 2006-01-08 07:45 Royman.Chen 阅读(72) 评论(0) 编辑