Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table called prod_inventory
with columns like this
prod-grp-id, prod-id ,qty, Warehouse-id
1              1      10     1
1              1      5      2 
2              2      30     1
2              2      25     2



now i want to transfer products from one warehouse to another warehouse
than it should update the qty in warehouses.

like if i transfer 5 product having prod-grp-id=1 and prod_id=1 from warehouse-id 1 to warehouse id 2 than it should update the qty like qty=qty-5(result 5) in warehouse_Id=1 and qty=qty+5(results 10) in warehouse_id 2 .

But it should check for the condition too. if we are transferring more no. of products than the available than it should not perform the transaction.qty should not be updated to negative

how to write query for this plz help me.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 25-Mar-12 2:56am
v4

You can't update two tables or two different rows in one statement - you need to do two UPDATE statements, preferably as part of a TRANSACTION.

Personnaly, I would create a Stored Procedure to do them both within a transaction:


SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE MoveGoods
AS
BEGIN
	SET NOCOUNT ON;
    BEGIN TRANSACTION

    UPDATE prod_inventory SET qty = qty-5 WHERE prodId = 1 AND WarehouseId=1
    UPDATE prod_inventory SET qty = qty+5 WHERE prodId = 1 AND WarehouseId=2

    COMMIT
END
GO
 
Share this answer
 
Comments
psychic6000 25-Mar-12 11:13am    
answer is good, just that, use a function instead procedure, so instead of prodID= [constant] etc, use arguments, :)
ashu1810 26-Mar-12 5:42am    
Thank you for the reply, i used it like this.

ALTER procedure [dbo].[SP] (@PGID INT ,@PID int,@qty int,@FWRID int , @TWRID INT)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION

UPDATE PROD_INVENTORY SET QTY = QTY-@qty WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@FWRID
UPDATE PROD_INVENTORY SET QTY = QTY+@qty WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@TWRID


COMMIT
END

i want to put some condition before the transaction, It should check for the qty whether it is available in the warehouse or not form where we are transferring to other warehouse.If there is no sufficient qty than it should give some message(instead of generating the negative values).

please help
OriginalGriff 26-Mar-12 6:50am    
Look at including an SQL IF statement:
IF (SELECT qty FROM prod_inventory where prodId=2 AND WarehouseId=1) >5
BEGIN
...do your transaction
END
ELSE
BEGIN
...report your problem.
END
Hi ,

you can use if else statements in it

SQL
IF (SELECT  QTY FROM PROD_INVENTORY WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@FWRID) < $15
BEGIN
   //Do your transaction
END
ELSE
    @msg='No Sufficient Goods';


Here @msg is out parameter

I hope you know out parameters in sqlserver
 
Share this answer
 
Comments
ashu1810 26-Mar-12 9:21am    
yes I know.
Thanks for the code, and how can I catch that error in my application.
Muralikrishna8811 26-Mar-12 10:21am    
catching error in application means you want that message ?

just using that out parameter

you'll get that message
ashu1810 27-Mar-12 3:06am    
thank you for the reply
I did something like this

protected void Button1_Click(object sender, EventArgs e)

{

VIEW();
for (int i = 0; i < GridView1.Rows.Count; i++)
{

int PGID = Convert.ToInt32(ds.Tables["T"].Rows[i][0].ToString());
int PID = Convert.ToInt32(ds.Tables["T"].Rows[i][1].ToString());
int QTY = Convert.ToInt32(ds.Tables["T"].Rows[i][3].ToString());
int FWRID = Convert.ToInt32(ds.Tables["T"].Rows[i][4].ToString());
int TWRID = Convert.ToInt32(ds.Tables["T"].Rows[i][5].ToString());

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.CommandText = "SP";
cmd.Parameters.AddWithValue("@PGID", PGID);
cmd.Parameters.AddWithValue("@PID", PID);
cmd.Parameters.AddWithValue("@qty", QTY);
cmd.Parameters.AddWithValue("@FWRID", FWRID);
cmd.Parameters.AddWithValue("@TWRID", TWRID);

cmd.Parameters.Add("@MESSG", SqlDbType.VarChar).Direction = ParameterDirection.Output;



cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Label3.Text = cmd.Parameters["@MESSG"].Value.ToString();

}
and the SP

<pre>ALTER procedure [dbo].[SP] (@PGID INT ,@PID int,@qty int,@FWRID int , @TWRID INT,@MESSG varchar(50) output)
AS
IF (SELECT QTY FROM PROD_INVENTORY WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@FWRID) >@qty

BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION

UPDATE PROD_INVENTORY SET QTY = QTY-@qty WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@FWRID
UPDATE PROD_INVENTORY SET QTY = QTY+@qty WHERE PROD_GRP_ID=@PGID and PRODUCT_ID=@PID AND WAREHOUSE_ID=@TWRID


COMMIT
END
ELSE
BEGIN
SET @MESSG='No Sufficient Products';
END

</pre>

While executing it is showing an Error Message


String[5]: the Size property has an invalid size of 0.

Please help
Muralikrishna8811 27-Mar-12 4:49am    
hi can you specify the line where it exactly it raising
ashu1810 28-Mar-12 4:00am    
in the line.
while executing the command.
cmd.ExecuteNonQuery();
Plesae help.

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