T-SQL 常用语句

1. 查看 Table 或者 Column 被那些object(存储过程、函数或View)调用.

select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%tablename%'

2. 用其他表的字段数据更新表字段

update Table1

set Col1 = B.Col1, Col2 = B.Col2, Col3 = B.Col3

from Table1 A

join Table2 B on A.ID = B.ID

where A.Date = 20110329

3. 复制表结构

select * into b from a where 1=2

4. 两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

5. 随机取出N(10)条记录

select top 10 * from tablename order by newid()

6. 列出表里的所有的列名

select name from syscolumns where id=object_id('TableName')

7. 取最新version的一行数据

select A.* from table A

where A.version = (select max(B.version) from table B where A.ID = B.ID )

原文地址:https://www.cnblogs.com/bi-info/p/6554985.html