原表:A
id NBR cnt
----------- ---------- ----------- ----------- -----------
1 1 20
1 2 30
1 3 20
2 1 20
2 2 20
如果传入参数 id = 1, @cnt1 = 11 , @cnt2 = 1, @cnt3=1,@cnt4 = 20 , @cnt5 = 0 则结果为
id NBR cnt
----------- ---------- ----------- ----------- -----------
1 1 11
1 2 1
1 3 1
2 1 20
2 2 20
1 4 20
1 5 0
如果传入参数 id = 1, @cnt1 = 11 , @cnt2 = 1, @cnt3=1,@cnt4 = 20, @cnt5 = 22则结果为
id NBR cnt
----------- ---------- ----------- ----------- -----------
1 1 11
1 2 1
1 3 1
2 1 20
2 2 20
1 4 20
1 5 22
传入参数的数目是固定的,就是id , @cnt1 , @cnt2 , @cnt3,@cnt4, @cnt5 这6个
12 个解决方案
#1
if (select * from tablename where ...)=0
insert into tablename(...) values(...)
else
update tablename set ... where ...
insert into tablename(...) values(...)
else
update tablename set ... where ...
#2
在存储过程里写逻辑
#3
仅供参考:
create table #temp
(
id int,
NBR int,
cnt int
)
insert #temp
select 1, 1, 20 union all
select 1, 2, 20 union all
select 1, 3, 20 union all
select 2, 1, 20 union all
select 2, 2, 20
go
--SQL:
declare
@id int,
@cnt1 int,
@cnt2 int,
@cnt3 int,
@cnt4 int,
@cnt5 int
select
@id = 1,
@cnt1 = 11 ,
@cnt2 = 1,
@cnt3=1,
@cnt4 = 20,
@cnt5 = 0
--SQL:
declare @table table
(
rowno int,
id int,
new_cnt int,
NBR int,
old_cnt int
)
insert @table
select a.rowno,a.id, new_cnt=a.cnt, b.NBR, old_cnt=b.cnt from
(
select rowno = ROW_NUMBER() over(order by getdate()), * FROM
(
select id = @id, cnt = @cnt1 union all
select id = @id, cnt = @cnt2 union all
select id = @id, cnt = @cnt3 union all
select id = @id, cnt = @cnt4 union all
select id = @id, cnt = @cnt5
)T
) a
LEFT JOIN #temp b
ON a.id = b.id and a.rowno = b.NBR
insert into #temp
select id, rowno, new_cnt from @table
where NBR is null and old_cnt is null
update A
set cnt = new_cnt
from #temp A
inner join
@table b
on a.id = b.id and a.NBR = b.rowno
GO
--RESULT:
select * from #temp
/*
id NBR cnt
1 1 11
1 2 1
1 3 1
2 1 20
2 2 20
1 4 20
1 5 0
*/
#4
在存储过程里一条一条判断:
if exists (select 1 from 表A where id=1 and NBR=1)
begin
update ....
end
else
begin
insert ....
end
后面4句一样
#5
多谢 wwwwgou, 但是我的是sql 2000.
另外,在存储过程里一条一条判断,5条还行,实际情况是nbr 到十多个。。。 那代码太多了。。。
Leftie 方法不行
另外,在存储过程里一条一条判断,5条还行,实际情况是nbr 到十多个。。。 那代码太多了。。。
Leftie 方法不行
#6
在线等,解决马上给分
#7
不定数量的话用动态语句
#8
定数量,就是11个,但是各位高手举例说明一下到5 就行了。
#9
UP
#10
定数量,就是11个,但是各位高手举例说明一下到5 就行了。
#11
create table #A (id int, NBR int, cnt int)
insert #A select 1 ,1 ,20 union all
select 1 ,2 ,30 union all
select 1 ,3, 20 union all
select 2 ,1, 20 union all
select 2 ,2, 20
create proc TestProc
@id int,
@cnt1 int,
@cnt2 int,
@cnt3 int,
@cnt4 int,
@cnt5 int
as
declare @Temp table(nbr int,cnt int)
insert @Temp
select 1,@cnt1 union all
select 2,@cnt2 union all
select 3,@cnt3 union all
select 4,@cnt4 union all
select 5,@cnt5
update #A set #A.cnt=t.cnt from @Temp as T where #A.id=@id and t.nbr=#A.nbr
insert #A select @id,nbr,cnt from @Temp as t where
not exists (select 1 from #A as a where @id=a.id and a.nbr=t.nbr)
這樣行麼?
#12
太好了,比我想的方法好!给分
#1
if (select * from tablename where ...)=0
insert into tablename(...) values(...)
else
update tablename set ... where ...
insert into tablename(...) values(...)
else
update tablename set ... where ...
#2
在存储过程里写逻辑
#3
仅供参考:
create table #temp
(
id int,
NBR int,
cnt int
)
insert #temp
select 1, 1, 20 union all
select 1, 2, 20 union all
select 1, 3, 20 union all
select 2, 1, 20 union all
select 2, 2, 20
go
--SQL:
declare
@id int,
@cnt1 int,
@cnt2 int,
@cnt3 int,
@cnt4 int,
@cnt5 int
select
@id = 1,
@cnt1 = 11 ,
@cnt2 = 1,
@cnt3=1,
@cnt4 = 20,
@cnt5 = 0
--SQL:
declare @table table
(
rowno int,
id int,
new_cnt int,
NBR int,
old_cnt int
)
insert @table
select a.rowno,a.id, new_cnt=a.cnt, b.NBR, old_cnt=b.cnt from
(
select rowno = ROW_NUMBER() over(order by getdate()), * FROM
(
select id = @id, cnt = @cnt1 union all
select id = @id, cnt = @cnt2 union all
select id = @id, cnt = @cnt3 union all
select id = @id, cnt = @cnt4 union all
select id = @id, cnt = @cnt5
)T
) a
LEFT JOIN #temp b
ON a.id = b.id and a.rowno = b.NBR
insert into #temp
select id, rowno, new_cnt from @table
where NBR is null and old_cnt is null
update A
set cnt = new_cnt
from #temp A
inner join
@table b
on a.id = b.id and a.NBR = b.rowno
GO
--RESULT:
select * from #temp
/*
id NBR cnt
1 1 11
1 2 1
1 3 1
2 1 20
2 2 20
1 4 20
1 5 0
*/
#4
在存储过程里一条一条判断:
if exists (select 1 from 表A where id=1 and NBR=1)
begin
update ....
end
else
begin
insert ....
end
后面4句一样
#5
多谢 wwwwgou, 但是我的是sql 2000.
另外,在存储过程里一条一条判断,5条还行,实际情况是nbr 到十多个。。。 那代码太多了。。。
Leftie 方法不行
另外,在存储过程里一条一条判断,5条还行,实际情况是nbr 到十多个。。。 那代码太多了。。。
Leftie 方法不行
#6
在线等,解决马上给分
#7
不定数量的话用动态语句
#8
定数量,就是11个,但是各位高手举例说明一下到5 就行了。
#9
UP
#10
定数量,就是11个,但是各位高手举例说明一下到5 就行了。
#11
create table #A (id int, NBR int, cnt int)
insert #A select 1 ,1 ,20 union all
select 1 ,2 ,30 union all
select 1 ,3, 20 union all
select 2 ,1, 20 union all
select 2 ,2, 20
create proc TestProc
@id int,
@cnt1 int,
@cnt2 int,
@cnt3 int,
@cnt4 int,
@cnt5 int
as
declare @Temp table(nbr int,cnt int)
insert @Temp
select 1,@cnt1 union all
select 2,@cnt2 union all
select 3,@cnt3 union all
select 4,@cnt4 union all
select 5,@cnt5
update #A set #A.cnt=t.cnt from @Temp as T where #A.id=@id and t.nbr=#A.nbr
insert #A select @id,nbr,cnt from @Temp as t where
not exists (select 1 from #A as a where @id=a.id and a.nbr=t.nbr)
這樣行麼?
#12
太好了,比我想的方法好!给分