常用 SQL指令碼 收集

2022-03-28 09:59:34 字數 2499 閱讀 5002

declare

@ttable(成績 int,學生 varchar(10

))insert

@tselect

100,'小張'

union

select

70 ,'小力'

union

select

80 ,'小剛'

union

select

78 ,'小王'

select

*,pid = (select

count(1) from

@twhere 成績 > a.成績)+

1from@ta

order

bypid

/*成績 學生 pid

----------- ---------- -----------

100 小張 1

80 小剛 2

78 小王 3

70 小力 4

(所影響的行數為 4 行)

*/

select

*,dense_rank()over(order

by integraltotal desc) from ht_members

--

1.sql server查詢表的主外來鍵

select

外來鍵表id

=b.fkeyid ,

外來鍵表名稱

=object_name

(b.fkeyid) ,

外來鍵列id

=b.fkey ,

外來鍵列名

= (select name from syscolumns where colid = b.fkey and id =

b.fkeyid) ,

主鍵表id

=b.rkeyid ,

主鍵表名

=object_name

(b.rkeyid) ,

主鍵列id

=b.rkey ,

主鍵列名

= (select name from syscolumns where colid = b.rkey and id =

b.rkeyid) ,

級聯更新

=objectproperty(a.id,'

cnstisupdatecascade

') ,

級聯刪除

=objectproperty(a.id,'

cnstisdeletecascade')

from

sysobjects a

join sysforeignkeys b on a.id =

b.constid

join sysobjects c on a.parent_obj =

c.id

where a.xtype ='f

'and c.xtype ='u

'--2.sql 語句查詢外來鍵 以及對應的外來鍵表名、列名和引用的主表名、列名

;with

cte as(

select

object_name(constraint_object_id) constraint_name , object_name

(parent_object_id) table_name , c.name column_name

from

sys.foreign_key_columns fk

inner

join

sys.columns c

on fk.parent_object_id = c.object_id

and fk.parent_column_id =

c.column_id

)select c.constraint_name , c.table_name , c.column_name , object_name

(fk.referenced_object_id) referenced_table_name , sc.name referenced_column_name

from

cte c

inner

join

sys.foreign_key_columns fk

on c.constraint_name =

object_name

(fk.constraint_object_id)

inner

join

sys.columns sc

on fk.referenced_object_id = sc.object_id

and fk.referenced_column_id = sc.column_id

SQL 常用指令碼

select o.name as tablename,c.name as colname,k.keyno as sort from sysindexes i join sysindexkeys k on i.id k.id and i.indid k.indid join sysobjects o ...

常用SQL查詢指令碼

本文假定讀者已經對資料庫連線技術有所了解,因此只討論有關sql查詢命令的語法。表結構如下 ms access 2000 表名 usertable 欄位名 字段型別 userid 自動計數 長整型 username 文字 user 文字 createdate 日期 時間 預設值為now 一 用sele...

MySQL常用SQL指令碼

1.重新定義表中某個欄位的屬性 alter table user modify column name varchar 200 comment 姓名 2.給表新增列欄位 alter table user add column varchar 200 comment 性別 3.刪除表中的乙個列欄位 a...