Transact_sql:
TRUNCATE TABLE test //用于刪除test表中的數(shù)據(jù)
CREATE FUNCTION //創(chuàng)建用戶自定義函數(shù)
ALTER FUNCTION //語句修改
DROP FUNCTION //除去用戶定義函數(shù)
CREATE UNIQUE CLUSTERED INDEX client_id ON clients(client_id)
sql:
(1). group by ... with cube having sum(number)>=40
下面兩語句是為了保持前后兼容
compute
compute by
如果使用COMPUTE BY,則必須也使用ORDER BY子句.
(2). JOIN ON 用法
select book.name,tool.name from book JOIN tool ON (boo.id==tool.id)
INNER JOIN ON (內(nèi)聯(lián)接:用于消除與另一個(gè)表中的任何不匹配的行)
select book.name,tool.name from book JOIN tool ON (boo.id==tool.id)
LEFT OUTER JOIN ON (左外聯(lián)接:)
FULL OUTER JOIN (完整外部聯(lián)接:不管另一個(gè)表是否有匹配的值,此運(yùn)算符都包括兩個(gè)表中所有行)
CROSS JOIN (交叉連接:顯示可能的組合)
(3). 使用@@ERROR全局變量處理錯(cuò)誤:
SQL Server的所有錯(cuò)誤都存儲(chǔ)在系統(tǒng)表master.dbo.sysmessages中.用戶定義的消息也可以存儲(chǔ)在sysmessages中.如果需要,可以使用RAISERROR語句將這些用戶定義的錯(cuò)誤返回到一個(gè)應(yīng)用程序.若出現(xiàn)一個(gè)錯(cuò)誤,則返回一條錯(cuò)誤信息.
例子: use test
drop table text1
go
create table text1(c1 int,c2 text)
exec sp_tableoption 'text1','text in row','on'
insert text1 values(1,'This is a text.')
go
select * from text1
go
(4). 檢索ntext,text,image
首先看例題: use test
go
create table text1(c1 int,c2 text)
exec sp_tableoption 'text1','text in row','on'
insert text1 values('1','This is a text.')
go
select * from text1
go
有幾種方法:
1.在SELECT語句中引用該列
2.使用TEXTTPTR函數(shù)
3.使用SUBTRING函數(shù)
4.使用PATINDEX函數(shù)
例題:
a. exec sp_tableoption 'text1','text in row','off'
declare @ptrval varbinary(16)
select @ptrval=TEXTPTR(c2)
from text1
READTEXT text1.c2 @ptrval 0 7
b. select substring(c2,1,7) as c2
from text1
c. use text
go
select patindex('%a%',c2) as 起始位置
from text1
go
(5).修改ntext,text或image值
可以使用下面的幾種方式來修改:
1. 使用數(shù)據(jù)庫API函數(shù);
2. 使用WRITETEXT語句重寫該列的整個(gè)數(shù)據(jù)值;
3. 使用UPDATETEXT語句更新ntext,text或image列的特定數(shù)據(jù)塊.
example:
1. use test
go
declare @ptrval varbinary(16)
exec sp_tableoption 'text1','text in row','off'
select @ptrval=textptr(c2) from text1
writetext text1.c2 @ptrval 'This is a modified text.'
go
select * from text1
go
2. use text
go
declare @ptrval varbinary(16)
select @ptrval=textptr(c2)
from text1
updatetext text1.c2 @ptrval NULL 0 'this is an inserted text.'
go
select * from text1
go
(6).顯示事務(wù):需要顯示的定義事務(wù)的啟動(dòng)和結(jié)束.它是通過BEGIN TRANSACTION,COMMIT TRANSACTION,COMMIT WORK,ROLLBACK TRANSACTION,ROLLBACK WORK 等Transact-SQL語句來完成的.
1.啟動(dòng)事務(wù)使用BEGIN TRANSACTION語句.如果遇到錯(cuò)誤,以后的語句能自動(dòng)回滾.
2.結(jié)束事務(wù)使用COMMIT TRANSACTION語句.該事務(wù)中的所有修改都將永久有效.事務(wù)占用的資源被釋放.
3.回滾事務(wù)使用ROLLBACK TRANSACTION清除自事務(wù)的起點(diǎn)或到某個(gè)保存點(diǎn)所有數(shù)據(jù)修改.ROLLBACK不釋放由事務(wù)控制的資源.
4.保存點(diǎn)使用SAVE TRANSACTION語句.如果有條件地取消事務(wù)的一部分,事務(wù)可以返回的位置.
example:
use bookdb
go
begin tran MyTran --啟動(dòng)事務(wù)
insert into book values(9,'Windows 2000 Professional 看圖速成',1,35,'2')
save tran MySave --保存點(diǎn)
delete book where book_id=9 --刪除記錄
rollback tran MySave --回滾事務(wù)
commit tran
go
select * from book
5. 標(biāo)記事務(wù)用WITH MARK選項(xiàng)使事務(wù)名置于事務(wù)日志中.將數(shù)據(jù)庫還原到早期狀態(tài)時(shí),可使用標(biāo)記事務(wù)替代日期和時(shí)間.
(7).自動(dòng)提交事務(wù)
(8).自定義鎖
自定義鎖超時(shí):
set lock_timeout 1800
go
declare @timeout int
select @timeout=@@lock_timeout
print @timeout
go
自定義隔離級(jí)別:
use bookdb
go
set transaction isolation level serializable
go
begin transaction
select *from book
go
dbcc useroptions--(顯示當(dāng)隔離級(jí)別)
go
鎖定提示:
use bookdb
go
set transaction isolation level serializable
go
begin transaction
select book_name from book with (nolock)
exec sp_lock --(顯示鎖定)
go
返回?cái)?shù)據(jù)庫被鎖定對(duì)象
select object_name(725577623)
go
(9)游標(biāo)
SQL Server支持四種API服務(wù)器游標(biāo):
* 靜態(tài)游標(biāo)
* 動(dòng)態(tài)游標(biāo)
* 只進(jìn)游標(biāo)
* 鍵集驅(qū)動(dòng)游標(biāo)
ROWCOUNT返回上次操作影響的行數(shù).
use bookdb
go
set nocount on
update book set book_name='LINUX教程'
where book_id=20
if @@ROWCOUNT=0
print '沒有行被更新!'
go
游標(biāo)示例:
--聲明游標(biāo)
declare book_cursor cursor
for select *from book
open book_cursor
--提取一行數(shù)據(jù)
fetch next from book_cursor
close book_cursor
deallocate book_cursor
(10)視圖
use bookdb
go
create view dbo.book_total
as
select abo.book.book_name as 書名,
abo.book.price as 價(jià)格,
abo.orderform.book_number as 數(shù)量,
abo.book.price*abo.orderform.book_number
as 總額
from abo.orderform inner join dbo.book on dbo.orderform.book_id=abo.book.book_id
go
通過視圖修改數(shù)據(jù)
use test
go
/*如果表Table1存在,則刪除*/
if exists(select table_name from information_schema.tables
where table_name='Table1')
drop table Table1
go
/*如果視圖View1存在,則刪除*/
if exists(select table_name from information_schema.views
where table_name='View1')
drop view View1
go
/*創(chuàng)建表Table1*/
create table Table1(column_1 int,column_2 varchar(30))
go
/*創(chuàng)建視圖View1*/
create view View1 as select column_2,column_1
from Table1
go
/*通過視圖View1插入一筆記錄*/
insert into View1 values('Row1',1)
/*檢查結(jié)果*/
select * from Table1
聯(lián)系客服