SQLServer存储过程中能关联到的数据更新,关联不到的插入或者删除如何实现?

C罗子 2017-01-13 07:58:33
SQLServer存储过程中能关联到的数据更新,关联不到的插入或者删除如何实现?
现在有Student表,Student_A表有学号,姓名,年龄,座位号,户籍。Student_B表有学号,姓名,年龄,座位号,户籍。
Student_A与Student_B结构相同。存储过程的一个输入参数是flag,当这个flag=‘0’时,并且Student_A表的学号,姓名,年龄,与Student_B表的学号,姓名,年龄都能关联上时,以Student_B表的座位号,户籍为准去更新Student_A表的座位号,户籍。当这个flag=‘0’时,并且Student_A表的学号,姓名,年龄,与Student_B表的学号,姓名,年龄关联不上时,将Student_B表关联不到的数据插入Student_A表。当这个flag=‘1’时,并且Student_A表的学号,姓名,年龄,与Student_B表的学号,姓名,年龄能关联不上时,删除Student_A在Student_B表关联不到的数据。如何写这样的存储过程?
...全文
294 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
C罗子 2017-01-15
  • 打赏
  • 举报
回复
引用 6 楼 Merry0101 的回复:
刚才测试了一下,符合你描述的要求

Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
    Set NoCount On
    If @Flag=0
    Begin
        Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B b on a.学号=b.学号
        and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is not null
 
        Insert Student_A(学号,姓名,年龄,座位号,户籍)
        Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a left outer join Student_A b on a.学号=b.学号
        and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
    End
    If @Flag=1
    Begin
        Delete a from Student_A a left  outer Join Student_B on a.学号=b.学号 and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
    End
End
update的时候,不可以where b.学号 is not null。如果不加exists匹配不到的项目就变成了全变更新成null.insert的时候是关联不到的时候才插入,不可以 where b.学号 is null。这个不对啊!
0与1之间 2017-01-14
  • 打赏
  • 举报
回复
刚才测试了一下,符合你描述的要求

Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
    Set NoCount On
    If @Flag=0
    Begin
        Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B b on a.学号=b.学号
        and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is not null
 
        Insert Student_A(学号,姓名,年龄,座位号,户籍)
        Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a left outer join Student_A b on a.学号=b.学号
        and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
    End
    If @Flag=1
    Begin
        Delete a from Student_A a left  outer Join Student_B on a.学号=b.学号 and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
    End
End
0与1之间 2017-01-14
  • 打赏
  • 举报
回复
引用 3 楼 xianying7509 的回复:
[quote=引用 2 楼 Merry0101 的回复:] 看看这样是否可行?

Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
	Set NoCount On
	If @Flag=0
	Begin
		Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is not null

		Insert Student_A(学号,姓名,年龄,座位号,户籍)
		Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a left outer join Student_A b on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
	End
	If @Flag=1
	Begin
		Delete a from Student_A a Join Student_B on a.学号=b.学号 and a.姓名=b.姓名 and  a.年龄=b.年龄 where a.学号 is null
	End
End
这样不行,where条件里应该写where exists和where not exists吧,还有insert的语法应该是insert into XXXX这样的吧?[/quote] 查有或没有不一定非得用 exists和not exists啊 插入表Insert 或Insert into 都一样呀
二月十六 版主 2017-01-14
  • 打赏
  • 举报
回复
Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
    Set NoCount On
    If @Flag=0
    Begin
        Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B b on a.学号=b.学号

 
        Insert Student_A(学号,姓名,年龄,座位号,户籍)
        Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a  where a.学号 NOT IN (SELECT 学号 FROM Student_A)
    End
    If @Flag=1
    Begin
        Delete a from Student_A a where a.学号 NOT IN (SELECT 学号 FROM Student_B)
    End
End
C罗子 2017-01-14
  • 打赏
  • 举报
回复
引用 2 楼 Merry0101 的回复:
看看这样是否可行?

Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
	Set NoCount On
	If @Flag=0
	Begin
		Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is not null

		Insert Student_A(学号,姓名,年龄,座位号,户籍)
		Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a left outer join Student_A b on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
	End
	If @Flag=1
	Begin
		Delete a from Student_A a Join Student_B on a.学号=b.学号 and a.姓名=b.姓名 and  a.年龄=b.年龄 where a.学号 is null
	End
End
这样不行,where条件里应该写where exists和where not exists吧,还有insert的语法应该是insert into XXXX这样的吧?
0与1之间 2017-01-14
  • 打赏
  • 举报
回复
看看这样是否可行?

Create proc [dbo].[Test]
@Flag nvarchar(1)=''
As 
Begin
	Set NoCount On
	If @Flag=0
	Begin
		Update a set a.座位号=b.座位号,a.户籍=b.户籍 from Student_A a Join Student_B on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is not null

		Insert Student_A(学号,姓名,年龄,座位号,户籍)
		Select a.学号,a.姓名,a.年龄,a.座位号,a.户籍 from Student_B a left outer join Student_A b on a.学号=b.学号
		and a.姓名=b.姓名 and  a.年龄=b.年龄 where b.学号 is null
	End
	If @Flag=1
	Begin
		Delete a from Student_A a Join Student_B on a.学号=b.学号 and a.姓名=b.姓名 and  a.年龄=b.年龄 where a.学号 is null
	End
End

34,837

社区成员

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

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