我要做的是,从表A中提取一部分信息(这里按时间取)插入到表B中(表A和表B字段是相同的)。
举个例子:
表A和表B都有3个字段—> ID Name Time。表A是有信息的而表B是空的。
打开表A时看到的信息如下:
ID Name Time
1 名字1 2010-3-7 15:24:32
2 名字2 2010-3-7 15:33:19
3 名字3 2010-3-7 15:59:22
4 名字4 2010-3-7 16:7:28
5 名字5 2010-3-7 16:10:12
.
..
...
20000 名字20000 2011-8-1 23:32:18我现在要做的事是将A表中时间大于15:33:19的所有信息都插入到表B中。也就是说除了前两行数据,其它的信息全部发送给表B。希望高手最好能给出代码。
感激不尽!!!!!!
举个例子:
表A和表B都有3个字段—> ID Name Time。表A是有信息的而表B是空的。
打开表A时看到的信息如下:
ID Name Time
1 名字1 2010-3-7 15:24:32
2 名字2 2010-3-7 15:33:19
3 名字3 2010-3-7 15:59:22
4 名字4 2010-3-7 16:7:28
5 名字5 2010-3-7 16:10:12
.
..
...
20000 名字20000 2011-8-1 23:32:18我现在要做的事是将A表中时间大于15:33:19的所有信息都插入到表B中。也就是说除了前两行数据,其它的信息全部发送给表B。希望高手最好能给出代码。
感激不尽!!!!!!
没记错的话好像就是这样
这样只能生成新的B表,如果B表已存在就会报错
where time> '2010-3-7 15:33:19'
否则就使用substring切换获取时间比较大小
if cast(’15:33:19' as datetime )<cast(‘15:33:19' as datetime )
print '小'
else
print '大'
select * from a
where CONVERT(VACHAR(8),time,108) > '15:33:19'
--------
20:36:38(所影响的行数为 1 行)
*/上面VARCHAR打错了,晕
-- Author :SQL77(只为思齐老)
-- Date :2010-03-07 20:37:42
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#tb1
if object_id('tempdb.dbo.#tb1') is not null drop table #tb1
go
create table #tb1([ID] int,[Name] varchar(9),[Time] datetime)
insert #tb1
select 1,'名字1','2010-3-7 15:24:32' union all
select 2,'名字2','2010-3-7 15:33:19' union all
select 3,'名字3','2010-3-7 15:59:22' union all
select 4,'名字4','2010-3-7 16:7:28' union all
select 5,'名字5','2010-3-7 16:10:12' union all
select 20000,'名字20000','2011-8-1 23:32:18'
--------------开始查询--------------------------select * INTO #T from #tb1 WHERE CONVERT(VARCHAR(8),TIME,108)>'15:33:19'SELECT * FROM #T
----------------结果----------------------------
/* (所影响的行数为 6 行)
(所影响的行数为 4 行)ID Name Time
----------- --------- ------------------------------------------------------
3 名字3 2010-03-07 15:59:22.000
4 名字4 2010-03-07 16:07:28.000
5 名字5 2010-03-07 16:10:12.000
20000 名字20000 2011-08-01 23:32:18.000(所影响的行数为 4 行)
*/
cmd.CommandText = string.Format("select * from sysobjects where name='B'");//判断表B是否存在?
SqlDataReader dr = cmd.ExecuteReader(); int a = 0;
while (dr.Read())
{
a = 1;
}
dr.Close(); if (a == 1)//如表B存在就删除
{
cmd.CommandText = string.Format("drop table Income");
cmd.ExecuteNonQuery();
}
cmd.CommandText = string.Format("select * into B from A where (Time>'15:33:19' )");
dr = cmd.ExecuteReader();
dr.Close();
cmd.CommandText = string.Format("select * from sysobjects where name='B'");//判断表B是否存在?
SqlDataReader dr = cmd.ExecuteReader(); int a = 0;
while (dr.Read())
{
a = 1;
}
dr.Close(); if (a == 1)//如表B存在就删除
{
cmd.CommandText = string.Format("drop table B");
cmd.ExecuteNonQuery();
}
cmd.CommandText = string.Format("select * into B from A where (Time>'15:33:19' )");
dr = cmd.ExecuteReader();
dr.Close();