例子:往商品表插入的同时,也将对应的多个分类加入到商品分类表中
create PROCEDURE TProduct_ADD
@ProID bigint output, --自动编号
@ISBN nvarchar(50),
@pCode nvarchar(100),
@pName nvarchar(200),
@Unit nvarchar(50),
@price decimal(18,2),
@PurchasePrice decimal(18,2),
@Discount decimal(4,2),
@MembePrice decimal(18,2),
@MembePrice2 decimal(18,2),
@Press nvarchar(100),
@PressTime nvarchar(50),
@Editor nvarchar(200),
@TEditor nvarchar(200),
@StockNumber int,
@ProSmallImage nvarchar(200),
@ProBiglImage nvarchar(200),
@BriefIntro nvarchar(300),
@Remark nvarchar(MAX),
@ISHot char(1),
@ISNew char(1),
@ISTop char(1),
@ISBargain char(1),
@CreateTime datetime,
@ModifyTime datetime,
@AddedBy nvarchar(200),
@ISBuy nchar(1),
@ReturnPrice decimal(18,2),
@ISCheck char(1),
@storehouseID nvarchar(50),
@DelFlag char(1),
@EditionNum nvarchar(50),
@PCategriy nvarchar(50) --2,8,4
AS
INSERT INTO [TProduct](
[ISBN],[pCode],[pName],[Unit],[price],[PurchasePrice],[Discount],[MembePrice],[MembePrice2],[Press],[PressTime],[Editor],[TEditor],[StockNumber],[ProSmallImage],[ProBiglImage],[BriefIntro],[Remark],[ISHot],[ISNew],[ISTop],[ISBargain],[CreateTime],[ModifyTime],[AddedBy],[ISBuy],[ReturnPrice],[ISCheck],[storehouseID],[DelFlag],[EditionNum]
)VALUES(
@ISBN,@pCode,@pName,@Unit,@price,@PurchasePrice,@Discount,@MembePrice,@MembePrice2,@Press,@PressTime,@Editor,@TEditor,@StockNumber,@ProSmallImage,@ProBiglImage,@BriefIntro,@Remark,@ISHot,@ISNew,@ISTop,@ISBargain,@CreateTime,@ModifyTime,@AddedBy,@ISBuy,@ReturnPrice,@ISCheck,@storehouseID,@DelFlag,@EditionNum
)
SET @ProID = @@IDENTITY
--然后往商品分类表中添加记录
--需要把2,8,4东西,分开
declare @_classid BIGINT
declare rebate cursor --定义回扣游标
for select * from dbo.f_split(@PCategriy,‘,‘)//调用函数,实现(多个id号)字符串用‘,‘分割开
open rebate
fetch next from rebate into @_classid
declare @i int ;
set @i=1;
while @@fetch_status=0
begin
INSERT INTO [TProduct_Category] ( [productID] ,[PCategory] ) VALUES (@ProID ,@_classid)
fetch next from rebate into @_classid
end
--4.关闭游标
close rebate
--5.删除游标
deallocate rebate
//创建 f_split函数
create function [dbo].[f_split](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
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
存储过程中用游标联动插入(商品分类),布布扣,bubuko.com