擷取SQL資料庫中某個表中的所有欄位名稱的通用方法

來源:互聯網
上載者:User

此功能經常用到,現在貼出代碼,希望對有需要的能提到供點協助吧。

1、首先建立一個視圖,如下。

create view fielddesc as    select o.name as oname,            c.name as cname,           convert(varchar(30),p.value) as value,           p.smallid as psmallid,           t.name as tname from syscolumns c join systypes t            on c.xtype = t.xtype join sysobjects o            on o.id=c.id left join sysproperties p            on p.smallid=c.colid and p.id=o.id where o.xtype= 'U'; 

2、將你需要查詢的表名傳遞進來即可。

select * from fielddesc where oname = '你的表名';

3、在刪除一個表中資料時,串聯刪除另一個表中想對應的資料(含主外鍵關係)

create trigger tri_del on tb_Usersfor deleteasbegin   delete tb_UserLogin from tb_UserLogin a,deleted d where a.UserID = d.UserIDend 

4、SQL中根據名稱分組求最新一條資料的SQL語句:

select * from 預警記錄表 t where not exists(select 1 from 預警記錄表 where 雨量站名=t.雨量站名 and (預警時間>t.預警時間 or 預警時間=t.預警時間 and id>t.id))and CONVERT(datetime,預警時間,120) between dateadd(day,-3,getdate()) and getdate()order by  預警時間 desc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.