易截截图软件、单文件、免安装、纯绿色、仅160KB

MSSQL自增列重複與不連續

--> Title  : 自增列重複與不連續
--> Author : wufeng4552
--> Date   : 2009-11-13 08:31:12
 
--自增列通常在以下幾種情況而導致不連續
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--1插入失敗自動回滾
insert tb([name]) select 'A'
insert tb([name]) select 'A'
/*
錯誤原因
訊息2627,層級14,狀態1,行4
違反UNIQUE KEY 條件約束'UQ__tb__6B79F03D'。無法在物件'dbo.tb' 中插入重複的索引鍵。
陳述式已經結束。
*/
insert tb([name]) select 'B'
select * from tb
-->查詢結果
/*
(1 個資料列受到影響)
ID          name
----------- ----------
0           A
2           B
(2 個資料列受到影響)
*/
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--2手動回滾
insert tb([name]) select 'B'
begin tran
insert tb([name]) select 'A'
rollback tran
insert tb([name]) select 'C'
select * from tb
-->查詢結果
/*
ID          name
----------- ----------
0           B
2           C
(2 個資料列受到影響)
*/
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--3重置種子
insert tb([name]) select 'B'
dbcc checkident(tb,reseed,2)
insert tb([name]) select 'C'
insert tb([name]) select 'D'
select * from tb
-->查詢結果
/*
ID          name
-----------


相关文档:

基于mssql 百万级 数据 查询 优化 技巧三十则

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id ......

测试MSSQL中SQL语句执行花费的时间

select语句前加:
declare @d datetime
set @d=getdate()
并在select语句后加:
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
转自:动态网制作指南 www.knowsky.com
这是简易的查看执行时间的方法。
===========================================(一下内容转自:CSDN)
MSSQL Server中通过查 ......

mssql row_number() partition 使用方法理解

Sql2005中使用ow_number() partition进行分组实验,
SQL:
select * from stu
select id,row_number() over (partition by snm order by id) from stu
结果:
id      snm
----------------
111 111V
111 111W
222 222N
333 3123
444 3123
555 3123
666 3232
777 3232
--分组后的结果
id &n ......

php 调用mssql存储过程实例应用

php教程 调用mssql存储过程实例应用
用mssql_init语句用于初始化存储过程,而后调用mssql_bind语句指定存储过程参数,最后调用mssql_execute执行存储过程。
*/
//连接mssql数据库教程服务器
 $link = mssql_connect("127.0.0.1", "sa", "sa") or die("Can't connect sql server");
    mssql_sele ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号