Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
SQL
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
SQL
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:

SQL
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 
Posted
Updated 29-Oct-18 1:51am
v2
Comments
CHill60 25-Oct-18 9:18am    
Why are you using a cursor? It would be far better to use EXISTS and update/insert in SETS of data (SQL is a set-based language after all)
ZurdoDev 25-Oct-18 9:33am    
I agree with ^. You are way over-complicating it.

IF EXISTS (SELECT 1 FROM table WHERE id = @id)
BEGIN
-- do an update
END
ELSE
BEGIN
-- do an insert
END
Abuamer 28-Oct-18 22:44pm    
thank you for you help.thanks so much

1 solution

As mentioned in the comments, you'll want to do something much simpler like
SQL
IF EXISTS (SELECT 1 FROM table WHERE id = @id)
  BEGIN
    -- do an update
  END
ELSE
  BEGIN
    -- do an insert
  END
 
Share this answer
 
Comments
Abuamer 29-Oct-18 13:20pm    
thank you ZurdoDev. thank you for your support.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900