简体   繁体   English

储存程序

[英]Stored Procedure

I am using SQl-server 2012 and this code is inside in a Stored Procedure. 我正在使用SQl-server 2012,此代码位于存储过程中。

These are my SQL queries.I want to execute "Insert" query when "Update" query not executed. 这些是我的SQL查询。我想在未执行“更新”查询时执行“插入”查询。 If Update query executed,then insert query should not be executed. 如果执行更新查询,则不应执行插入查询。

Update tblStock Set Balance= Balance +  @ReduceRawQty 
    Where LocCode=@LocCode 
    AND ItemCode=@rawitemcode 
    and CostPrice=@rawcostprice

Insert Into tblStock(LocCode, ItemCode, CostPrice, Balance,Transfer,PCode)
    Values(@LocCode,@rawitemcode,@RawCostPrice,@RawQty,0,@PCode)    

Is there any method to do this ,please help me 有什么方法可以做到,请帮帮我

You can check @@ROWCOUNT to see how many rows were affected by the UPDATE statement: 您可以检查@@ROWCOUNT以查看UPDATE语句影响了多少行:

UPDATE ...

IF @@ROWCOUNT = 0
    INSERT ...

Alternatively you can use EXISTS to check for a row first and decide which operation to perform: 或者,您可以使用EXISTS首先检查一行并确定要执行的操作:

IF EXISTS (SELECT 1 FROM tblStock WHERE LocCode=@LocCode AND ItemCode=@rawitemcode and CostPrice=@rawcostprice)
    UPDATE ...
ELSE
    INSERT ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM