码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程中用游标联动插入(商品分类)

时间:2014-08-20 09:14:06      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:os   io   for   ar   cti   new   ad   ef   

例子:往商品表插入的同时,也将对应的多个分类加入到商品分类表中

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

存储过程中用游标联动插入(商品分类)

标签:os   io   for   ar   cti   new   ad   ef   

原文地址:http://www.cnblogs.com/ying12318/p/3923725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!