Board index » cppbuilder » 2 selects becoming one returned query

2 selects becoming one returned query


2007-08-17 01:49:06 AM
cppbuilder85
Here is my problem:
I want to create a storedProc that will query if a given value is in 2
different tables, and if one of the tables (or both) contain that value,
I would like to have a recordset sent to my TADOQuery component.
CREATE PROCEDURE dbo.sp_CanDeleteItem
--Required Parameters
@Supplier INT = NULL
AS
'This query will return a set if the supplier in question is present
SELECT Supplier_ID
FROM SupplierItems
WHERE Supplier_ID = @Supplier
'Now, there is another table <SupplierPurchases>
SELECT Supplier_ID
FROM SupplierPurchases
WHERE Supplier_ID = @Supplier
GO
How do I combine both queries to be something like this as in C++ code
but have the SQL version of the OR
if( SupItmFound(iSup) || SupPurFound(iSup) )
{
//...
}
 
 

Re:2 selects becoming one returned query

Assuming you are using MS SQL Server. If you need query, use UNION:
SELECT Supplier_ID
FROM SupplierItems
WHERE Supplier_ID = @Supplier
UNION
SELECT Supplier_ID
FROM SupplierPurchases
WHERE Supplier_ID = @Supplier
Or you may use function that returns boolean. If @Supplier is NULL result
will depend on ANSI_NULLS.
//------------------------------------------
Regards,
Vassiliev V. V.
www.managed-vcl.com - using .Net objects in Delphi for Win32 +
ADO.Net
www.oledbdirect.com - The fastest way to access MS SQL Server,
MS Jet (Access) and Interbase (through OLEDB)
"Stephane Baillargeon" < XXXX@XXXXX.COM >???????/???????? ?
???????? ?????????: news:46c48e15$ XXXX@XXXXX.COM ...
Quote
Here is my problem:

I want to create a storedProc that will query if a given value is in 2
different tables, and if one of the tables (or both) contain that value, I
would like to have a recordset sent to my TADOQuery component.


CREATE PROCEDURE dbo.sp_CanDeleteItem

--Required Parameters
@Supplier INT = NULL

AS

'This query will return a set if the supplier in question is present
SELECT Supplier_ID
FROM SupplierItems
WHERE Supplier_ID = @Supplier

'Now, there is another table <SupplierPurchases>
SELECT Supplier_ID
FROM SupplierPurchases
WHERE Supplier_ID = @Supplier

GO


How do I combine both queries to be something like this as in C++ code but
have the SQL version of the OR
if( SupItmFound(iSup) || SupPurFound(iSup) )
{
//...
}