Friday, 4 January 2013

list of products add once stored procedure

/*
list of products add once
ProductID = 106,107,109,210
*/

Create PROCEDURE [dbo].[Nop_Product](Procedure name)
@ProductID varchar(max),
@UserName varchar(max)
as
begin
declare @CustomerID int
select @CustomerID=CustomerID from [Nop_Customer] where Username=@UserName
declare @CsvIds varchar(max)
declare @index int=0
set @CsvIds=@ProductID
select @index=CHARINDEX(',',@CsvIds,0)
--While loop started
while (@index >0 )
begin
declare @CUR_ProductID int = CONVERT(INT,(SUBSTRING (@CsvIds,1,@index-1)))
if not exists (select * from [Nop_Product_Customer_Mapping] where [ProductId] =@CUR_ProductID)
begin
INSERT INTO [dbo].[Nop_Product_Customer_Mapping]
           ([ProductId]
           ,[CustomerID])
     VALUES
           (@CUR_ProductID ,
            @CustomerID)
         
            end
            else
            begin
            update [dbo].[Nop_Product_Customer_Mapping] set [CustomerID]=@CustomerID where [ProductId]=@CUR_ProductID
            end
--print SUBSTRING (@CsvIds,1,@index-1)
set @CsvIds=SUBSTRING (@CsvIds,@index+1,LEN (@CsvIds))
select @index=CHARINDEX(',',@CsvIds,1)
end

if not exists (select * from [Nop_Product_Customer_Mapping] where [ProductId] =@CsvIds)
begin
INSERT INTO [dbo].[Nop_Product_Customer_Mapping]
           ([ProductId]
           ,[CustomerID])
     VALUES
           (@CsvIds ,
            @CustomerID)
         
            end
            else
            begin
            update [dbo].[Nop_Product_Customer_Mapping] set [CustomerID]=@CustomerID where [ProductId]=@CsvIds
            end
END

No comments:

Post a Comment