当前位置: 代码迷 >> Sql Server >> 初学者关于sql语句提问 多谢
  详细解决方案

初学者关于sql语句提问 多谢

热度:103   发布时间:2016-04-27 21:37:40.0
菜鸟关于sql语句提问 谢谢啊
表如下:
SenNum     int   不是键   容许空
A               vchar   不是键   容许空

比如说:
SenNum     A
1               我
2               天

操作:刷新时先看SenNum=2的A下有没有值,若有则插入新的一行,若没有则直接填入值。对于本例则插入一行,结果是:
SenNum     A
1               我
2               天
2               空

请问   Sql语句该咋写呢?谢谢大侠们

------解决方案--------------------
declare @s varchar(10)
select @s=isnull(A, '| ') from table where SenNum=2
if @s <> '| '
insert into table 2, 'value '
else
update table set a= 'value ' where SenNum=2


------解决方案--------------------
"刷新时先看SenNum=2 "

刷新時,是指什麼?往表中插入一條數據?

看樣子,好象是要寫一個觸發器.
------解决方案--------------------
create table #t6(SenNum int, A varchar(50))
insert #t6
select 1 , '我 ' union all
select 2 , '天 '


declare @SenNum int
declare @value varchar(50)
set @SenNum=2
set @value= '空 '
if (select count(1) from [#t6] where [email protected])> 0
begin
insert [#t6] select @SenNum, ' '[email protected]+ ' '
end
else
update [#t6] set a= ' '[email protected]+ ' ' where [email protected]

查看结果
select * from #t6
------解决方案--------------------
以上那个需要修改一下,下边这个可以满足lz所说的两种情况了。
drop table #t6
create table #t6(SenNum int, A varchar(50))
insert #t6
select 1 , '我 ' union all
select 2 , null


declare @SenNum int
declare @value varchar(50)
set @SenNum=2
set @value= '空 '
begin
if (select count(A) from [#t6] where [email protected])> 0
begin
insert [#t6] select @SenNum, ' '[email protected]+ ' '
end
else
begin
update [#t6] set a= ' '[email protected]+ ' ' where [email protected]
end
end

select * from #t6
-------------
1 我
2 空
  相关解决方案