34,594
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: [table1]
if object_id('[table1]') is not null drop table [table1]
go
create table [table1] (id int,weburl varchar(20))
insert into [table1]
select 1,'http://www.baidu.com' union all
select 2,'http://www.126.com' union all
select 3,'http://www.163.com' union all
select 6,'http://www.yahoo.com'
--> 测试数据: [table2]
if object_id('[table2]') is not null drop table [table2]
go
create table [table2] (id int,webip varchar(11),dt datetime,urlid varchar(100),bz varchar(1))
insert into [table2]
select 11,'192.168.1.1','2010-12-5','1,','H' union all
select 12,'192.168.1.2','2010-12-5','1,3,','M' union all
select 13,'192.168.1.6','2010-12-5','1,6,3,2,','H'
if object_id('pp') is not null drop proc pp
go
create proc pp
@ip varchar(50),
@dt datetime,
@bz varchar(100),
@return varchar(100) output
as
declare @s varchar(10)
if exists(select 1 from table2 where webip=@ip and dt=@dt )
-- 这里增加第3种情况,不过好像和第一种情况有重复的操作
if exists(select 1 from table1 a,table2 b where webip=@ip and dt=@dt and charindex(','+ltrim(a.id),','+b.urlid)=0)
begin
set @s=(select top 1 ltrim(a.id) from table1 a,table2 b where webip=@ip and dt=@dt and charindex(','+ltrim(a.id),','+b.urlid)=0 order by newid())
update table2
set urlid=urlid+@s+','
where webip=@ip and dt=@dt
select @return=weburl from table1 where id=cast(@s as int)
end
else
begin
select @return='0'
end
else
begin
set @s=(select top 1 ltrim( id) from table1 order by newid())
insert table2 select 0, @ip,@dt,@s+',',@bz -- id用0测试,实际自增列,不用插入此列值
select @return=weburl from table1 where id=cast(@s as int)
end
go
-- 1
declare @r varchar(100)
exec pp '192.168.1.1','2010-12-5','H',@r output
select @r
select * from [table2]
/*
----------------------------------------------------------------------------------------------------
http://www.163.com
(1 行受影响)
id webip dt urlid bz
----------- ----------- ----------------------- ---------------------------------------------------------------------------------------------------- ----
11 192.168.1.1 2010-12-05 00:00:00.000 1,3, H
12 192.168.1.2 2010-12-05 00:00:00.000 1,3, M
13 192.168.1.6 2010-12-05 00:00:00.000 1,6,3,2, H
(3 行受影响)
*/
-- 2
declare @r varchar(100)
exec pp '192.168.1.5','2010-12-5','M',@r output
select @r
select * from [table2]
/*
----------------------------------------------------------------------------------------------------
http://www.163.com
(1 行受影响)
id webip dt urlid bz
----------- ----------- ----------------------- ---------------------------------------------------------------------------------------------------- ----
11 192.168.1.1 2010-12-05 00:00:00.000 1, H
12 192.168.1.2 2010-12-05 00:00:00.000 1,3, M
13 192.168.1.6 2010-12-05 00:00:00.000 1,6,3,2, H
0 192.168.1.5 2010-12-05 00:00:00.000 3, M
(4 行受影响)
*/
-- 3
declare @r varchar(100)
exec pp '192.168.1.6','2010-12-5','H',@r output
select @r
select * from [table2]
/*
----------------------------------------------------------------------------------------------------
0
(1 行受影响)
id webip dt urlid bz
----------- ----------- ----------------------- ---------------------------------------------------------------------------------------------------- ----
11 192.168.1.1 2010-12-05 00:00:00.000 1, H
12 192.168.1.2 2010-12-05 00:00:00.000 1,3, M
13 192.168.1.6 2010-12-05 00:00:00.000 1,6,3,2, H
(3 行受影响)
*/