Get only one column from stored procedure using T-SQL - sql

I'm trying to select only one column from a stored procedure that I didn't write, I'm just a consumer.
When I execute this code for example:
SELECT ID
FROM OPENROWSET('MSOLEDBSQL',
'Server=ServerName;Database=DatabaseName;UID=Username;Pwd=Password;',
N'EXEC stored_procedure_name #param1 = 1, #param2 = 11');
I get this error:
The metadata could not be determined because statement 'INSERT INTO #Temp.....' in procedure 'stored_procedure_name' uses a temp table.
Can you please suggest a solution, or any other way to call a stored procedure and retrieve only one column?

Related

Why is my stored procedure query returning extra results?

I have the following query inside of a stored procedure:
CREATE PROCEDURE [s_Staff_ByLikeLastNmByLikeFirstNm]
#LastNm varchar(10),
#FirstNm varchar(10)
/*WITH ENCRYPTION*/
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT
Staff.FirstNm,
Staff.LastNm
FROM
Staff
WHERE
Staff.LastNm LIKE #LastNm + '%'
AND Staff.FirstNm LIKE #FirstNm + '%'
If I input 'Christiansen' in my query for #LastNm, when I execute the stored procedure, it is returning me both 'Christiansen' and 'Christianson', and performing more of a SOUNDEX search.
How can I fix this?
If I run do a select outside of the stored procedure, I am getting the correct results.
'Christiansen' has 12 characters in it.
You have defined the parameters to the stored procedure to have a length of 10, so the value is truncated to 'Christians'.
Fix the length parameter in the declaration of the stored procedure.

Getting result from SQL Server Stored Procedure

I need to get the result from a stored procedure in a MSSQL Server database. I tried different ways:
Using a Table Input Step, with this query: exec dbo.storedproc #param1 = 5, #param2 = 12, #param3 = null, #param4 = null, #param5 = '2017/08/29', #param6 = 1. When I right click on it -> Show output fields, it shows me the output fields from the stored procedure, but I don't know how to get the results and dump them into another table.
Using the Call DB Procedure step. With this one, I set up the input parameters, and tried to pass them through a Generate Rows step. But, With this one I don't even get the output parameters.
Please help me figure out how to do this.
With regards,
As #Brian.D.Myers suggested, the best approach is execute the stored procedure in a Table input step, and connect it to a Table output step. That, alongside the fact that the stored procedure must be executed with the following syntax: exec stored_procedure_name [#param = value,]
Thanks a lot!

How to stop results of nested stored procedure showing up at top level?

I've made an alteration to an existing stored procedure (dbo.pr1) so that it now calls a second stored procedure (dbo.pr2).
Both of these stored procedures return data with a final SELECT query.
In dbo.pr1 I've now added the line:
EXEC #var1 = dbo.pr2
I've done this in order to assign the values in dbo.pr2 to the variable #var1 (dbo.pr2 returns a single bit).
However, now when I execute dbo.pr1 I get two results back instead of the expected one. I get the SELECT query results at the end of dbo.pr1 but I also get the SELECT query result from dbo.pr2.
I cannot alter dbo.pr2 as it's being used elsewhere in the system. Is there a way that I can stop its result showing up when I execute dbo.pr1?
Do something with what is returned in the caller,
SET NOCOUNT ON;
...
DECLARE #resultsOfPr2 TABLE
(
...
);
INSERT #resultsOfPr2
EXEC #var1 = [dbo].[pr2];
Fiddle here
Note: I'm assuming [dbo].[pr2] selects a single result set.

Insert SQL stored procedure values into another stored procedure

I have a large stored procedure that returns a record for a person, there are four fields that I need to return very specific values for. I have another stored procedure that performs this specific action.
The small stored procedure is as follows:
SELECT TOP 1
wea.field,
wea.degree,
wea.degreeyear,
wpp.ProgramCategory
FROM dbo.webeventaffiliation wea
LEFT JOIN dbo.WebProgramParticipants wpp
ON
wea.userid = wpp.UserID AND
wea.eventid = wpp.eventid
INNER JOIN dbo.WebProgramCategoryDescriptions wpcd
ON
wpcd.ProgramCategory = wpp.ProgramCategory
WHERE wea.UserID = #UserID
ORDER BY wea.datelastmodified DESC
LARGE STORED PROCEDURE SAMPLE RETURN DATA:
Name: XXXXX
Address: XXXXX
Field: [small stored procedure value]
Degree: [small stored procedure value]
DegreeYear: [small stored procedure value]
ProgramCategory: [small stored procedure value]
My question is how do I get the 4 data items from this stored procedure into their respective columns within the dataset that is returned from the large stored procedure?
Using a table-valued function instead of a stored procedure could be helpful. You will be able to use the TVF just like a table ie:
SELECT
COLUMNS_NAMES
FROM
TVF(PARAMS)
As your small stored procedure doesn't write anything, you could just write it as a table valued function.
You can then apply the function to an entire data-set by using APPLY.
(Table valued functions that are written INLINE (not multi-statement) are then explanded macro-like to execute extremely efficiently. This is perfect for your description as the function would just be a single SELECT statement already.)
The Function:
CREATE FUNCTION dbo.your_function(#user_id AS INT)
RETURNS TABLE
AS
RETURN
<your query>
The function used in a query inside your big SP:
SELECT
...,
user_detail.field,
user_detail.degree,
user_detail.degreeyear,
user_detail.programcategory
FROM
...
CROSS APPLY
dbo.your_function(some_table.user_id) AS user_detail
In general I use functions to encapsulate queries, and only wrap them up in Stored Procedures if...
1) I need to write data. (Functions can't INSERT, UPDATE or DELETE)
2) I want to create an API like interface to client applications.
Since you're getting only one row with four values you could use OUTPUT parameters:
EXECUTE SomeSmallerProcedure
#field OUTPUT, #degree OUTPUT, #degreeyear OUTPUT, #ProgramCategory OUTPUT;
Your procedure listed above would change to:
ALTER PROCEDURE SomeSmallerProcedure
#field varchar(255) OUTPUT,
#degree varchar(255) OUTPUT,
#degreeyear varchar(255) OUTPUT,
#ProgramCategory varchar(255) OUTPUT
AS BEGIN SET NOCOUNT ON;
SELECT TOP 1
#field = wea.field,
#degree = wea.degree,
#degreeyear = wea.degreeyear,
#ProgramCategory = wpp.ProgramCategory
-- ... rest as before
The signature of your procedure above would have to include those parameters explicitly for OUTPUT.

Insert into table from stored proc

I am trying to use a stored procedure that contains two different cursors as table input like such:
INSERT INTO table1 EXEC * FROM tblDailySales
The stored proc contains two cursors - I did not run just using.
I get the following error:
A cursor with the name 'csrDistricts' does not exist.
I also, get this error
An INSERT EXEC statement cannot be nested
The stored proc contains no EXEC that I can see.
What kind of stored proc other than simple SELECT can be used as source for table?
Is table1 already defined? If so all you should have to do is
INSERT INTO table1
EXEC storedProcedureName
Now, the trick is, the stored procedure will only be able to return one result set and insert into the table.
If you need to insert two different result sets, you'll have to gather then in two different stored procedures, then run two INSERT statements.
If you must do them at once, you'll need to do the insert from within the stored procedure.