计算机学习积累

----转载有理,转载是想研究,想研究才会看,看了才会有感想,转载后我有时会写一些自己的感受
数据加载中……

itpub笔记-字符串分割

有一个字符串: a,bc,d,abce,ddz
现在用T-SQL语句把这一字符串变成这种样子:
item
----------
a
bc
d
abce
ddz

(5 row(s) affected)

正解:
declare   @str   varchar ( 20 ), @strSql   varchar ( 8000 )
set   @str   =   ' a,bc,d,abce,ddz '   --  此处的字符串可以随心所欲的更改

if   object_id ( ' tempdb.dbo.#temp1 ' is   null
create   table  #temp1(item  varchar ( 20 ))
else

truncate   table  #temp1
SELECT   @strSql = ' insert into #temp1 values( ''' + REPLACE ( @str , ' , ' , '''

insert into #temp1 values(
''' ) + ''' ) '

print   @strSql
exec  ( @strSql )
select   *   from  #temp1

知识:
1.)  truncate table tableName   一次删除表中的所有数据,同于delete没有where,但是比delete省资源,因为占用的日志少。

posted on 2006-08-08 08:40 freebird 阅读(251) 评论(0)  编辑  收藏 所属分类: 数据库


只有注册用户登录后才能发表评论。


网站导航: