此功能经常用到,现在贴出代码,希望对有需要的能提到供点帮助吧。
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_Users for delete as begin delete tb_UserLogin from tb_UserLogin a,deleted d where a.UserID = d.UserID end
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