☆ 獲取數據庫所有表名,表的所有列名
select name from sysobjects where xtype='u'
select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')
☆ 遞歸查詢數據
Sql語句裏的遞歸查詢 SqlServer2005和Oracle 兩個版本
|
以前使用Oracle,覺得它的遞歸查詢很好用,就研究了一下SqlServer,發現它也支持在Sql裏遞歸查詢 --下面的Sql是查詢出1結點的所有子結點 --下面的Sql是查詢出8結點的所有父結點 --下面是遞歸刪除1結點和所有子結點的語句: Oracle版本的Sql如下: --下面的Sql是查詢出8結點的所有父結點 今天幫別人做了一個有點意思的sql,也是用遞歸實現,具體如下: |
SQLserver2008使用表達式遞歸查詢
--由父項遞歸下級
with cte(id,parentid,text)
as
(--父項
select id,parentid,text from treeview where parentid = 450
union all
--遞歸結果集中的下級
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,parentid,text from cte
---------------------
--由子級遞歸父項
with cte(id,parentid,text)
as
(--下級父項
select id,parentid,text from treeview where id = 450
union all
--遞歸結果集中的父項
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,parentid,text from cte