发布网友 发布时间:2022-04-23 02:40
共7个回答
热心网友 时间:2022-07-12 00:54
看他人的回答,都写得很累,我也没想更快捷的办法,但有一点,SQL是为了你的软件而服务的,用软件代码去处理更方便些,split函数得到数组很方便。
再者,即使用SQL,你把同在A表里把A1,A2取出来插入做了A3,A4,这样下次再执行这些代码时,必然很累,所以你应该把数据做到别的表。
我用游标试写一个如下:
--insert into a select '大葱;马铃薯;黄瓜;西红柿;栀','723;34;54;9921;9'
--insert into a select '牛肉','454'
GO
create function [dbo].[f_split](@c varchar(2000),@split varchar(2))
returns @t table(id int identity(1,1), col varchar(2000))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
GO
GO
CREATE PROCEDURE [dbo].[A_test]
AS
BEGIN
SET NOCOUNT ON;
declare @cur cursor
set @cur = cursor for
select a1,a2 from A
declare @a1 nvarchar(2000), @a2 nvarchar(2000)
declare @tb table (iid int identity(1,1), a3 nvarchar(2000),a4 nvarchar(2000))
open @cur
fetch next from @cur into @a1 ,@a2
WHILE @@FETCH_STATUS = 0
begin
insert into @tb (a3,a4)
select a.col ,b.col
from dbo.f_split(@a1,';') a inner join dbo.f_split(@a2,';') b
on a.id = b.id ;
fetch next from @cur into @a1 ,@a2
end
select * from @tb
END
GO
exec A_test
---------------------
iid a3 a4
1 大葱 723
2 马铃薯 34
3 黄瓜 54
4 西红柿 9921
5 栀 9
6 牛肉 454
热心网友 时间:2022-07-12 00:55
单一SQL应该不行的吧,用存储过程吧
用函数以;为分割进行截取数据
然后循环一下,就可以了
热心网友 时间:2022-07-12 00:55
CREATE TABLE a1
(
a1 VARCHAR(5000)
)
GO
CREATE TABLE a2
(
a2 VARCHAR(5000)
)
GO
CREATE TABLE a3
(
a3 VARCHAR(100),
a4 VARCHAR(100)
)
GO
INSERT INTO a1 VALUES ('大葱;马铃薯;黄瓜;西红柿;栀;')
GO
INSERT INTO a2 VALUES ('723;34;54;9921;9;')
GO
CREATE PROC UP_Devide
AS
DECLARE @Memo1 VARCHAR(1000)
DECLARE @Memo2 VARCHAR(1000)
DECLARE @Memo3 VARCHAR(100)
DECLARE @Memo4 VARCHAR(100)
DECLARE @Door_Int INT
SET @Door_Int = 1
DECLARE @IsOnlyOne INT
SET @IsOnlyOne = 1
SELECT @Memo1 = a1 FROM a1
SELECT @Memo2 = a2 FROM a2
WHILE @Door_Int > 0
BEGIN
SET @Door_Int = CHARINDEX(';',@Memo1)
IF @Door_Int = 0
BEGIN
IF @IsOnlyOne = 1
BEGIN
INSERT INTO a3 VALUES (@Memo1,@Memo2)
END
BREAK
END
SET @IsOnlyOne = 0
SET @Memo3 = LEFT(@Memo1,@Door_Int - 1)
SET @Memo1 = RIGHT(@Memo1,LEN(@Memo1) - @Door_Int)
SET @Door_Int = CHARINDEX(';',@Memo2)
SET @Memo4 = LEFT(@Memo2,@Door_Int - 1)
SET @Memo2 = RIGHT(@Memo2,LEN(@MEMO2) - @Door_Int)
INSERT INTO a3 VALUES (@Memo3,@Memo4)
END
GO
--结果
大葱 723
马铃薯 34
黄瓜 54
西红柿 9921
栀 9
热心网友 时间:2022-07-12 00:56
假设你的a1和a2都以“;”结尾,如果不是,可以自己增加“;”结尾
自己增加即可在
set @fstr1=(select a1 from a)
这个后面添加
set @fstr1=(select rtrim(@fstr1)+';')
即可
create procere procname
as
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[test]
create table test
(
a3 varchar(100),
a4 int,
)
declare @fstr1 varchar(1000)
declare @fstr2 varchar(1000)
declare @ftemp1 varchar(100)
declare @ftemp2 varchar(100)
declare @fint1 int
declare @fint2 int
set @fstr1=(select a1 from a)
set @fstr2=(select a2 from a)
set @fint1=(select charindex(';',@fstr1))
set @fint2=(select charindex(';',@fstr2))
while @fint1>0
begin
set @fint1=(select charindex(';',@fstr1))
set @fint2=(select charindex(';',@fstr2))
set @ftemp1=(select left(@fstr1,@fint1-1))
set @ftemp2=(select left(@fstr2,@fint2-1))
insert into test
select @ftemp1,@ftemp2
set @fstr1=(select right(@fstr1,len(@fstr1)-@fint1))
set @fstr2=(select right(@fstr2,len(@fstr2)-@fint2))
set @fint1=(select charindex(';',@fstr1))
set @fint2=(select charindex(';',@fstr2))
end
热心网友 时间:2022-07-12 00:57
下边将返回你需要的列表信息,之后你要插入还是怎么处理就看你了
DECLARE @a1 varchar(50)
DECLARE @a2 varchar(50)
create table ##temp1
(
id [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
a1 varchar(50) not null
)
create table ##temp2
(
id [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
a2 varchar(50) not null
)
DECLARE @str varchar(200)
DECLARE @result varchar(1000)
DECLARE testcursor1 CURSOR FOR
SELECT a1,a2
FROM a
OPEN testcursor1
FETCH NEXT FROM testcursor1
INTO @a1,@a2
WHILE @@FETCH_STATUS = 0
BEGIN
set @result =' insert into ##temp1(a1) select '''+replace(@a1,';','''union select''')+''''
exec(@result)
set @result =' insert into ##temp2(a2) select '''+replace(@a2,';','''union select''')+''''
exec(@result)
FETCH NEXT FROM testcursor1
INTO @a1,@a2
END
select ##temp1.a1,##temp2.a2 from ##temp1 inner join ##temp2 on (##temp1.id=##temp2.id)
drop table ##temp1
drop table ##temp2
热心网友 时间:2022-07-12 00:57
一行数据,如何做成有多行数据的?那增加的数据前面的a1,a2不是都变成空了?
热心网友 时间:2022-07-12 00:58
加个随机排序进去
select distinct top 20 * from procts where u_id=? order by data desc,newid();
************
补充,试试下面的方法看看可以不:
************
select distinct top 20 *
(select * from procts where u_id=1
union
select * from procts where u_id<>1
)tmp
order by data desc,newid();
---
以上,希望对你有所帮助。