创建存储过程时出现问题 [英] problem in creating store procedure

查看:76
本文介绍了创建存储过程时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用存储过程来实现两个查询


首先,我想使用计数查询来检查产品是否已经退出.那么如果产品没有退出.那我想插入数据.否则,抛出一个错误,表明记录已经退出.

我想在存储过程中实现这种事情..但不知道该怎么办..

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

)

推荐答案

类似的事情(可能需要调整):

Something like this (probably needs tweaking):

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");
}



您可以简单地

Hi
You can simply do

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)


创建过程[PROCEDURENAME]
@PRODUCTID INT,
AS

宣告@COUNT INT,
SELECT @COUNT = SELECT *从购物车中PRODUCTID = @ PRODUCTID
IF(@ COUNT> 0)
RAISERROR(``您的味精'',16,1)
ELSE
插入购物车
(客户编号,产品编号,数量)
VALUES(@ CustomerID,@ ProductID,@ Quantity)
CREATE PROCEDURE [PROCEDURENAME]
@PRODUCTID INT,
AS

DECLARE @COUNT INT,
SELECT @COUNT = SELECT * FROM SHOPPINGCART WHERE PRODUCTID=@PRODUCTID
IF(@COUNT>0)
RAISERROR(''YOUR MSG'', 16, 1)
ELSE
INSERT INTO SHOPPINGCART
(CUSTOMERID,PRODUCTID,QUANTITY)
VALUES(@CustomerID,@ProductID,@Quantity)


这篇关于创建存储过程时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆