颠倒咯

wwwhch2004 2005-03-23 03:16:12
有字符串"a,b,c,d,e,f,g"
如何将其用SQL语句插入到表N_ty中
如:
id Name_Code
1 a
2 b
3 c
...
...全文
78 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwhch2004 2005-03-23
  • 打赏
  • 举报
回复
谢各位革命同志
子陌红尘 2005-03-23
  • 打赏
  • 举报
回复
用楼上jinjazz(近身剪(N-P攻略)) 的方法即可。
xluzhong 2005-03-23
  • 打赏
  • 举报
回复
declare @s nvarchar(1000)
set @s='a,b,c,d,e,f,g'
set @s='create table ##t (id int identity(1,1),col nvarchar(10)) insert into ##t(col) select '''+replace(@s,',',''' as col union all select ''')+''' select * from ##t drop table ##t'
exec(@s)

xiajianfeng 2005-03-23
  • 打赏
  • 举报
回复
--Try
CREATE FUNCTION SplitStr1 (@splitString varchar(8000), @separate varchar(10))
RETURNS @returnTable table(num int, col_Value varchar(20))
AS

BEGIN

declare @thisSplitStr varchar(20)
declare @thisSepIndex int
declare @lastSepIndex int
declare @num int

set @lastSepIndex = 0
set @num = 1

if Right(@splitString ,len(@separate)) <> @separate set @splitString = @splitString + @separate
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)

while @lastSepIndex <= @thisSepIndex
begin
set @thisSplitStr = SubString(@splitString ,@lastSepIndex,@thisSepIndex-@lastSepIndex)
set @lastSepIndex = @thisSepIndex + 1
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)
insert into @returnTable values(@num, @thisSplitStr)
set @num = @num + 1
end
return

END

INSERT INTO N_ty SELECT * FROM dbo.SplitStr1('a,b,c,d,e,f,g', ',')
jinjazz 2005-03-23
  • 打赏
  • 举报
回复
declare @s varchar(1000)
set @s='a,b,c,d,e,f,g'
set @s='insert into N_ty select '''+replace(@s,',',''' union select ''')+''''
exec(@s)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧