i am developing aquery that Update two Tables at the Same Time with user_defined_Table_types
(Tbl_addition_Store and Tbl_item_Journals)
tbl_addition_store (add_id,date,product_id,Supplier_id,Quanty,price,value)
Tbl_Journals(journal_id,add_id,date,product_id,Supplier_id,Quanty,price,value)
i have created two user_defined_Table_types
the first called import_products
CREATE TYPE [dbo].[Import_Products] AS TABLE(
[add_num] [bigint] NULL,
[add_Date] [date] NULL,
[product_id] [int] NULL,
[Supp_id] [int] NULL,
[quantity] [decimal](18, 2) NULL,
[price] [decimal](18, 2) NULL,
[value] [decimal](18, 2) NULL
)
and the second type called Insert_Items_Journals
CREATE TYPE [dbo].[Insert_Items_Journals] AS TABLE(
[Add_Num] [bigint] NULL,
[date] [date] NULL,
[Product_Id] [int] NULL,
[Supp_Id] [int] NULL,
[Quantity] [decimal](18, 0) NULL,
[Price] [decimal](18, 0) NULL,
[Value] [decimal](18, 0) NULL
)
after that i created a stored procedure called import_All_Items .i want to save values from my datagridview inside the two tables with this stored procedure.I want the Query to check first if the values are existed .if they are existed the query will update the quantity.if it was not existed the query will add new record .the problem is the query saves the data inside tbl_addition_store from the first row but in the tbl_item_Journals it saves the data from the second row
What I have tried:
create proc [dbo].[import_All_Items]
@import_product Import_Products readonly,
@Insert_Items_Journals Insert_Items_Journals readonly
as
declare @adition cursor
set @adition =cursor for(select add_num, add_Date,product_id,Supp_id,supp_name,quantity,price,value from @import_product)
open @adition
declare @add_num bigint,@add_date date,@productid int,@Supp_id int,@supp_name nvarchar(150),@quantity decimal(18,2),@price decimal(18,2),@value decimal(18,2)
fetch @adition into @add_num,@add_date,@productid,@Supp_id,@supp_name,@quantity,@price,@value
while @@FETCH_STATUS=0
begin
declare @insert_Journal cursor
set @insert_Journal =cursor for(select Add_Num,date,Product_Id,Supp_Id,Quantity,Price,Value from @Insert_Items_Journals)
open @insert_Journal
fetch first from @insert_Journal into @add_num,@add_date,@productid,@Supp_id,@quantity,@price,@value
while @@FETCH_STATUS=0
begin
if exists (select Add_Id from Tbl_Addition_Store where Date=@add_date and Product_Id=@productid)
begin
update Tbl_Addition_Store set Quantity=quantity+@quantity,Value=Value+@value
where Add_Id=(select Add_Id from Tbl_Addition_Store where Date=@add_date and Product_Id=@productid)
update Tbl_Item_Journals set Quantity=quantity+@quantity,Value=Value+@value
where Add_Sarf_Id=(select Add_Sarf_Id from Tbl_Item_Journals where journal_date=@add_date and product_id=@productid)
end
else
begin
INSERT INTO [dbo].[Tbl_Addition_Store]
([Add_Num]
,[Date]
,[Product_Id]
,[Supp_Id]
,[Supp_Name]
,[Quantity]
,[Price]
,[Value])
values (@add_num,@add_date,@productid,@Supp_id,@supp_name,@quantity,@price,@value)
fetch next from @adition into @add_num,@add_date,@productid,@Supp_id,@supp_name,@quantity,@price,@value
INSERT INTO [dbo].[Tbl_Item_Journals]
(Add_Sarf_Id,
[journal_date]
,[product_id]
,[supp_id]
,[quantity]
,[price]
,[value])
VALUES
(@add_num,@add_date,@productid,@supp_id,@quantity,@price,@value)
fetch next from @insert_Journal into @add_num,@add_date,@productid,@Supp_id,@quantity,@price,@value
end
fetch @adition into @add_num,@add_date,@productid,@Supp_id,@supp_name,@quantity,@price,@value
fetch @insert_Journal into @add_num,@add_date,@productid,@Supp_id,@quantity,@price,@value
end
fetch @adition into @add_num,@add_date,@productid,@Supp_id,@supp_name,@quantity,@price,@value
end