Transact SQL基本的資料訪問

2021-04-07 06:57:46 字數 1928 閱讀 1452

1、表中列的使用技法如下:

1) 選擇所有列

可以使用(*)來表示表中所有列

2) 選擇部分列

use pubs

select au_lname,au_fname,phone,address from authors

3) 為字段設定別名

use pubs

select title 「書名」,price 「**」 from tiltles  /*替代名帶雙引號*/

select title 書名,price ** from titles  /*替代名不帶引號*/

select title 『書名』,price 『**』 from titles  /*替代名帶單引號*/

select title as 書名,price as ** from titles  /*使用as關鍵字*/

4) 在選擇列表中使用表示式

select title as 書名, price as 原價, price*0.5 as 現價 from titles

5) 消除字段資料的重複項

select distinct type as 分類from titles

6) 限制記錄的行數

使用top關鍵字

select top n [percent] * from 表名

使用set rowcount語句

set rowcount 4

select * from authors

select * from titles

set rowcount 0

select * from authors

select * from titles

2、from子句使用的技法如下:

1) 指定乙個或多個表

use pubs

select * from titles

use pubs/*錯誤*/

select * from titles and authors

2) 在多個表之間聯結

select titles.title, titleauthor,au_ord,authors.au_lname,

titles.price,titles.ytd_sales.titles.pub_id

form authors inner join

titleauthor on authors.author.au_id = titleauthor.au_id inner join

titles on titleauthor.titles_id = titles.titles_id

3)  使用派生表

3、條件子查詢

1) 條件子句

use pubs

select stores.stor_id as 書店編號, stores.stor_name as 書店名稱

from stores,(select stored, count(distinct title_id) as title_count

from sales group by stor_id) as stored

where stores.stor_id = stored.stor_id and stor_id.title_count =

(select count(*) from titles)

2) 使用比較運算子

select title_id as 書號,title as 書名,price as ** from titles

where price>15

3) 使用邏輯運算子

4) 使用between搜尋條件

5) 使用in列表搜尋條件

6) 使用like匹配模式

7) 空值比較搜尋條件

4、排序和分組查詢技法如下:

1) 排序查詢技法

2) 資料記錄分組技法

二〇〇六年五月二十三日

Transact SQL解析和基本的實用語句

sql語言 ddl 資料定義語句 dml 資料操作語句 dcl 資料控制語句 ddl 資料定義 操作物件 操作方式 建立刪除 修改模式 create schema drop schema 表create table drop table alter table 檢視create view drop ...

Transact SQL的游標例項

定義authors表的游標 declare author cursor cursor for select au id,au fname,au lname from authors 宣告變數 declare au id varchar 100 au fname varchar 100 au lnam...

Transact SQL語句遍歷結果集的三種方法

transact sql語句是可以實現遍歷的,有三種方法使用可以通過使用transact sql語句遍歷乙個結果集。下面就為您詳細介紹transact sql語句遍歷結果集的幾種方法,供您參考。第一種方法是使用temp表。使用這種方法您建立的初始的select語句的 快照 並將其用作基礎 指標 例如...