Question

I am looking to return a single set of data from my stored proceedure, but the result are returning just the first of the two sets. How do I return just one set of data from the following:

SELECT TOP 1 categoryname, displaypartno
FROM Categories
WHERE catalogid = @CatalogID AND source = @Manufacturer
ORDER BY categoryid DESC

IF @@RowCount=0
BEGIN
    SELECT '' AS categoryname, displaypartno
    FROM Products
    WHERE catalogid = @CatalogID AND source = @Manufacturer
END

Because I need the second SQL to execute only if the first returns no rows, I don't think I can use a UNION.

Was it helpful?

Solution 2

SELECT TOP 1 categoryname, displaypartno
FROM (
  SELECT categoryname, displaypartno, 0 AS ResultPriority
  FROM Categories
  WHERE catalogid = @CatalogID AND source = @Manufacturer
  UNION ALL
  SELECT '' AS categoryname, displaypartno, 1 AS ResultPriority
  FROM Products
  WHERE catalogid = @CatalogID AND source = @Manufacturer
) t
ORDER BY ResultPriority, categoryid DESC

OTHER TIPS

So you want to select one row if you have a category or all matching product rows:

You were almost there but you need to put both parts in the IF...ELSE statement.

IF EXISTS (SELECT * 
           FROM Categories 
           WHERE catalogid = @CatalogID AND source = @Manufacturer) 

    SELECT TOP 1 categoryname, displaypartno
    FROM Categories
    WHERE catalogid = @CatalogID AND source = @Manufacturer
    ORDER BY categoryid DESC

ELSE

    SELECT '' AS categoryname, displaypartno
    FROM Products
    WHERE catalogid = @CatalogID AND source = @Manufacturer

END

Don't worry about calling it twice unless you have huge demands on this query or are doing something silly elsewhere it won't gause big performance issues.

You should use IF NOT EXIST ( ) function.

Create a variable table and add first step's returned items. Then, select all items from variable table you created in IF NOT EXIST condition, and write down your second step.

If you dont create variable table, you select redundant select from Categories table.

Also why you wont use @@ROWCOUNT http://www.johnpapa.net/t-sql-if-not-exists-versus-rowcount/

HERE IS THE BEST EXAMPLE. IF YOU HAVE MULTIPLE QUERY AND IF ONE QUERY GIVE ERROR, ANOTHER ONE WILL DEFINETELY RUN . LET ASSUME BOTH QUERY GIVE ERROR.. THERE ARE MULTIPLE DATA ARE PRESENT WHERE SALARY IS GREATER THAN 4000 AND FOR EMP CODE 7500 THERE IS NO DATA IS PRESENT.

*

Create Or Replace Procedure Proc_Test As
  l_Str_1    Varchar2(2000);
  l_Str_2    Varchar2(2000);
  p_Msg      Varchar2(2000);
  p_Err_Code Varchar2(2000);
  l_Count_1  Number;
  l_Count_2  Number;
Begin
  l_Str_1 := 'Select Sal  From Cmc7 Where Sal > 4000'; 
  l_Str_2 := 'SELECT sal FROM Cmc7 WHERE empno = 7500';
  Begin
    Execute Immediate l_Str_1
      Into l_Count_1;
  Dbms_Output.Put_Line(l_Count_1);
  Exception
    When Too_Many_Rows Then
      Dbms_Output.Put_Line('yahoo... many rows');
  End;
  Begin
    Execute Immediate l_Str_2
      Into l_Count_2;
  Dbms_Output.Put_Line(l_Count_2);
  Exception
    When No_Data_Found Then
      Dbms_Output.Put_Line('sorry...');
  End;
Exception
  When Others Then
    p_Msg      := Sqlerrm;
    p_Err_Code := Sqlcode;
End;

*

OUT PUT : yahoo... many rows sorry...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top