1. 程式人生 > >在SQLSERVER中建立檢視的一些注意事項

在SQLSERVER中建立檢視的一些注意事項

建立檢視 :


create view tableName

(列別名)可有可無

with Attribute

as

fullselect {}

with [checkOption]

eg.

create view view_card_tr
with encryption(進行檢視加密)
as
select top 10 cash as cc from card_transaction where substring(entry_site,0,3)='站點' order by id desc
with check option(進行檢視約束)

我們還可以採取以下措施為欄位起別名:

create view view_card_tr
(cash)
with encryption(進行檢視加密)
as
select  top 10 money from card_transaction where substring(entry_site,0,3)='站點' order by id desc
with check option(進行檢視約束)

NOTE

1.只有在建立檢視的語句中使用了Top關鍵字之後才可以使用order by 字句,否則就會出錯

2.當我們對檢視使用了約束以後,如果在對檢視進行操作的時,必須符合該約束條件,否則就不能夠正確地進行操作.

   但是我們可以直接對資料表進行操作.此時就不再受檢視約束的制約

3.在sqlserver2000中.我們不能夠使用Substr()來代替substring()函式.

4.我們使用substring()函式時.第一個欄位名不能加引號.否則不能正確執行

5.我們在使用substring()函式的時候需要注意的是:第一個引數是欄位的名稱不需要加引號,第二個引數是擷取字元 串的開始位置,第二個引數是擷取字串的結束位置。並且字串的位置是從0開始的

修改檢視的方法

 Alter view view_card_tr
with encryption(進行檢視加密)
as
select top 10 cash as cc from card_transaction where substring(entry_site,0,3)='站點' order by id desc
with check option(進行檢視約束)

刪除檢視的方法

drop view view_card_tr