日期:2014-05-17  浏览次数:20456 次

面试题真不会.
表A
ID (int)  name (varchar(50))

ID   name
 1    中国
 2    美国

表B
ID (int)    AID (int) 
ID  AID
1   1
2   2
3   1

表C  
ID (int)  BID(int)   SSS(int) BBB(int)

ID   BID   SSS  BBB
1     1     3     0
2     2     3     0
3     1     2     0
4     1     3     0
5     2     3     0
6     1     9     0

求一个存储过程
exec '美国'
实现 表C中MAX(sss)对应的BBB更新为2
面试题

------解决方案--------------------

declare @id int=0;
select @id =Id from 表A where name='美国'

update 表C
set BBB=@id
where sss=(select MAX(sss) from 表C)

------解决方案--------------------


if object_id('Tempdb..#a') is not null drop table #a
if object_id('Tempdb..#b') is not null drop table #b
if object_id('Tempdb..#c') is not null drop table #c
create table #a(
 ID int not null,
 [name] nvarchar(100) null
)

create table #b(
 ID int   not null,
 [AID] int null
)

create table #c(
 ID int  not null,
 [BID] int null,
 [SSS] int null,
 [BBB] int null


Insert into #a
select 1,'中国' union all
select 2,'美国'