t-sql 更新大批量数据

需求:有两张很大的表,写了一个存储过程处理(加工数据然后插入到一张新表中),因为一次性处理数据量很大,会导致tempdb等资源被耗尽,为了解决这个问题,每次输入两个参数,每次根据两个id的范围处理程序
1、建一张日志表

CREATE TABLE [dbo].[data_log](
	[start_time] [datetime] NULL DEFAULT (getdate()),
	[min_id] [int] NULL,
	[max_id] [int] NULL,
	[end_time] [datetime] NULL DEFAULT (getdate()
) 

2、编写处理过程

CREATE PROCEDURE [dbo].[insert_more](@min_data int,@max_data int)
as
begin
--开始处理
insert into data_log values(getdate(),@min_data,@max_data,getdate())
--处理程序
INSERT INTO test_1 
 SELECT t1.name,
           t2.name,
           t1.id
    FROM   t_1 t1,
           t_2 t2
    WHERE  t1.id = t2.id
    AND t1.id<=@max_data
    AND t1.id>@min_data
--结束处理    
update data_log set end_time=getdate() where min_id=@min_data and max_id=@max_data
end

3、生成批量执行该存储过程语句

CREATE PROCEDURE [dbo].[get_list](@min_id int,@max_id int,@mode int)
AS
declare @i int
declare @max_m int
begin
set @i=0
while @i<=(@max_id-@min_id)/@mode
begin
set @max_m=@min_id+(@i+1)*@mode
if @max_m>@max_id  
  set	@max_m=@max_id
print 'EXEC [dbo].[insert_more] @min_data = '+CAST(@min_id+@i*@mode as VARCHAR(50))+',@max_data = '+CAST(@max_m as VARCHAR(50))+';'
set @i=@i+1
END
end

利用3的过程,输入最小id,最大id,取值间隔,生成需要执行2过程的sql语句,执行这些语句完成操作,通过日志监控操作情况

创建sql server分区表

1、创建数据库

USE Master 
GO
CREATE DATABASE Test_Partitioning
ON PRIMARY
(NAME='Partitioning_1',
FILENAME=
'E:\database\partitions\Partitioning_1.mdf',
SIZE=4,
MAXSIZE=100,
FILEGROWTH=1 ),
FILEGROUP FG2
(NAME = 'Partitioning_2',
FILENAME =
'E:\database\partitions\Partitioning_2.mdf',
SIZE = 4,
MAXSIZE=100,
FILEGROWTH=1 ),
FILEGROUP FG3
(NAME = 'Partitioning_3',
FILENAME =
'E:\database\partitions\Partitioning_3.mdf',
SIZE = 4,
MAXSIZE=100,
FILEGROWTH=1 )
GO

2、创建分区函数

Use test_Partitioning 
GO
CREATE PARTITION FUNCTION salesYearPartitions (datetime)
AS RANGE RIGHT FOR VALUES ( '2009-01-01', '2010-01-01')
GO

说明:
RIGHT:表示”=”在右边
LEFT:表示”=”在左边

3、创建分区方案

Use test_Partitioning 
GO
CREATE PARTITION SCHEME Test_PartitionScheme
AS PARTITION salesYearPartitions
TO ([PRIMARY], FG2, FG3 )
GO

4、使用分区创建表

Use test_Partitioning 
GO
CREATE TABLE SalesArchival
(SaleTime datetime PRIMARY KEY,
ItemName varchar(50))
ON Test_PartitionScheme (SaleTime);
GO

5、验证SQL语句
5.1)确定文件组的数量和数据库数据文件的数量

Use test_Partitioning 
GO
-- Confirm Filegroups
SELECT name as [File Group Name]
FROM sys.filegroups
WHERE type = 'FG'
GO 
-- Confirm Datafiles
SELECT name as [DB File Name],physical_name as [DB File Path]
FROM sys.database_files
where type_desc = 'ROWS'
GO

5.2)验证分区表上的数据分布

Use test_Partitioning 
GO
select partition_id, index_id, partition_number, Rows
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='SalesArchival'
GO

关于日期sql语句

网友请求写几条关于日期的sql语句
1、查询两个日期天数
select trunc(to_date(to_date( ‘2004-3-20 ‘, ‘yyyy-mm-dd ‘)-to_date( ‘2004-3-25 ‘, ‘yyyy-mm-dd ‘),’mm’) ) from dual ;
–trunc函数不用也行,因为日期格式化就是到天

2、查询两个日期的月份
SELECT trunc(months_between(to_date( ‘2004-3-20 ‘, ‘yyyy-mm-dd ‘),SYSDATE)) FROM dual;
–根据需求是截断还是取近似值决定使用floor或者trunc

3、根据生日查询年龄
1)计算年龄(周岁)
select floor(months_BETWEEN(SYSDATE,to_date( ‘2004-4-25 ‘, ‘yyyy-mm-dd ‘))/12) FROM dual;
2)计算年龄(虚岁)
SELECT to_char(SYSDATE,’yyyy’)-to_char(to_date(‘2004-03-04′,’yyyy-mm-dd’),’yyyy’) FROM dual;

主要就是trunc(近似值)和floor(截断)函数使用

ora_rowscn

一、默认情况下
–创建t_orascn测试表
SQL> create table t_orascn(id number);

Table created

–插入两条数据
SQL> insert into t_orascn values(1);

1 row inserted

SQL> insert into t_orascn values(2);

1 row inserted

SQL> commit;

Commit complete

–查询ora_rowscn和相关数据
SQL> select ora_rowscn,id from t_orascn;

ORA_ROWSCN ID
———- ———-
559036 1
559036 2

–更新其中一条数据
SQL> update t_orascn set id=2 where rownum=1;

1 row updated

SQL> commit;

Commit complete

–再查询ora_rowscn和相关数据
SQL> select ora_rowscn,id from t_orascn;

ORA_ROWSCN ID
———- ———-
559669 2
559669 2

–查询更详细信息
SQL> Select versions_xid,versions_startscn,versions_endscn,
2 DECODE(versions_operation,’I’,’Insert’,’U’,’Update’,’D’,’Delete’, ‘Original’) “Operation”, id from t_orascn versions between scn minvalue and maxvalue;

VERSIONS_XID VERSIONS_STARTSCN VERSIONS_ENDSCN Operation ID
—————- —————– ————— ——— ———-
0500180055010000 559669 Update 2
02001C0068010000 559036 Insert 2
02001C0068010000 559036 559669 Insert 1

–查询操作时间
SQL> select to_char(scn_to_timestamp(ora_rowscn),’yyyy-mm-dd hh24:mi:ss’),id from t_orascn;

TO_CHAR(SCN_TO_TIMESTAMP(ORA_R ID
—————————— ———-
2011-04-11 00:01:12 2
2011-04-11 00:01:12 2

–查询数据详细操作时间
SQL> Select versions_xid,to_char(scn_to_timestamp(versions_startscn),’yyyy-mm-dd hh24:mi:ss’),versions_endscn,DECODE(versions_operation,’I’,’Insert’,’U’,’Update’,’D’,’Delete’, ‘Original’) “Operation”, id from t_orascn versions between scn minvalue and maxvalue;

VERSIONS_XID TO_CHAR(SCN_TO_TIMESTAMP(VERSI VERSIONS_ENDSCN Operation ID
—————- —————————— ————— ——— ———-
0500180055010000 2011-04-11 00:01:12 Update 2
02001C0068010000 2011-04-10 23:59:03 Insert 2
02001C0068010000 2011-04-10 23:59:03 559669 Insert 1

–结论:ora_rowscn在没有默认情况下,如果数据库块中的任何一条记录发生改变,该块中的所有记录的ora_rowscn中对应的scn值都改变

二、创建表含有rowdependencies测试
–创建测试表t_orascn_b
SQL> create table t_orascn_b(id number) rowdependencies;

Table created

SQL> insert into t_orascn_b values(1);

1 row inserted

SQL> insert into t_orascn_b values(2);

1 row inserted

SQL> commit;

Commit complete

SQL> select ora_rowscn,id from t_orascn_b;

ORA_ROWSCN ID
———- ———-
560532 1
560532 2

SQL> insert into t_orascn_b values(3);

1 row inserted

SQL> select ora_rowscn,id from t_orascn_b;

ORA_ROWSCN ID
———- ———-
560532 1
560532 2
3

SQL> commit;

Commit complete

SQL> select ora_rowscn,id from t_orascn_b;

ORA_ROWSCN ID
———- ———-
560532 1
560532 2
560555 3
–说明一点:没有提交ora_rowscn不改变(update)或者不存在(insert)

SQL> update t_orascn_b set id=10 where id<2; 1 row updated SQL> commit;

Commit complete

SQL> select to_char(scn_to_timestamp(ora_rowscn),’yyyy-mm-dd hh24:mi:ss’),id from t_orascn_b;

TO_CHAR(SCN_TO_TIMESTAMP(ORA_R ID
—————————— ———-
2011-04-11 00:15:38 10
2011-04-11 00:12:37 2
2011-04-11 00:13:28 3

SQL>
SQL> Select versions_xid,versions_startscn,versions_endscn,
2 DECODE(versions_operation,’I’,’Insert’,’U’,’Update’,’D’,’Delete’, ‘Original’) “Operation”, id from t_orascn_b versions between scn minvalue and maxvalue;

VERSIONS_XID VERSIONS_STARTSCN VERSIONS_ENDSCN Operation ID
—————- —————– ————— ——— ———-
0800290054010000 560614 Update 10
0500130056010000 560555 Insert 3
0A00090001010000 560532 Insert 2
0A00090001010000 560532 560614 Insert 1

SQL> Select versions_xid,to_char(scn_to_timestamp(versions_startscn),’yyyy-mm-dd hh24:mi:ss’),versions_endscn,DECODE(versions_operation,’I’,’Insert’,’U’,’Update’,’D’,’Delete’, ‘Original’) “Operation”, id from t_orascn_b versions between scn minvalue and maxvalue;

VERSIONS_XID TO_CHAR(SCN_TO_TIMESTAMP(VERSI VERSIONS_ENDSCN Operation ID
—————- —————————— ————— ——— ———-
0800290054010000 2011-04-11 00:15:38 Update 10
0500130056010000 2011-04-11 00:13:28 Insert 3
0A00090001010000 2011-04-11 00:12:37 Insert 2
0A00090001010000 2011-04-11 00:12:37 560614 Insert 1
–结论:如果创建表时指定了rowdependencies,则ora_rowscn是以行为单位变化,而不是块

sql server中查看对象

1、查询所有数据库
select * from sys.databases;

2、查询对象
select * from sysobjects;

3、复制表结构和数据
SELECT * INTO t_1 FROM master.dbo.spt_monitor;

4、查询视图
SELECT * FROM sys.views;

5、查询列
select * from sys.columns;

6、查询索引
select * from sys.indexes;