编写存储过程If / Else创建错误消息

时间:2014-03-04 18:50:18

标签: sql sql-server sql-server-2008 tsql

我正在尝试使用if / else语句创建存储过程,如果在存储过程中输入了错误的CustomerID,将导致Text消息。就像现在一样,当CustomeID中没有输入任何内容时,它只会给我一条打印行。

Create proc spCustOrder
@CustomerID VarChar(10),
@StartDate SmallDateTime = null,
@EndDate SmallDateTime =  NUll
as 
Begin
iF @CustomerID > 0

Select Distinct OrderID, OrderDate
from Customer C  join CustOrder CO on CO.CustomerID = C.CustomerID
where C.CustomerID  = @CustomerID and 
OrderDate >= Isnull(@startDate,'1900-01-01') and 
OrderDate <= IsNull(@EndDate, getDate( ))
Else 
Print 'Please enter a CustomerID'
end

基本上我不清楚的是我应该使用什么而不是“@CustomerID&gt; 0”中的“0”来使程序功能。我尝试使用CustomerID或C.和CO.CustomerID,但它说使用该命令时出错。

5 个答案:

答案 0 :(得分:2)

尝试

IF Exists(
    SELECT DISTINCT OrderID, ...
    )
ELSE
    PRINT ...
END

此外,您通常希望从存储过程返回ID或true / false值,并在调用proc的例程中执行任何打印或IO;避免在proc本身做IO.

答案 1 :(得分:2)

如果用户没有传递CustomerID,我使用了SQL Server的RAISERROR函数来抛出错误。

RETURN关键字将停止代码执行并退出,RETURN关键字后不再执行任何代码行。即如果@CustomerID的值为null或0且控件进入IF Block

Create proc spCustOrder
@CustomerID VarChar(10) = NULL,
@StartDate SmallDateTime = null,
@EndDate SmallDateTime =  NUll
AS
BEGIN
  SET NOCOUNT ON;

  IF (@CustomerID IS NULL OR @CustomerID = 0)
   BEGIN
    RAISERROR('Please enter a CustomerID',16,1)
    RETURN;
   END

    Select Distinct OrderID, OrderDate
    from Customer C  join CustOrder CO 
    on CO.CustomerID = C.CustomerID
    where C.CustomerID  = @CustomerID 
    and OrderDate >= Isnull(@startDate,'1900-01-01') 
    and OrderDate <= IsNull(@EndDate, getDate( ))

END

答案 2 :(得分:2)

您编写的查询似乎不需要Customers表,因此查询可以写成:

Select OrderID, OrderDate
from  CustOrder CO
where CO.CustomerID  = @CustomerID and 
      OrderDate >= Isnull(@startDate,'1900-01-01') and 
      OrderDate <= IsNull(@EndDate, getDate( ));

然后,您希望在表格中没有行时打印某些内容。我建议使用临时表来存储中间结果,这样就不必计算两次。结果是这样的:

Create procedure spCustOrder (
    @CustomerID VarChar(10),
    @StartDate SmallDateTime = null,
    @EndDate SmallDateTime =  NUll
)
as Begin

    Select OrderID, OrderDate
    into #tmp
    from  CustOrder CO
    where CO.CustomerID  = @CustomerID and 
          OrderDate >= Isnull(@startDate,'1900-01-01') and 
          OrderDate <= IsNull(@EndDate, getDate( ));

    if exists (select 1 from #tmp)
    begin
        select *
        from #tmp;
    end
    else 
        Print 'Please enter a CustomerID'
end;  -- spCustOrder

答案 3 :(得分:1)

说实话,我会完全不同。您可以使用以下代码:

ALTER PROCEDURE name
@CustomerID (uniqueidentifier) - varchar is very bad solution for your performance
@date1 DATE,
@date2 DATE,
@result INT OUTPUT=0 - use it as output parameter and handle text in app code
AS
BEGIN TRAN
IF EXISTS (your SELECT query. You don't need DISTINCT)
BEGIN
-- if exists do something smart. I don't know... UPDATE TABLE
IF @@error<>0
BEGIN
SET @result=1 --there was an error executing query in something smart
END
ELSE - from IF EXISTS statement
BEGIN
SET @result=2 --means no records found
END
IF @result=1
BEGIN
RETURN 1 --you know that this is error from your application code
ROLLBACK TRAN --there was an error in doing something smart
END
IF @result=2
RETURN 2 -- from your application code you know that this means no users found
END
IF RETURN=0
BEGIN
RETURN 0 -- userid found and something smart done without error :)
COMMIT TRAN
END 

希望这会有所帮助。您应该分析更多关于您将在应用程序级别执行的操作以及您希望在数据库级别执行的操作。 如果没有帮助标记,请问问题

答案 4 :(得分:0)

尝试IS NULL

    Create proc spCustOrder
    @CustomerID VarChar(10),
    @StartDate SmallDateTime = null,
    @EndDate SmallDateTime =  NUll
    as 
    Begin
    iF (@CustomerID IS NULL)
    Select Distinct OrderID, OrderDate
    from Customer C  join CustOrder CO on CO.CustomerID = C.CustomerID
    where C.CustomerID  = @CustomerID and 
    OrderDate >= Isnull(@startDate,'1900-01-01') and 
    OrderDate <= IsNull(@EndDate, getDate( ))
    Else 
    Print 'Please enter a CustomerID'
    end