100分求一个简单的存储过程!!!!!!
下面每一行都是表table_A中字段colum_A的值,每一个值都是由逗号分隔、多个整数组成的数字串:
1,102,
1,105,
2,205,
1,101,10102,1010202,
2,202,20203,
2,203,20303,
1,104,10402,
1,105,21,
2,206,50008164,50008822,
1,105,50008164,50001705,211509,
1,105,50008164,50001705,50008398,50008400,
现在需要求出表中所有不同的整数并列出来,怎样用一个最简单的存储过程高效率的实现?
[解决办法]
--刚才贴得太急了,这个是正确的
create table #table_A(colum_A varchar(2000))
insert #table_A
SELECT '1,102,' AS colum_A UNION ALL
SELECT '1,105,' AS colum_A UNION ALL
SELECT '2,205,' AS colum_A UNION ALL
SELECT '1,101,10102,1010202,' AS colum_A UNION ALL
SELECT '2,202,20203, ' AS colum_A UNION ALL
SELECT '2,203,20303,' AS colum_A UNION ALL
SELECT '1,104,10402,' AS colum_A UNION ALL
SELECT '1,105,21,' AS colum_A UNION ALL
SELECT '2,206,50008164,50008822,' AS colum_A UNION ALL
SELECT '1,105,50008164,50001705,211509,' AS colum_A UNION ALL
SELECT '1,105,50008164,50001705,50008398,50008400, ' AS colum_A
CREATE TABLE #Temp(colum_A INT)
WHILE EXISTS(SELECT 1 FROM #table_A WHERE colum_A<>'')
BEGIN
INSERT #Temp
SELECT DISTINCT LEFT(colum_A,CHARINDEX(',',colum_A)-1)
FROM #table_A
WHERE CHARINDEX(',',colum_A)>1
AND LEFT(colum_A,CHARINDEX(',',colum_A)-1) NOT IN (SELECT colum_A FROM #Temp)
AND ISNUMERIC(LEFT(colum_A,CHARINDEX(',',colum_A)-1))=1
UPDATE #table_A
SET colum_A=STUFF(colum_A,1,CHARINDEX(',',colum_A),'')
WHERE CHARINDEX(',',colum_A)>1
END
SELECT * FROM #Temp
SELECT * FROM #table_A
DROP TABLE #Temp,#table_A
[解决办法]
--??
- SQL code
create table #(col varchar(100))insert into #select '1,102,' union allselect '1,105,' union allselect '2,205,' union allselect '1,101,10102,1010202,' union allselect '2,202,20203,' union all select '2,203,20303,' union allselect '1,104,10402,' union allselect '1,105,21,' union allselect '2,206,50008164,50008822,' union all select '1,105,50008164,50001705,211509,' union allselect '1,105,50008164,50001705,50008398,50008400,'select top 1000 id=identity(int,1,1)into #t from sysobjects,syscolumnsselect *from #select distinct col=substring(a.col,b.id,charindex(',',a.col+',',b.id)-b.id)from # a,#t bwhere substring(','+a.col,b.id,1)=','order by col/*col ---------------------------------------------------------------- 11011010210102021021041040210522022020320320303205206212115095000170550008164500083985000840050008822(23 row(s) affected)*/
[解决办法]
- SQL code
--建立环境create table a (col1 varchar(1000))insert into aselect '1,102,' union allselect '1,105,' union all select '2,205,' union all select '1,101,10102,1010202,' union all select '2,202,20203,' union all select '2,203,20303,' union all select '1,104,10402,' union all select '1,105,21,' union all select '2,206,50008164,50008822,' union all select '1,105,50008164,50001705,211509,' union all select '1,105,50008164,50001705,50008398,50008400,' ---执行句declare @exec varchar(8000)set @exec=''select @exec=@exec+col1 from aset @exec='select '+ left(replace(@exec,',',' union select '),len(replace(@exec,',',' union select '))-len('union select'))exec (@exec)/*执行结果:--------------12211011021041052022032052061010210402202032030321150910102025000170550008164500083985000840050008822*/
[解决办法]
- SQL code
--把所有值连成一串,逗号替换成union select,--然后再把最后一个union select 去掉declare @string varchar(2000)set @string=''select @string=@string+colum_A from aselect @string='select '+left(replace(@string,',',' union select '), len(replace(@string,',',' union select '))-len('union select '))exec (@string)
[解决办法]
- SQL code
--sql server 2000,用一临时表解决.create table tb(col varchar(100))insert into tbselect '1,102,' union allselect '1,105,' union allselect '2,205,' union allselect '1,101,10102,1010202,' union allselect '2,202,20203,' union all select '2,203,20303,' union allselect '1,104,10402,' union allselect '1,105,21,' union allselect '2,206,50008164,50008822,' union all select '1,105,50008164,50001705,211509,' union allselect '1,105,50008164,50001705,50008398,50008400,'--临时表SELECT TOP 8000 id = identity(int,1,1) INTO tmp FROM syscolumns a, syscolumns b SELECT distinct col = SUBSTRING(A.col, B.ID, CHARINDEX(',', A.col + ',', B.ID) - B.ID) FROM tb A, tmp BWHERE SUBSTRING(',' + a.col, B.id, 1) = ','ORDER BY colGOdrop table tb , tmp/*col ---------------------------------------------------------------- 11011010210102021021041040210522022020320320303205206212115095000170550008164500083985000840050008822(所影响的行数为 23 行)*/
[解决办法]
这句效率蛮高
- SQL code
select distinct [agentpath]=cast(reverse(substring(reverse([agentpath]),2,case when (charindex(',',reverse([agentpath]),2)-2)<=0 then len([agentpath])-1 else charindex(',',reverse([agentpath]),2)-2 end)) as int)from CommodityBargain where agentpath <> ''order by agentpath asc