IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_saleprds_details]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_saleprds_details]
GO
create proc [dbo].[mps_new_saleprds_details]
@cmpid int,
@useruid int,
@branchid int,
@userdpid int,
@sptype nvarchar(100),
@pspid int
as
begin
set nocount on

declare @category1 nvarchar(200)= '',@category2 nvarchar(200)= '',@category3 nvarchar(200) = '', @pcid int = 0

if @pspid = -999999999 set @pspid = 0
if @pspid > 0 
begin
select @category1 = sp_category1,@category2 = sp_category2,@category3  = sp_category3, @pcid  = sp_pcid
from mps_saleprds where sp_id = @pspid
end
-- insert values into mps_saleprds 
insert into mps_saleprds(sp_cmpid,sp_bcid,sp_dpid,sp_cuid,sp_type,sp_prdid,sp_category1,sp_category2,sp_category3,sp_code,sp_use)
values(@cmpid,@branchid,@userdpid,@useruid,@sptype,0,@category1,@category2,@category3,'',1)

-- select insert result
select top 1 * from mps_saleprds
where sp_cmpid = @cmpid and
  sp_bcid = @branchid and 
  sp_dpid = @userdpid and 
  sp_cuid = @useruid and
  sp_type = @sptype and 
  sp_prdid = 0 and
  sp_category1 = @category1 and 
  sp_category2= @category2 and 
  sp_category3= @category3 and 
  sp_code = '' and
  sp_use = 1
order by sp_id desc

set nocount off
end