求一个SQL或者是存储过程

flyforlove 2006-09-28 02:46:19
先用一个字段的值去查询另一个字段。
然后判断是否有这条记录,
如果有的话,把查出的字段值,连带其他参数插入到另外一个表中。

为代码也就是

select id from table1 where name = @name
如果有返回值
假设id为@id
insert table2(id, addr, tel)values(@id, @addr, @tel)

...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghj303155066 2006-10-08
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3643/3643027.xml?temp=8.854312E-02
flyforlove 2006-09-29
  • 打赏
  • 举报
回复
谢谢大家
冷箫轻笛 2006-09-28
  • 打赏
  • 举报
回复

if exists(select 1 from table1 where name = @name)
insert into table2(id, addr, tel)
select id,@addr, @tel
from table1 where name = @name
子陌红尘 2006-09-28
  • 打赏
  • 举报
回复
insert table2(id, addr, tel)
select id,@addr,@tel from table1 where name=@name
xyxfly 2006-09-28
  • 打赏
  • 举报
回复
@addr从哪里来的?
ww3347 2006-09-28
  • 打赏
  • 举报
回复
create proc test
@name varchar(20),
@addr varchar(100),
@tel varchar(100)
as
declare @id int
select @id=id from table1 where name = @name
if (@id is not null)
insert table2(id, addr, tel)values(@id, @addr, @tel)
return 0
go

34,576

社区成员

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

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