补充昨天:
declare @renum int --不同P的不同单价数
declare @Id int不清 --循环变量
declare @str varchar (8000) --总串
declare @S_str varchar(8000) --select 串
declare @F_str varchar(8000) --from 串
declare @W_str varchar(8000) --where 串
declare @tablename varchar(10) --表别名
declare @Ftablename varchar(10) --前一表别名
declare @pname varchar(10) --单价别名
declare @idp int --第二循环变量
declare @case_str varchar(8000) --case串
--drop table #temp
create table #temp (p char(10),price decimal(18,2))
insert into #temp select distinct p,price from pprice
select @renum=count(*) from #temp
set @id=1
set @S_str='select a.p,a.price'
set @F_str=' from #temp a'
set @W_str=' where a.p=a.p'
if @renum=1
begin
set @str=@S_str+@F_str
end else
begin
--set @case_str=' case when a.price'
while @id<=@renum-1
begin
set @tablename='a'+convert(varchar,@ID)
set @pname='price'+convert(varchar,@ID)
if @id<2
begin
set @S_str=@S_str+','+@pname+'= case when a.price=a1.price then 0 else a1.price end'
end else
begin
set @idp=1
set @case_str=' case when a.price='+@tablename+'.price'
while @idp<=@iD-1
begin
set @Ftablename='a'+convert(varchar,@IDp)
--set @ppname='price'+convert(varchar,@IDp)
set @case_str=@case_str+' or '+@Ftablename+'.price='+@tablename+'.price'
set @idp=@idp+1
end
set @case_str=@case_str+' then 0 else '+@tablename+'.price end'
set @S_str=@s_str+','+@pname+'='+@case_str
end
set @F_str=@F_str+',#temp '+@tablename
set @W_str=@W_str+' and a.p='+@tablename+'.p'
set @id=@id+1
end
set @str=@S_str+@F_str+@W_str
end
set @str='select distinct a.p,a.pricet from (select a.p,pricet=a.price+a.price1+a.price2+... from (' +@str+') a) a'
---此句也可用循环
exec(@str)
表的记录
p px price
-----------------
p p1 1.2
p p2 1.6
p p2 1.8
相加前@str的结果
p price price1 price2
---------- -------------------- -------------------- --------------------
p 1.20 .00 1.80
p 1.60 1.20 1.80
p 1.80 1.20 .00
p 1.20 .00 1.60
p 1.60 1.20 .00
p 1.80 1.20 1.60
p 1.20 .00 .00
p 1.60 1.20 .00
p 1.80 1.20 .00
p 1.20 1.60 1.80
p 1.60 .00 1.80
p 1.80 1.60 .00
p 1.20 1.60 .00
p 1.60 .00 .00
p 1.80 1.60 .00
p 1.20 1.60 .00
p 1.60 .00 1.20
p 1.80 1.60 1.20
p 1.20 1.80 .00
p 1.60 1.80 .00
p 1.80 .00 .00
p 1.20 1.80 1.60
p 1.60 1.80 .00
p 1.80 .00 1.60
p 1.20 1.80 .00
p 1.60 1.80 1.20
p 1.80 .00 1.20
最后结果
p pricet
---------- ----------------------
p 1.20
p 1.60
p 1.80
p 2.80
p 3.00
p 3.40
p 4.60
set @n = 3
select @mm1 = 'b,b1¦b,b2¦b,b3¦c,c1¦c,c2¦c,c3¦d,d1¦d,d2¦d,d3'
CREATE TABLE #dd (
[p] [int] NULL ,
[m] [numeric] (18,3) NULL )
declare @sqltext varchar(8000)
if @mm1 <> ''
begin
select @sqltext = 'insert into #dd (p,m) values ('+replace(@mm1,'¦',') insert into #dd (p,m) values (')+')'
exec(@sqltext)
end
declare @i int
set @i = 2
select @sqltext ='select m from #dd where p = 1'
while @i<=@n
begin
select @sqltext = 'select a.m+b.m as m from (select m from #dd where p = '+cast(@i as varchar(30))+') a cross join ('+@sqltext+') b'
select @i =@i+1
end
set @n = 3
select @mm1 = '1,2.8|1,5|1,6|2,2|2,6|2,9|3,9|3,3.6|3,5'
CREATE TABLE #dd (
[p] [int] NULL ,
[m] [numeric] (18,3) NULL )
declare @sqltext varchar(8000)
if @mm1 <> ''
begin
select @sqltext = 'insert into #dd (p,m) values ('+replace(@mm1,'|',') insert into #dd (p,m) values (')+')'
exec(@sqltext)
end
declare @i int
set @i = 2
select @sqltext ='select m from #dd where p = 1'
while @i<=@n
begin
select @sqltext = 'select a.m+b.m as m from (select m from #dd where p = '+cast(@i as varchar(30))+') a cross join ('+@sqltext+') b'
select @i =@i+1
end
sorry,[method two]中有些不完整
if n > 1
begin
set @newtable = "#price" + convert(varchar(100),n)
set @strsql = "select v1.*,v2.* into " + @newtable
set @strsql = @strsql + " from #itemprice v1," + @oldtable + " v2 "
set @strsql = "drop table " & oldtable
end
else
begin
set @newtable = "#price" + convert(varchar(100),n)
set @strsql = "select v1.* into " + @newtable + " from #itemprice v1 "
-------------------
end
set @oldtable = @newtable
execute (strsql)
select u1.*,u2.*,...,un.* from price1 u1,price2 u2,...,pricen un
[method two]
用上述办法可能SQL比较长,也不易组织和维护,可以改进SQL大致如下:
declare @lcount int
declare @ItemID int
declare @oldtable varchar(100)
declare @newtable varchar(100)
declare @strsql varchar(4000)
set @lcount = 0
declare cur_item cursor for
select fchildid from bomtable
open cur_item
fetch cur_item into @itemid
while (@@fetch_status = 0)
begin
set @lcount = @lcount + 1
---得到该物料价格 #itemprice
if n > 1
begin
set @newtable = "#price" + convert(varchar(100),n)
set @strsql = "select v1.*,v2.* into " + @newtable
set @strsql = @strsql + " from #itemprice v1," + @oldtable + " v2 "
set @strsql = "drop table " & oldtable
end
else
begin
set @newtable = "#price" + convert(varchar(100),n)
set @strsql = "select v1.* into " + @newtable + " v1 "
end
set @oldtable = @newtable
execute (strsql)
fetch cur_item into @itemid
end
drop table #itemprice
close cur_item
DEALLOCATE cur_item