![[SQL] 待整理3 [SQL] 待整理3](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
Create proc procedureName
@orderId int
,@name varchar()
,@count int
as
begin Declare @id int
declare @tb table(postid int)
BEGIN TRANSACTION
insert into A value(@orderId)
Select @id=@@identity
SET @count =
SELECT @count = count(*) FROM dbo.f_split('A,B,C,D,E',',')
WHILE @count >
begin
insert into @tb select @id
set @count=@count-
end
--select * from dbo.f_split('A,B,C,D,E',',') insert into zibiao values(select * from @tb,select * from dbo.f_split('A,B,C,D,E',','),select * from dbo.f_split('A,B,C,D,E',',')) --Insert into B values(@id,@name) IF @@error <> --发生错误
BEGIN
ROLLBACK TRANSACTION
RETURN
END
ELSE
BEGIN
COMMIT TRANSACTION
RETURN --执行成功
END
end
下面的sql文是个函数,功能是:分析字符串,根据特殊符号整理插入到一个表结构里
create function f_split
(
@SourceSql varchar()
,@StrSeprate varchar()
)
returns @temp table(a varchar())
--实现split功能 的函数
--date :--
--Author :Domino
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=
begin
insert @temp values(left(@SourceSql,@i-))
set @SourceSql=substring(@SourceSql,@i+,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return
end