如果 SQL 写不出来 , 麻烦帮我写个存储过程游标的 也行啊
谢谢了
谢谢了
解决方案 »
- sql server 还原
- 怎么同时删除两个相关联表里的记录?能用连接吗?
- SQL字段类型高手解决中……
- 怎么修改这个存储过程?(断缺号问题)
- 事务在什么情况下不会回滚呀!
- 存储过程中的参数的数目是不是有限制啊?
- 求援:最难的一种数据转换要求了,网上找了半天也未找到答案。。。
- 如果在修改表结构的同时有访问会怎么样?
- 我想用SQL7.0通过VPN连接远程计算机,行的通吗?
- 怎样建立一个索引文件,是需要用到数据库吗?急需解决这个问题,请好心肠的大虾救命.如果能告诉在哪里下载源代码就更加感激不尽了.
- 这个分页存储过程最后一页怎么调用??
- WinXP(sp2)下安装SQL Server 2000出现“安装程序配置服务器失败”
create table a(id1 int,id2 varchar(50) )
insert into a
select 2, 1 union all
select 2 , 2 union all
select 2 , 3 union all
select 2 ,3 union all
select 2 , 5 union all
select 3 , 2 union all
select 3 , 3 union all
select 3, 5 union all
select 3 , 5
if exists (select 1 from sysobjects where name='combine' and xtype='fn')
drop Function combine
------編寫函數實現整合功能
create FUNCTION combine(@vchA varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+','+id2 FROM a WHERE id1=@vchA
RETURN(substring(@r,2,8000))
END
GO
-------執行語句
select id1,id2=dbo.combine(id1) from a group by id1
----刪除測試環境
---drop function combine
---drop table a
-------create by talantlee---------
--------------逗號問題的還原問題----
----------------------------
---測試環境
--要求從--表 a(id1,id2)
drop table a,b,#a
create table a (id1 int,id2 varchar(100))
insert into a
select 2, ' 1,2,3' union all
select 2, '3,5' union all
select 3, '2,3,5' union all
select 3, '5'--轉化成為表b
id1 id2
2 1
2 2
2 3
2 3
2 5
3 2
3 3
3 5
3 5----建立必須條件
select * into b from a where 1<>1 ---復制表結構到b
select * into #a from a ---把數據導入虛擬表----循環取結果
declare @a int
set @a=1
while(@a<>0)
begininsert into b select * from #a where patindex('%,%',id2)=0delete from #a where patindex('%,%',id2)=0insert into b select id1,left(id2, patindex('%,%',id2)-1) from #a
update #a set id2=stuff(id2,1,patindex('%,%',id2),'')
select @a=count(*) from #a
end---得到結果
select * from b order by id1 ---b表為所求
----刪除測試環境
drop table #a,a,b
create table #t (id int Identity(1,1), a varchar(10), b int)
insert #t (a, b) values ('apple', 5)
insert #t (a, b) values ('apple', 6)
insert #t (a, b) values ('apple', 7)
insert #t (a, b) values ('pear', 1)
insert #t (a, b) values ('pear', 2)
insert #t (a, b) values ('pear', 3)
create table #t1 (id int Identity(1,1), a varchar(10), b varchar(8000) not null, c int)
Insert #t1 (a, b, c) select a, '', count(*) from #t group by a
declare @count int, @min int, @max int, @a varchar(10), @min1 int, @max1 int
set @min = 1
set @min1 = 1
select @max=Max(id) from #t1
while @min <= @max
begin
Select @a = a from #t1 where id = @min order by id
Select @max1 = max(id) from #t where a = @a
while @min1 <= @max1
begin
if @min1 < @max1
begin
update #t1 set #t1.b = #t1.b + convert(varchar(1), s.b)+',' from #t s where s.id = @min1 and #t1.a = @a
end
if @min1 = @max1
begin
update #t1 set #t1.b = #t1.b + convert(varchar(1), s.b) from #t s where s.id = @min1 and #t1.a = @a
end
set @min1 = @min1 + 1
end
set @min = @min + 1
end
--结果
select id, a, b from #t1
id a b
1 apple 5,6,7
2 pear 1,2,3--删除测试drop table #t, #t1
insert t (a, b) values ('apple', 5)
insert t (a, b) values ('apple', 6)
insert t (a, b) values ('apple', 7)
insert t (a, b) values ('pear', 1)
insert t (a, b) values ('pear', 2)
insert t (a, b) values ('pear', 3)alter function jionstr (@str varchar(50))
returns varchar(100)
as
begin
declare @str1 varchar(100)
set @str1=''
select @str1=@str1+cast(b as varchar(10))+',' from t where a=@str
return @str1
endselect a,dbo.jionstr(a) from t group by a
id a b
1 apple ,5,6,7
2 pear 1,2,3,
使用了dbo.jionstr(a),很正确
---------------------------------------
tdtjjiao(给分) 结果:
id a b
1 apple 5,6,7
2 pear 1,2,3
只用SQL 方法最简单最 实用 个人比较喜欢这样的
-----------------------------------------talantlee(為了她,再努力點!!!) 简单函数方法的结果是:id a b
1 apple ,5,6,7
2 pear 1,2,3,使用了dbo.,很正确
解答的方法很全具有学习研究价值。(偶还没来的及研究你所有代码 忙 汗```)