求一存储过程,请高手们赐教(本人以前未接触过存储过程)

chengfeng 2006-09-13 04:41:01
我需要执行的数据库操作步骤如下:
1、一个web页面带有uid和str两个参数,当用户访问的时候先
"select key from T1 where userid =" & uid
2、如果纪录存在比较str和rs("key"),如果str中包含字符串rs("key")
"select id from T2 where str = '" & str & "'"
3、如果T2表中纪录存在则
"update T2 set num=num+1, vtime=now() where str='" & str & "'"
如果T2标中纪录不存在则
"insert into T2(str,num,vtime) values('"&str&"',1,now())"
4、获得T2表中对应纪录的id字段值(自增量字段)在并T3表中插入访问纪录
"insert into T3(vid,vtime) values("&id&",now())"
...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
云中客 2006-09-13
  • 打赏
  • 举报
回复
执行存储过程的方法
exec test @uID=2,@str='test'

云中客 2006-09-13
  • 打赏
  • 举报
回复
Create Proc test
@uID int,
@str varchar(100)
As
Begin
if Exists(Select * From T1 Where userid=@uid and charindex([key],@str))
Begin
if Exists(Select * From T2 Where str=@str)
update T2 set num=num+1, vtime=getdate() where str=@str
else
insert into T2(str,num,vtime) values(@str,1,getdate())
insert into T3(vid,vtime) Select id,getdate() From T2 where str=@str
End
End

子陌红尘 2006-09-13
  • 打赏
  • 举报
回复
create procedure sp_test(@uid int,@str varchar(100))
as
begin
if exists(select 1 from T1 where userid=@uid and charindex([key],@str))
begin
if exists(select id from T2 where str=@str)
update T2 set num=num+1,vtime=getdate() where str=@str
else
insert into T2(str,num,vtime) values(@str,1,getdate())

insert into t3(vid,vtime) select id,getdate() from T2 where str=@str
end
end
go
tx1icenhe 2006-09-13
  • 打赏
  • 举报
回复
前端调用:

"exec intodata " & uid & "'" & str & "'"
tx1icenhe 2006-09-13
  • 打赏
  • 举报
回复
create proc intodata
@uid int,
@str varchar(200)
as
if not exists (select 1 from t1 where userid=uid and @str like '%'+key+'%')
return -1

declare @id int
select @id=id from T2 where str=@str
if @id is null
begin
insert into T2(str,num,vtime) values(@str,1,getdate())
select @id=SCOPE_IDENTITY()
else
update T2 set num=num+1, vtime=getdate()
where str=@str

insert into T3(vid,vtime) values(@id,getdate())

go

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧