自增表
- USE [li]
- GO
- /****** 对象: Table [dbo].[IDArray] 脚本日期: 05/11/2012 08:26:46 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[IDArray](
- [id] [int] NOT NULL,
- CONSTRAINT [PK_IDArray] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
结果表
- USE [li]
- GO
- /****** 对象: Table [dbo].[ResultRecord] 脚本日期: 05/11/2012 08:27:25 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[ResultRecord](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [ResultID] [int] NULL,
- [TableType] [nvarchar](40) NULL,
- [Keyword] [nvarchar](100) NULL,
- [KeywordType] [nvarchar](50) NULL,
- CONSTRAINT [pk_ResultRecord] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
查询的存储过程
- USE [li]
- GO
- /****** 对象: StoredProcedure [dbo].[CheckNoExist] 脚本日期: 05/11/2012 08:27:55 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER Procedure [dbo].[CheckNoExist]
- @Column varchar(50)='',
- @Table varchar(50)='',
- @Url varchar(200)=''
- As
- Set NoCount on
- Declare @ID int,@Number int,@IsEqual int
- exec('Declare IDCursor Cursor For Select top 1 '+@Column+' from '+@Table+' With(Nolock) Order by '+@Column+' desc')
- Open IDCursor
- set @Number=1
- Fetch Next From IDCursor Into @ID
- ----填充自增循环表---------------------
- if exists (select id from IDArray)
- exec('delete from IDArray')
- While @ID>=@Number
- Begin
- Insert IDArray(ID) values(''+@Number+'')
- set @Number=@Number+1
- End
- ----对比ID---------------------------
- exec('Insert UrlRecord(id,tname,url) select id,'''+@Table+''',REPLACE(REPLACE('''+@Url+''',''{ID}'',id),''{DIR}'',id/1000) from IDArray where id not in(select '+@Column+' from '+@Table+')')
- Close IDCursor
- deallocate IDCursor
存储过程调用示例
- EXEC CheckNoExist agent_info_id,'cnsbdata.dbo.agent_info','html/agent/{DIR}/agent_info_show_{ID}.html';
转载于:https://blog.51cto.com/497017/860455