我想要读取SQl中的值。
我正在创建一个采购订单如果假设任何正文更新了库存的价格,那么我首先检查价格是否可用。
如果该价格不可用,那么我首先将该价格插入数据库&然后用库存映射新价格。
我已经实现了这个功能,但我现在已经为此编写了五个内联查询 改变代码和替换为单个存储过程。 &安培;我如何将逻辑写入SQL
这是我的代码及解释
//Checking that Buying Price Is Exist or not
//string CheckingIBM = "select * from RS_Inventory_Buying_Master where buying_price ='" + UpdatedPrice + "'";
//cm.TableConnect(CheckingIBM);
//If Buying Price is Exist then Update PIIM table with new buying_product_id
if (cmIS_Price_Exist.rs.Read())
{
//If Buying Price is Exist then Update PIIM table with new buying_product_id
common cm1 = new common();
string BuyingProductId = cmIS_Price_Exist.rs["buying_product_id"].ToString();
string UpdatePIIM = "update RS_Purchase_Invoice_Info_Master set buying_product_id = '" + BuyingProductId + "', qty = '" + UpdatedQuantity + "',tax_id ='" + TaxDetails + "',picreated = 1 where purchase_order_no = '" + PO + "' and product_id = '" + ProductId + "'";
cm1.TableInsert(UpdatePIIM);
cm1.con.Close();
}
//If Buying Price does not Exist then first Insert the price & then Update the other tables
else
{
//If Price is not exist then firsrt insert the price
common cm2 = new common();
string InsertBuyingPrice = "insert into RS_Inventory_Buying_Master (buying_price,latest) values ('" + UpdatedPrice + "','0')";
cm2.TableInsert(InsertBuyingPrice);
cm2.con.Close();
//After inserting the price find the buying product Id of that price
common cm3 = new common();
string FindingUpdatedPrice = "select * from RS_Inventory_Buying_Master where buying_price ='" + UpdatedPrice + "'";
cm3.TableConnect(FindingUpdatedPrice);
//Now finallly after finding the buying price id by using the inserted Price. Now update the buying product id of PIIM
if (cm3.rs.Read())
{
string BuyingProductId = cm3.rs["buying_product_id"].ToString();
//Now finallly after finding the buying price id. Now update the buying product id of PIIM
common cm4 = new common();
string UpdatePIIM = "update RS_Purchase_Invoice_Info_Master set buying_product_id = '" + BuyingProductId + "', qty = '" + UpdatedQuantity + "',tax_id ='" + TaxDetails + "',picreated = 1 where purchase_order_no = '" + PO + "' and product_id = '" + ProductId + "'";
cm4.TableInsert(UpdatePIIM);
cm4.con.Close();
}
cm3.con.Close();
}
任何建议都将受到赞赏。
答案 0 :(得分:1)
declare @BuyingProductId varchar(50)
set @BuyingProductId = (select isnull(buying_product_id, '') from RS_Inventory_Buying_Master where buying_price = @UpdatedPrice)
if(@BuyingProductId <> '')
begin
--your update query
update RS_Purchase_Invoice_Info_Master set buying_product_id = @BuyingProductId ,
qty = @UpdatedQuantity ,tax_id = @TaxDetails ,picreated = 1
where purchase_order_no = @PO
and product_id = @ProductId ;
end
else
begin
--your insert query
insert into RS_Inventory_Buying_Master (buying_price,latest)
values (@UpdatedPrice,'0')
set @BuyingProductId = (SELECT @@IDENTITY)
update RS_Purchase_Invoice_Info_Master set buying_product_id = @BuyingProductId ,
qty = @UpdatedQuantity ,tax_id = @TaxDetails ,picreated = 1
where purchase_order_no = @PO
and product_id = @ProductId ;
end
检查此查询。请确保创建新的sp并提供@UpdatedQuantity等所有的值。