7、说明:in 的使用方法
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert [a]
select 1,'矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
select * from a where [入库时间] in( '2013-01-02', '2013-01-03')
/*
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
1 矿泉水 100 2013-01-02 00:00:00.000
2 方便面 60 2013-01-03 00:00:00.000
3 方便面 50 2013-01-03 00:00:00.000
(3 行受影响)
*/
select * from a where [入库时间] not in( '2013-01-02', '2013-01-03')
/*
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
4 矿泉水 80 2013-01-04 00:00:00.000
5 方便面 50 2013-01-05 00:00:00.000
(2 行受影响)
*/ 8、说明:前3条记录
--> 测试数据:[a] if object_id('[a]') is not null drop table [a] go create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime) insert [a] select 1,'矿泉水',100,'2013-01-02' union all select 2,'方便面',60,'2013-01-03' union all select 3,'方便面',50,'2013-01-03' union all select 4,'矿泉水',80,'2013-01-04' union all select 5,'方便面',50,'2013-01-05' select top(3) * from a /* ID 品名 入库数量 入库时间 ----------- ------ ----------- ----------------------- 1 矿泉水 100 2013-01-02 00:00:00.000 2 方便面 60 2013-01-03 00:00:00.000 3 方便面 50 2013-01-03 00:00:00.000 (3 行受影响) */ 9、说明:随机取出3条数据
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert [a]
select 1,'矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
select top(3) * from a order by newid()
/*
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
5 方便面 50 2013-01-05 00:00:00.000
1 矿泉水 100 2013-01-02 00:00:00.000
4 矿泉水 80 2013-01-04 00:00:00.000
(3 行受影响)
*/
10、说明:列出数据库里所有的表名
select name from sysobjects where type='U' // U代表用户 11、说明:列出表里的所有的列名
select name from syscolumns where id=object_id('a')
/*
name
----------------------------
ID
品名
入库数量
入库时间
(4 行受影响)
*/
12、说明:初始化表a
TRUNCATE TABLE a
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert [a]
select 1,'