SQL Server - Call stored procedure in a IN Statement [duplicate] - sql

This question already has answers here:
How to SELECT FROM stored procedure
(14 answers)
Closed 8 years ago.
First of all, is it even possible ?
I have a stored procedure which looks like:
SELECT this FROM table WHERE this IN (SELECT that FROM another_table WHERE that = #Param)
I would like to replace (SELECT that FROM another_table WHERE that = #Param) by another stored procedure
I am having trouble finding the right syntax to make it work. I tried:
SELECT this FROM table WHERE this IN (EXEC new_stored_procedure #Param)
But this doesn't work. Does somebody know the right syntax to do so ?
Thank you for helping

You can create a temporary table
-- match exact columns with datatype returned from the stored procedure
create table #temp(col1 int, col2 .... )
insert into #temp(col1,...)
EXEC new_stored_procedure #Param
SELECT this FROM table WHERE this IN (select col from #temp)

You can use a Table-Valued Function
CREATE FUNCTION [dbo].[Get10Companies]
(
#DepartmentId Int
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT TOP (10) ID from company
WHERE DepartmentId = #DepartmentId
)
SELECT * from ( Select * from Get10Companies (1104)) t

Related

Returning a table in firebird 3.0 with stored function or stored procedure

I'm trying to write a stored procedure/function that returns me a table with one or multiple rows of data.
The returned data depends on a variable shown in the following sql statement:
SELECT * FROM table_name AS SD WHERE EXISTS
(SELECT DISTINCT S.PARENT_ID FROM table_name AS S
WHERE S.COMPONENT_ID = 10011 AND S.CARRIER_GROUP_ID = X AND SD.SD_ID = S.PARENT_ID)
So far I have seen that something is done like this:
CREATE FUNCTION f_test_function (X INT)
RETURNS TABLE
AS
RETURN
(SELECT * FROM table_name AS SD WHERE EXISTS
(SELECT DISTINCT S.PARENT_ID FROM table_name AS S
WHERE S.COMPONENT_ID = 10011 AND S.CARRIER_GROUP_ID = X AND SD.SD_ID = S.PARENT_ID));
Afterwards you call the function/procedure with a X value. I know that there is something wrong with the returns type but I don't know what.
Can anyone help?
What you are looking for is a selectable stored procedure. Firebird requires you to explicitly declare the columns the stored procedure returns, so something like returns table is not an option. For example:
create procedure sp_test_procedure (x integer)
returns (column1 integer, column2 varchar(50))
as
begin
for select value1, value2
from table_name SD
where exists (
SELECT DISTINCT S.PARENT_ID
FROM table_name AS S
WHERE S.COMPONENT_ID = 10011
AND S.CARRIER_GROUP_ID = :X
AND SD.SD_ID = S.PARENT_ID)
into column1, column2
do
begin
suspend;
end
end
You will need to explicitly map the columns, so a simple select * is not a good idea.
Note the use of for select, which selects zero or more rows and iterates over the cursor, and suspend, which outputs a row to be fetched from the stored procedure (in this case for each row of the cursor).
You can produce values from this procedure like:
select column1, column2
from sp_test_procedure(10)

Combine sp result in select as column

I am trying to execute sp as sub query and treat result set of sp as column of outer query . Some thing like this
Select U.FirstName , (exec SomeSP ) as columnFromSP from User U
Is this possible i searched alot but found nothing on google.
Update
I cannot use #temp table because i am trying to do without #temp table
If you are able to convert your USP to a table value UDF, you will be use the UDF in your FROM statement.
CREATE FUNCTION dbo.SomeUDF
(
-- Add the parameters for the function here
#param varchar(1000)
)
RETURNS TABLE
AS
RETURN
(
SELECT #param as Value
)
GO
SELECT
a.Value,
'B' as Value2
FROM dbo.SomeUDF('ABC') a
Not possible, but you can work around it
Create a temp table & insert the results of the procedure into
it
Now join the User table with the temporary table and select the
columns you want from both tables
This assumes however, you have a joinable expression returned from the stored proc (one that you can match to a field in the user table). If the stored procedure on returns a single row, use a condition of 1=1 or something similar
-- Declare a temp table and column(for eg you have only 1 column)
CREATE TABLE #TEMP
(
FirstName VARCHAR(50)
)
-- The results after execution will be inserted to this table
INSERT INTO #TEMP
Exec SomeSP 'Params'
-- Select records from both tables in all combinations
SELECT U.FirstName , COL1 as columnFromSP
from User U
CROSS JOIN #TEMP

Compare Actual table with #TypeTable_TVP IF Duplicate Row Exist [duplicate]

This question already has answers here:
Solutions for INSERT OR UPDATE on SQL Server
(23 answers)
Closed 8 years ago.
I want to compare Actual table with #TableType_TVP IF Duplicate Row Exist
Create procedure sp_InsertintoTable
#TableType_TVP TableType_TVP READONLY
AS
IF(Duplicate Row not Exist then)
Begin
INSERT INTO ActualTable
Select * from #TableType_TVP
Select Inserted row count also
END
else
Begin
Count Duplicate Row
End
Try this. Note: I have not tested below query in SSMS. So plz point out if any syntax error is there.
CREATE PROCEDURE proc_InsertintoTable
#TableType_TVP TABLE READONLY
AS
BEGIN
DECLARE #insertedCnt INT, #dupCnt INT
-- FETCH DUPLICATE ROW
SELECT #dupCnt = COUNT(*)
FROM ActualTable atbl
WHERE EXISTS (
SELECT 1
FROM #TableType_TVP tv
WHERE atbl.ID = tv.ID
)
-- INSERT NEW ROW
INSERT INTO ActualTable
SELECT * FROM #TableType_TVP tv
WHERE NOT EXISTS (
SELECT 1
FROM ActualTable atbl
WHERE atbl.ID = tv.ID
)
SELECT #insertedCnt = ##Rowcount
-- SELECT BOTH VARIABLE
SELECT #insertedCnt, #dupCnt
-- OR YOU CAN ALSO SET THIS 2 VARIABLE AS OUT VARIABLE
END

Using variables in stored procedures

I am using Microsoft SQL server and writing a stored procedure that contains many select statements. Here I want two variables that hold results of two select statements, and I want to add those two variables to get final result. Please help me doing this (syntax and example).
Below is the syntax for SQL Server:
DECLARE #UserEmail Varchar(250)
DECLARE #LoginID INT
SET #UserEmail = 'a#b.org'
select #LoginID = LoginID from Login L
Where L.UserEmail = #UserEmail
You should clarify which DB you are using. In MS SQL Server, you can use temporary table variable like this:
BEGIN
SELECT product_id,product_name INTO #temp1 FROM products;
SELECT product_id,product_name INTO #temp2 FROM products;
SELECT * FROM #temp1
UNION
SELECT * FROM #temp2;
END
There are several types of temporary table variable in MS SQL Server.I've used one of them.To know more about this, just search "MS SQL Server Temporary tables" in web.
EDIT:
Here is another example with another type of temporary table variable in MS SQL Server.
DECLARE #temp1 TABLE (product_id INT,product_name VARCHAR(100));
DECLARE #temp2 TABLE (product_id INT,product_name VARCHAR(100));
INSERT INTO #temp1 SELECT product_id,product_name FROM products WHERE cat_id=1;
INSERT INTO #temp2 SELECT product_id,product_name FROM products WHERE cat_id=2;
SELECT product_id,product_name
FROM #temp1
UNION
SELECT product_id,product_name
FROM #temp2;

SQL Select Statement including SP Call has syntax error

MYTABLE has ID column. However, following query generates syntax error.
SELECT ID FROM MYTABLE
WHERE ID = EXEC MY_SP ID
What do you think is wrong here?
You can't call stored procedures inline like this.
A couple of options include:
1) Execute the stored procedure and store the results in a temp table. Then use that temp table.
e.g.
CREATE TABLE #Example
(
ID INTEGER
)
INSERT #Example
EXECUTE My_SP
SELECT t.ID FROM MyTable t JOIN #Example e ON t.ID = e.ID
DROP TABLE #Example
2) convert the sproc to a user defined function which you CAN call inline
e.g.
CREATE FUNCTION dbo.MyFunc()
RETURNS TABLE
AS
RETURN
(
SELECT ID FROM SomeTable WHERE ....
)
SELECT t.ID FROM MyTable t JOIN dbo.MyFunc() f ON t.ID = f.ID
3) If the sproc returns a single ID, consider returning an OUTPUT parameter from the sproc instead and use like this:
DECLARE #ID INTEGER
EXECUTE MY_SP #ID OUTPUT
SELECT ID FROM MYTABLE
WHERE ID = #ID
I don't think you need the exec statement, just call the sp, exec is expected to be a separate statement
I don't think you can do that at all. Are you perhaps thinking of a User Defined Function rather than a stored Procedure. Based on the context, you'll need a scalar UDF.
Try this:
SELECT ID FROM MYTABLE WHERE ID = ##SPID