--测试环境
Create table Test(dobillno varchar(10),billno int)
insert into Test select 'a',1
union all select 'b',1
union all select 'c',2
union all select 'd',2
union all select 'e',2
union all select 'f',3
--建函数
Create function F_GetStr(@billno int)
returns varchar(200)
as
begin
declare @return varchar(200)
set @return=''
select @return=@return+'/'+dobillno from Test where billno=@billno
return stuff(@return,1,1,'')
end
--查询
select
billno,
billline=count(*),
dobillno=dbo.F_GetStr(billno)
from Test
group by billno--结果
billno billline dobillno
1 2 a/b
2 3 c/d/e
3 1 f
--删除测试环境
drop table Test
Drop function F_GetStr(3 row(s) affected)
Create table Test(dobillno varchar(10),billno int)
insert into Test select 'a',1
union all select 'b',1
union all select 'c',2
union all select 'd',2
union all select 'e',2
union all select 'f',3
--建函数
Create function F_GetStr(@billno int)
returns varchar(200)
as
begin
declare @return varchar(200)
set @return=''
select @return=@return+'/'+dobillno from Test where billno=@billno
return stuff(@return,1,1,'')
end
--查询
select
billno,
billline=count(*),
dobillno=dbo.F_GetStr(billno)
from Test
group by billno--结果
billno billline dobillno
1 2 a/b
2 3 c/d/e
3 1 f
--删除测试环境
drop table Test
Drop function F_GetStr(3 row(s) affected)
解决方案 »
- sql比较
- 关于LEFT JOIN 的问题! SQL TimeOut!
- 请问int型最大数据是多少?
- 如何将 SQL Server 的验证模式更改为"SQL Server and Windows"(也称作"混合模式")。
- 现在有两个表,tab1,tab2,通过各自的id1和id2关联起来,现在我想通过id1和id2的对应关系,把tab1中某几个字段的值赋给tab2种的某几个字
- C#代码中怎样调用存储过程
- 按条件合并两个表的数据的sql语句
- SQL Server中企业管理器出现“1069登录失败”,怎么办,帮帮我,谢谢!
- VFP中的左联接、右联接、完全联接的适用场合?
- 紧急求救:数据库只读
- 求:列变行?
- SELECT ( 3 / 2 ) + ( 5 / 2 ) AS RETVAL 应该返回什么值
create table c_test(billno n(10),billline n(10),dobillno c(100))
use c_test
select 0
use yourtable
insert c_test select billno,count(*),"" from yourtable group by billno order by billno
s=""select c_test
if !bof() then go 1 endif
do while !eof()
nRecno=recno()
nBillno=billno
select * from yourtable where billno=nbillno into cursor c_test1
select c_test1
do while !eof()
s=s & dobillno & "/"
skip
enddo
use c_test1
select c_test
update c_test set dobillno=left(s,len(s)-1)
s=""
skip
enddo
select c_test
browse