当前位置:首页 » 编程语言 » sql拆分多行语句
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql拆分多行语句

发布时间: 2022-04-26 16:03:59

sql2000 表中一列内容拆分转多行语句

--搭建环境

create table #(a int,b varchar(60))

insert into # select 1,'123,456,xxx,789' union all select 2,'321,213,sss,985'

select * from #

go


------------------------------测试---------------------------------------------

--update组成查询字符串

update # set b=';'+b+';'

update # set b=replace(b,';','''')

update # set b=replace(b,',',''',''')

go

--创建中间表

create table #tmp(a int identity(1,1),co1 varchar(10),co2 varchar(10),co3 varchar(10),co4 varchar(10))

go

--如果#表中行数太多,可能会超过8000的长度,可以考虑用游标替换这部分

declare @sql varchar(8000)

set @sql=''

select @sql=@sql+b+' union all select ' from #

select @sql='select '+left(@sql,len(@sql)-17)

select @sql='insert into #tmp(co1,co2,co3,co4) '+@sql

print @sql

exec(@sql)

select * from #tmp

---------------------------------

select a,co1 from #tmp union all

select a,co2 from #tmp union all

select a,co3 from #tmp union all

select a,co4 from #tmp

order by a


❷ sql 拆分行数据

create or replace procere lpc_aa as
j number(38, 0);
k number(38, 0) default 0;
begin
for i in (select name, qty from lpc_001) loop
while (k < i.qty) loop
j := round(dbms_random.value(1, 10));
if (k + j > i.qty) then
j := i.qty - k;
end if;
insert into lpc_002 values (i.name, j);
k := k + j;
end loop;
k := 0;
end loop;
exception
when others then
rollback;
commit;
end;

用循环之类的把他插入到表中就行。不知道这个可不可以满足你的要求。

❸ 如何用SQL语句将数据一行变成多行呢

截断数据,在新增分隔符即可。
放大招
指定索引号,进行数据截取,返回处理后的字符串数组
string str =你的数据
str.substring(你想分的长度)你自己打印一个转换符就好了

❹ 如何实现类似查询分析器的拆分多条SQL语句的功能

很简单啦:
在sqlserver里面开设用户只能进行"select"操作的用户。
你的程序里面就用这个用户进行连接。
然后在程序规定使用SQL语句结束符“;”的个数为1。
拆分的话思路还是要使用程序的字符串函数来过滤。
截取,截取判断都需要用到。

❺ sql如何把行拆分为几行

如此问题,我也只能这样贴例子了,如不能解决,可以继续联系
--分拆列值
--原着:邹建
--改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)2007-12-16广东深圳

/*
有表tb,如下:
idvalue
----------------------
1aa,bb
2aaa,bbb,ccc
*/
--欲按id,分拆value列,分拆后结果如下:
/*
idvalue
-------------------
1aa
1bb
2aaa
2bbb
2ccc
*/
--1.旧的解决方法(sqlserver2000)
selecttop8000id=identity(int,1,1)into#fromsyscolumnsa,syscolumnsb

selectA.id,substring(A.[values],B.id,charindex(',',A.[values]+',',B.id)-B.id)
fromtbA,#B
wheresubstring(','+A.[values],B.id,1)=','

droptable#

--2.新的解决方法(sqlserver2005)

createtabletb(idint,valuevarchar(30))
insertintotbvalues(1,'aa,bb')
insertintotbvalues(2,'aaa,bbb,ccc')
go
selectA.id,B.value
from(
selectid,[value]=convert(xml,'<root><v>'+replace([value],',','</v><v>')+'</v></root>')fromtb
)A
outerapply(
selectvalue=N.v.value('.','varchar(100)')fromA.[value].nodes('/root/v')N(v)
)B

droptabletb

/*
idvalue
-----------------------------------------
1aa
1bb
2aaa
2bbb
2ccc

(5行受影响)
*/

❻ sql一个字段内有分隔符如何拆分成多行

select num,id, substr(test1,0,instr(test1, ',')-1) test1, substr(test2,0,instr(test2, ',')-1) test2, substr(test3,0,instr(test3, ',')-1) test3 from table_name --前
union
select num,id, substr(test1,instr(test1, ',')+1) test1, substr(test2,instr(test2, ',')+1) test2, substr(test3,instr(test3, ',')+1) test3 from table_name --后

instr(test1, ',') 是计算逗号的位置。

❼ 求一个SQL语句,按某一字段值拆分多行

4行变成1行? 如果用 SQL Server 的话,可以用一种很 诡异的方法: SELECT DISTINCT ',' + SALE_ITEM FROM SALE_REPORT FOR XML PATH('') 楼主可以测试一下,最后用一个 FOR XML PATH('') 就好。 我上面的 SQL 执行结果为: ,C,A,B

❽ SQL如何把多条数据拆分

drop table test
create table test
(
VID int primary key,
VNum int,
vname varchar(10),
score int
)
insert into test values(1,001,'大海',20)
insert into test values(2,001,'大海',30)
insert into test values(3,001,'大海',40)

drop function fn_test
alter function fn_test
(
@vnum int
)
returns varchar(20)
as
begin
declare @str varchar(20)
select @str = isnull(@str+',','') + cast(score as varchar(2)) from test
return @str
end
go

select top 1 vid,vnum,vname,dbo.fn_test(vnum) as '分数' from test
/**
vid vnum vname 分数
----------- ----------- ---------- --------------------
1 1 大海 20,30,40

(所影响的行数为 1 行)
**/

可以了 呵呵呵呵呵

***********************************************************************************************这个是在SQL SERVER里执行的啊****************

❾ mssql根据条件数据列分解成多行数据,急

创建测试表

createtablet
(idint,
namevarchar(10),
typevarchar(10))

insertintotvalues(1,'苹果','003')
insertintotvalues(2,'香蕉','A04')
insertintotvalues(3,'西瓜',NULL)
insertintotvalues(4,'石榴','002')

运行

selectt.*,t.name+s.name2name2fromtleftjoin
(selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'级'elsecast(t1.idasvarchar)+'级'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1))s
ont.type=s.type

结果

selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'级'elsecast(t1.idasvarchar)+'级'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1)