说明备份sql server 创建 备份数据的 device
USE master
EXEC sp_addumpdevice disk testBack c:\mssqlbackup\MyNwind_dat
开始 备份
BACKUP DATABASE pubs TO testBack
说明增加一个列
Alter table tabname add column col type
注列增加后将不能删除DB中列加上后数据类型也不能改变唯一能改变的是增加varchar类型的长度
说明添加主键 Alter table tabname add primary key(col)
说明删除主键 Alter table tabname drop primary key(col)
说明复制表(只复制结构源表名a 新表名b) (Access可用) 法一select * into b from a where <>
法二select top * into b from a
说明拷贝表(拷贝数据源表名a 目标表名b) (Access可用) insert into b(a b c) select def from b;
说明跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用) insert into b(a b c) select def from b in 具体数据库 where 条件
例子from b in &ServerMapPath()&\datamdb & where
说明显示文章提交人和最后回复时间
select atitleausernamebadddate from table a(select max(adddate) adddate from table where tabletitle=atitle) b
说明外连接查询(表名a 表名b)select aa ab ac bc bd bf from a LEFT OUT JOIN b ON aa = bc
说明在线视图查询(表名a )select * from (SELECT abc FROM a) T where ta > ;
说明between的用法between限制查询数据范围时包括了边界值not between不包括select * from table where time between time and time
select abc from table where a not between 数值 and 数值
说明日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff(minutef开始时间getdate())>
说明一条sql 语句搞定数据库分页
select top b* from (select top 主键字段排序字段 from 表名 order by 排序字段 desc) a表名 b where b主键字段 = a主键字段 order by a排序字段
说明前条记录
select top * form table where 范围
说明选择在每一组b值相同的数据中对应的a最大的记录的所有信息
select abc from tablename ta where a=(select max(a) from tablename tb where tbb=tab)
说明包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)
说明随机取出条数据
select top * from tablename order by newid()
说明随机选择记录
select newid()
说明删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by colcol)
说明列出数据库里所有的表名
select name from sysobjects where type=U
说明列出表里的所有的
select name from syscolumns where id=object_id(TableName)
[] [] []