我们如果从一张表中获取数据的时候,可能会有重复的情况,那么我们如何去重操作,我这里是用一张表获取最大id值来控制的。
在查询数据之前先获取最大的id值是多少,然后将id值传过去,作为查询条件之后,将最后一条id值作为最大id值进行保存,在下一次进行查询时使用。
sql文件
-- ---------------------------- -- Table structure for maxidsave -- ---------------------------- DROP TABLE [dbo].[maxidsave] GO CREATE TABLE [dbo].[maxidsave] ( [id] int NOT NULL IDENTITY(1,1) , [name] varchar(50) NULL , [tablename] varchar(50) NULL , [maxid] int NULL ) GO DBCC CHECKIDENT(N'[dbo].[maxidsave]', RESEED, 4) GO IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 'SCHEMA', N'dbo', 'TABLE', N'maxidsave', NULL, NULL)) > 0) EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'记录最大ID数、名称、表名' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' ELSE EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'记录最大ID数、名称、表名' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' GO IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 'SCHEMA', N'dbo', 'TABLE', N'maxidsave', 'COLUMN', N'name')) > 0) EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'名称、方法名、接口名等' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'name' ELSE EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'名称、方法名、接口名等' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'name' GO IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 'SCHEMA', N'dbo', 'TABLE', N'maxidsave', 'COLUMN', N'tablename')) > 0) EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'表名' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'tablename' ELSE EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'表名' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'tablename' GO IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description', 'SCHEMA', N'dbo', 'TABLE', N'maxidsave', 'COLUMN', N'maxid')) > 0) EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N'最大ID数' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'maxid' ELSE EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'最大ID数' , @level0type = 'SCHEMA', @level0name = N'dbo' , @level1type = 'TABLE', @level1name = N'maxidsave' , @level2type = 'COLUMN', @level2name = N'maxid' GO -- ---------------------------- -- Indexes structure for table maxidsave -- ---------------------------- -- ---------------------------- -- Primary Key structure for table maxidsave -- ---------------------------- ALTER TABLE [dbo].[maxidsave] ADD PRIMARY KEY ([id]) GO