有一个字符串: 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省资源,因为占用的日志少。