求高手指点指点

tianxiao110 2007-09-25 08:43:53
我现在有两个表如table_A,table_B
table_A中有数据如下:
ta_id ta_1 ta_2 tb_id
1 123 234
2 456 410
table_B中没有数据,但table_B表中的主键是表table_A的外键,
tb_id tb_1 tb_2

问题是:我们在table_A中查询ta_1和ta_2的值,如果有就录入table_B的值,并把
tb_id的值自动添入table_A中,如果没有就提示‘没有此值!’等字样。
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
elvis_gao 2007-09-28
  • 打赏
  • 举报
回复
declare @ta_1 int
declare @ta_2 int
declare @tb_id int
set @ta_1=456
set @ta_2=410

if exists (select 1 from table_A where ta_1=@ta_1 and ta_2=@ta_2)
begin
set @tb_id=(select tb_id from table_A where ta_1=@ta_1 and ta_2=@ta_2)
insert into table_B(tb_1,tb_2) values(@ta_1,@ta_2)
set @tb_id=@@identity

update table_A set tb_id=@tb_id where ta_1=@ta_1 and ta_2=@ta_2
end
else
print '没有此值!'
Limpire 2007-09-26
  • 打赏
  • 举报
回复
table_A中有数据如下:
ta_id ta_1 ta_2 tb_id
1 123 234
2 456 410
table_B中没有数据,但table_B表中的主键是表table_A的外键,
tb_id tb_1 tb_2

问题是:我们在table_A中查询ta_1和ta_2的值,如果有就录入table_B的值,并把
tb_id的值自动添入table_A中,如果没有就提示‘没有此值!’等字样。
----------------------------------------------------------------
declare @ta_1 int
declare @ta_2 int
declare @tb_id int
set @ta_1=456
set @ta_2=410

if exists (select 1 from table_A where ta_1=@ta_1 and ta_2=@ta_2)
begin
insert into table_B(tb_1,tb_2) values(@ta_1,@ta_2)
set @tb_id=scope_identity() --如此设计表tb_id应该是标识列
update table_A set tb_id=@tb_id where ta_1=@ta_1 and ta_2=@ta_2
end
else
print '没有此值!'
dawugui 2007-09-25
  • 打赏
  • 举报
回复
--加上table_a中tb_id的值.
ta_id ta_1 ta_2 tb_id
1 123 234 1
2 456 410 2
table_B中没有数据,但table_B表中的主键是表table_A的外键,
tb_id tb_1 tb_2
问题是:我们在table_A中查询ta_1和ta_2的值,如果有就录入table_B的值,并把
tb_id的值自动添入table_A中,如果没有就提示‘没有此值!’等字样。
------------------------------------------------------------------------------------
declare @ta_1 as int
declare @ta_2 as int
set @ta_1 = 123
set @ta_2 = 234

if exists (select * from table_a where ta_1 = @ta_1 and ta_2 = @ta_2)
insert into table_b select ta_1,ta_2,tb_id where ta_1 = @ta_1 and ta_2 = @ta_2
else
print '没有此值!'

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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