22,210
社区成员
发帖
与我相关
我的任务
分享
/****** Object: Table [dbo].[ttt] Script Date: 2008-12-15 17:11:25 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ttt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ttt]
GO
/****** Object: Table [dbo].[ttt] Script Date: 2008-12-15 17:11:26 ******/
CREATE TABLE [dbo].[ttt] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[time] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ttt] ADD
CONSTRAINT [PK_ttt] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
insert into ttt(name,time) values('logan',getdate());
insert into ttt(name,time) values('peter',getdate());
insert into ttt(name,time) values('man',getdate());
insert into ttt(name,time) values('lida',getdate());
select * from ttt
delete from ttt where name='peter'
insert into ttt(name,time) values('peter',getdate());
select * from ttt
结果(一)如下:
1 logan 2008-12-15 17:14:02.840
2 peter 2008-12-15 17:14:02.840
3 man 2008-12-15 17:14:02.840
4 lida 2008-12-15 17:14:02.857
结果(二)如下:
1 logan 2008-12-15 17:14:02.840
3 man 2008-12-15 17:14:02.840
4 lida 2008-12-15 17:14:02.857
5 peter 2008-12-15 17:14:02.857
--完全沒必要連續,需要得時候生成一列新的即可
if object_id('tempdb..#')is not null drop table #
go
create table #(ID int identity,[Name] varchar(10))
insert # select 'A' union all
select 'B' union all
select 'C'
select * from #
delete # where ID=2
insert # select 'E'
select * from #
--生成新的列IDNEW
select ID=(select count(*)+1 from # where ID<t.ID),* from # t order by ID
/*
(影響 3 個資料列)
ID Name
----------- ----------
1 A
2 B
3 C
(影響 3 個資料列)
(影響 1 個資料列)
(影響 1 個資料列)
ID Name
----------- ----------
1 A
4 E
3 C
(影響 3 個資料列)
ID ID Name
----------- ----------- ----------
1 1 A
2 3 C
3 4 E*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ttt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ttt]
GO
/****** Object: Table [dbo].[ttt] Script Date: 2008-12-15 17:11:26 ******/
CREATE TABLE [dbo].[ttt] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[time] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ttt] ADD
CONSTRAINT [PK_ttt] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
insert into ttt(name,time) values('logan',getdate());
insert into ttt(name,time) values('peter',getdate());
insert into ttt(name,time) values('man',getdate());
insert into ttt(name,time) values('lida',getdate());
insert into ttt(name,time) values('fcuandy',getdate());
select * from ttt
/*
1 logan 2008-12-15 17:36:37.780
2 peter 2008-12-15 17:36:37.780
3 man 2008-12-15 17:36:37.780
4 lida 2008-12-15 17:36:37.780
5 fcuandy 2008-12-15 17:36:37.793
*/
GO
CREATE TRIGGER tr ON ttt
INSTEAD OF INSERT
AS
SET IDENTITY_INSERT ttt ON
DECLARE @n INT
SELECT @n=MAX(id) FROM ttt
;WITH fc AS
(
SELECT n=1
UNION ALL
SELECT n=n+1 FROM fc WHERE n<@n
),fc1 AS
(
SELECT n FROM fc a
LEFT JOIN ttt b
ON a.n = b.id
WHERE b.id IS NULL
)
INSERT ttt(id,name,time) SELECT n,name,time
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY GETDATE()) idx,n FROM fc1) a
INNER JOIN
(
SELECT idx=ROW_NUMBER() OVER (ORDER BY GETDATE()),name,time FROM inserted
) b
ON a.idx=b.idx
DECLARE @r INT
SELECT @r=@@ROWCOUNT
SET IDENTITY_INSERT ttt OFF
INSERT ttt(name,time) SELECT name,time FROM
(
SELECT idx=ROW_NUMBER() OVER (ORDER BY GETDATE()),name,time FROM inserted
) x
WHERE idx>@r
GO
DELETE FROM ttt WHERE name = 'peter' OR name='lida'
GO
INSERT ttt SELECT 'xxx',getdate()
INSERT ttt SELECT 'yyy',GETDATE()
GO
SELECT * FROM ttt
/*
1 logan 2008-12-15 17:37:20.967
2 xxx 2008-12-15 17:37:21.013
3 man 2008-12-15 17:37:20.967
4 yyy 2008-12-15 17:37:21.030
5 fcuandy 2008-12-15 17:37:20.967
*/
DELETE FROM ttt WHERE name ='xxx' OR name='yyy'
INSERT ttt SELECT 'roy_88',GETDATE() UNION ALL SELECT 'limpire',GETDATE() UNION ALL SELECT '熊',GETDATE()
SELECT * FROM ttt
/*
1 logan 2008-12-15 17:38:29.450
2 roy_88 2008-12-15 17:38:29.530
3 man 2008-12-15 17:38:29.467
4 limpire 2008-12-15 17:38:29.530
5 fcuandy 2008-12-15 17:38:29.467
6 熊 2008-12-15 17:38:29.530
*/
表jh03有下列数据:
name score
aa 99
bb 56
cc 56
dd 77
ee 78
ff 76
gg 78
ff 50
1. 名次生成方式1,Score重复时合并名次
SELECT * , Place=(SELECT COUNT(DISTINCT Score) FROM jh03 WHERE Score >= a.Score)
FROM jh03 a
ORDER BY Place
结果
Name Score Place
---------------- ----------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 3
ff 76.00 4
bb 56.00 5
cc 56.00 5
ff 50.00 6
2. 名次生成方式2 , Score重复时保留名次空缺
SELECT * , Place=(SELECT COUNT(Score) FROM jh03 WHERE Score > a.Score) + 1
FROM jh03 a
ORDER BY Place
结果
Name Score Place
--------------- ----------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 4
ff 76.00 5
bb 56.00 6
cc 56.00 6
ff 50.00 8
SET IDENTITY_INSERT 表名 ON