本文介绍了创建存储过程时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用存储过程来实现两个查询
首先,我想使用计数查询来检查产品是否已经退出.那么如果产品没有退出.那我想插入数据.否则,抛出一个错误,表明记录已经退出.
我想在存储过程中实现这种事情..但不知道该怎么办..
i want to implement two queries using store procedure
first i want to use count query to check wheather the product already exits or not. then if the product doesn''t exit. then i want to insert a data. other wise throw an error that record already exits.
i want to implement such kind of thing in storeprocedure.. but don''t know how to do..
query= Select Count * from ShoppingCart where Productid='"+@productid+"'
int count= obj.ohh(query)
if(Count>)
{
Response.Redirect("Error Page.aspx")
}
else
INSERT INTO SHOPPINGCART
(CUSTOMERID,PRODUCTID,QUANTITY)
VALUES(@CustomerID,@ProductID,@Quantity
)
推荐答案
query = "declare count int;set count = select count (*) from table where blah=blah; if (count = 0) insert into table blah blah;"
SqlCommand cmd = new SqlCommand(query, conn);
int recsAffected = sqlCommand.ExecuteNonQuery();
if (recsAffected == 0)
{
Response.Redirect("Errorpage.aspx");
}
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddToCart]
@CustomerID varchar(50),
@ProductID int,
@Quantity int
As
If Not Exists (Select 1 From table where ProductID = @ProductId)
INSERT INTO ShoppingCart (CustomerID,Quantity,ProductID) VALUES(@CustomerID,@Quantity,@ProductID)
这篇关于创建存储过程时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!