简体   繁体   中英

Azure stored procedure out value

I've been trying to assemble a stored procedure on my Azure database that when it runs the query, it returns one output value from one specific column.

The likelihood of multiple results is zero since the the table being queried has 3 columns, and the query must mach 2. Then it grabs data from another table. The key is I need the first query to output the value in order to commence the second query.

At present I have 2 procedures, I would like to have one.

Query is as such for the moment:

select 
    customers_catalogs_define.catalog_id 
from 
    customers_catalogs 
left outer join    
    customers_catalogs_define on customers_catalogs.catalog_id = customers_catalogs_define.catalog_id 
where 
    customers_catalogs.catalog_unique_identifier = @catalog_unique 
    AND customers_catalogs_define.customer_id = @customer_id 

The output of course is the catalog_id . From that I take it into another query which I have that does the actual list retrieval. At the very least I would like to add a line that simply states @catalog_id = output

Thanks

You have two options basically to have this work :

Since I haven't seen your second query, just make sure its querying the correct table listed below as CUSTOMERS_CATALOGS_DEFINE

Using a variable as you suggested:

DECLARE @CATALOG_ID INT

SET @CATALOG_ID = (
    SELECT CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID 
    FROM CUSTOMERS_CATALOGS 
    LEFT OUTER JOIN CUSTOMERS_CATALOGS_DEFINE 
    ON CUSTOMERS_CATALOGS.CATALOG_ID = CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID 
    WHERE CUSTOMERS_CATALOGS.CATALOG_UNIQUE_IDENTIFIER = @CATALOG_UNIQUE 
    AND CUSTOMERS_CATALOGS_DEFINE.CUSTOMER_ID = @CUSTOMER_ID )

SELECT * 
FROM CUSTOMERS_CATALOGS_DEFINE
WHERE CATALOG_ID = @CATALOG_ID

Second option would be to do it in one query:

SELECT * 
FROM CUSTOMERS_CATALOGS_DEFINE
WHERE CATALOG_ID IN (
    SELECT CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID 
    FROM CUSTOMERS_CATALOGS 
    LEFT OUTER JOIN CUSTOMERS_CATALOGS_DEFINE 
    ON CUSTOMERS_CATALOGS.CATALOG_ID = CUSTOMERS_CATALOGS_DEFINE.CATALOG_ID 
    WHERE CUSTOMERS_CATALOGS.CATALOG_UNIQUE_IDENTIFIER = @CATALOG_UNIQUE 
    AND CUSTOMERS_CATALOGS_DEFINE.CUSTOMER_ID = @CUSTOMER_ID
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM