
sql :请问以下需求如何用sql实现(count函数逆运算)
即字段2说明了字段1的个数,想把表1转为表2(select),有点像count的逆运算,如何实现??急求!!...
即字段2说明了字段1的个数,想把表1转为表2(select),有点像count的逆运算,如何实现??急求!!
展开
展开全部
declare @表1 table (字段1 varchar(20),字段2 int)
insert into @表1(字段1,字段2)values('A',2)
insert into @表1(字段1,字段2)values('B',3)
insert into @表1(字段1,字段2)values('C',4)
---上边的是测试的表数据
declare @return table (字段1 varchar(20))
declare @t table (字段1 varchar(20),字段2 int,i int)
insert into @t(字段1,字段2) select 字段1,字段2 from @表1 where 1=1
declare @i int,@count int,@j int
set @i=0
update @t set i=@i,@i=@i+1
set @i=1
declare @字段1 varchar(20)
declare @字段2 int
select @count=count(*) from @t
while @i<=@count
begin
select @字段1=字段1,@字段2=字段2 from @t where i=@i
set @j=1
while @j<=@字段2
begin
insert into @return(字段1)values(@字段1)
set @j=@j+1
end
set @i=@i+1
end
select * FROM @return
--上边信息在sql数据库后台运行
insert into @表1(字段1,字段2)values('A',2)
insert into @表1(字段1,字段2)values('B',3)
insert into @表1(字段1,字段2)values('C',4)
---上边的是测试的表数据
declare @return table (字段1 varchar(20))
declare @t table (字段1 varchar(20),字段2 int,i int)
insert into @t(字段1,字段2) select 字段1,字段2 from @表1 where 1=1
declare @i int,@count int,@j int
set @i=0
update @t set i=@i,@i=@i+1
set @i=1
declare @字段1 varchar(20)
declare @字段2 int
select @count=count(*) from @t
while @i<=@count
begin
select @字段1=字段1,@字段2=字段2 from @t where i=@i
set @j=1
while @j<=@字段2
begin
insert into @return(字段1)values(@字段1)
set @j=@j+1
end
set @i=@i+1
end
select * FROM @return
--上边信息在sql数据库后台运行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询