How can I assign an exec result to multiple sql variables? - sql

I got the returned data after executing a stored procedure named [s_sspCallSharePointWebService] in SSMS
DECLARE #sWebAddr AS NVARCHAR(MAX),
#bAPISuccess AS NVARCHAR(10),
#objSharePointXML AS XML
SET #sWebAddr = 'http://...'
EXEC [s_sspCallSharePointWebService] #sWebAddr, #bAPISuccess OUTPUT, #objSharePointXML OUTPUT
I was wondering how do I save
the data in variables instead of returning a table below.
---------------------------------------
| (No column name) | (No column name) |
---------------------------------------
| True | <XMLRoot>.... |
---------------------------------------
Additionally, I cannot insert the data into a temp table here because I had the result of EXEC sp_OAGetProperty #objecttoken, 'responseText' in [s_sspCallSharePointWebService], and then already inserted the response text into a temp table.
That would cause an error:
An INSERT EXEC statement cannot be nested

It is very ugly (for me), but this can be done using temporary tables. The temporary table is visible to all routines in the current scope.
So, you can have nesting like this:
EXEC SP1
...
EXEC SP2
...
EXEC SP3
...
and let's say EXEC SP2 is returning a row set executing a SELECT statement. So, the question is how to materialized it?
CREATE PROCEDURE dbo.SP1
AS
BEGIN;
SELECT 'SP1' AS [Caller];
EXEC dbo.SP2;
END;
GO
CREATE PROCEDURE dbo.SP2
AS
BEGIN;
SELECT 'SP2' AS [Caller];
INSERT INTO #TEST
SELECT 1;
END;
GO
CREATE TABLE #TEST
(
[ID] INT
);
EXEC dbo.SP1;
SELECT *
FROM #TEST;
The idea is to create the temporary table in the outer scope and the procedure to be sure that this table is already created and populate it. Then, use the table in the initial caller.
It's ugly for me, because if this table is not defined the routine will cause an error:
Msg 208, Level 16, State 0, Procedure dbo.SP2, Line 8 [Batch Start
Line 0] Invalid object name '#TEST'.
and it can be difficult to debug in some cases.

Instead of doing that, you can just use a table variable, and then SELECT from it to assign. Here's an example with a simple proc that returns a single row:
USE tempdb;
GO
CREATE OR ALTER PROC dbo.ReturnsOneRow
AS
BEGIN
SELECT 1 AS FirstColumn, 'Hello' AS SecondColumn, 4 AS ThirdColumn;
END;
GO
EXEC dbo.ReturnsOneRow;
GO
DECLARE #FirstColumn int;
DECLARE #SecondColumn varchar(20);
DECLARE #ThirdColumn int;
DECLARE #Row TABLE
(
FirstColumn int,
SecondColumn varchar(20),
ThirdColumn int
);
INSERT #Row
(
FirstColumn, SecondColumn, ThirdColumn
)
EXEC dbo.ReturnsOneRow;
SELECT TOP(1) #FirstColumn = r.FirstColumn,
#SecondColumn = r.SecondColumn,
#ThirdColumn = r.ThirdColumn
FROM #Row AS r;
-- Check the values
SELECT #FirstColumn, #SecondColumn, #ThirdColumn;
The table variable will be way better than a temp table in this situation, and you don't have issues with its visibility elsewhere, or having to clean it up (it's gone at the end of the batch).

Related

Adding and updating columns a posteriori in temp tables in SQL Server

I'm trying to alter an existing temp table that was created and populated in the stored procedure calling this one where I'm doing these changes. I can't change the calling stored procedure and I need to add columns to the temp table, so I tried this:
ALTER TABLE #MyTemp
ADD Column1 VARCHAR(100);
UPDATE x
SET Column1 = a.SomeColumn
FROM #MyTemp x
INNER JOIN dbo.AnotherTable a (NOLOCK) ON a.ColumnName = x.ColumnName
WHERE somecondition;
It compiles, but when I run it I get:
Msg 207, Level 16, State 1, Procedure ProcName, Line #
Invalid column name 'Column1'
Looks like this code is not even being executed.
Could someone please tell me if this is possible and how?
Thanks.
You can apparently do this, but you can't insert into the altered temp table in the same scope you alter it. Don't ask me why.
You can use dynamic sql in the the second proc to get to a lower nesting level, or call another proc. Like this:
use tempdb
go
create or alter proc a
as
begin
create table #t(id int)
exec b
select * from #t
end
go
create or alter proc b
as
begin
alter table #t add a int
exec c
end
go
create or alter proc c
as
begin
insert into #t(id,a) values (1,1)
end
go
As David Browne mentioned on his answer above, this can also be achieved by using dynamic sql, but there is a catch, and here is how:
DECLARE #ColName1 NVARCHAR(100)
DECLARE #DynamicSQL NVARCHAR(500)
SET #ColName1='Column1'
SET #DynamicSQL = 'ALTER TABLE #MyTemp ADD ['+ CAST(#ColName1 AS NVARCHAR(100)) +']
NVARCHAR(100) NULL; '
EXEC(#DynamicSQL)
SET #DynamicSQL = 'UPDATE x SET '+ CAST(#ColName1 AS NVARCHAR(100)) + = a.SomeColumn
FROM #MyTemp x INNER JOIN dbo.AnotherTable a (NOLOCK) ON a.ColumnName = x.ColumnName
WHERE somecondition;
EXEC(#DynamicSQL)
If you place the Alter and the Update in the same execution it won't work, they can't be executed at the same time, otherwise is the same problem as the static code all over again. This way is like a simulation of the extra call to another stored proc but without having an extra proc.
The stored procedure is going to have a different scope that when you try to update it after the proc runs. You will need your alter statement to be inside the proc for this to take affect, or bring the contents of the stored proc outside of the stored procedure so your temp table will share the same scope as your update statement.
For example:
CREATE PROC SPX_TEST
AS
SELECT 1 AS ABC INTO #TEMP_A
EXEC SPX_TEST
SELECT * FROM #TEMP_A
Generates an error saying the temp table does not exist.

Calling a stored procedure from another procedure

Using this table variable:
DECLARE #ReturnValue VARCHAR
DECLARE #OUT_MAIN_ERROR VARCHAR
DECLARE #Result VARCHAR(50)
BEGIN
DECLARE #TableVariable TABLE (result VARCHAR(50))
INSERT INTO #TableVariable
EXEC [dbo].[DRIVEPOOL2]
SELECT result
FROM #TableVariable
END
Using temp table:
DECLARE #ReturnValue VARCHAR
DECLARE #OUT_MAIN_ERROR VARCHAR
DECLARE #Result VARCHAR(50)
BEGIN
CREATE TABLE #kola(result VARCHAR(50))
INSERT INTO #kola
EXEC [dbo].[DRIVEPOOL2]
SELECT *
FROM #kola
DROP TABLE #kola
END
I get error:
Msg 8164, Level 16, State 1, Procedure DRIVEPOOL2, Line 45 [Batch Start Line 3]
An INSERT EXEC statement cannot be nested.
I have tried with both temp table and table variable, both are throwing the error that the INSERT EXEC statement can't be nested.
Drive Pool Procedure for Reference - Github
Seems to be duplicate with this thread: INSERT EXEC Statement cannot be nested
This error occurs when calling a stored procedure and inserting the
result of the stored procedure into a table or table variable (INSERT
... EXECUTE) and the stored procedure being called already contains an
INSERT ... EXECUTE statement within its body.
Read more about this error here:http://www.sql-server-helper.com/error-messages/msg-8164.aspx
You can try OPENROWSET to overcome this problem
INSERT INTO #TableVariable ( col1, col2, col3,... )
SELECT SP.*
FROM OPENROWSET('SQLNCLI', '[Your connection string]','EXECUTE [dbo].[DRIVEPOOL2]') AS SP

Calling stored procedure from other stored procedure

This is stored procedure #1:
ALTER PROCEDURE [dbo].[sp1]
AS
BEGIN
DECLARE #test varchar(255)
exec #test = dbo.sp2
SET NOCOUNT ON;
SELECT
CMS_ORG.description, #test
FROM
CMS_ORG
END
This is stored procedure #2:
ALTER PROCEDURE [dbo].[sp2]
AS
BEGIN
SET NOCOUNT ON;
SELECT
CMS_MAS.description + '' + CONVERT(varchar(50),
CAST(CMS_ORG.amount AS money), 1)
FROM
CMS_ORG
INNER JOIN
CMS_MAS = CMS_ORG.GUID = CMS_MAS.GUID
END
The problem is here is I was not able to execute #test in stored procedure #1 by calling the stored procedure #2. When I execute sp1, I got a null values instead but when I execute the query of sp2 in sp1, I got a correct value. May I know what is the possible solution or similar examples which can solve the issue?
Your stored proc sp2 outputs the result of a SELECT, but like all stored procs, it returns an integer using the return statement. You don't have a return statement, so Sql Server generates one for you: return 0. The purpose of the return code is to give feedback on whether it ran as expected. By convention, a return code of 0 means no errors.
This shows the difference between the return code and the output of a stored proc. Create a temp table #output to capture the rows of the SELECT that the stored proc outputs.
DECLARE #return_code int
-- capture the output of the stored proc sp2 in a temp table
create table #output( column_data varchar(max) )
insert #output( column_data )
exec #return_code = dbo.sp2 -- returns 0 because you have no RETURN statement
-- extract column_data from #output into variable #test
-- if there is more than one row in #output, it will take the last one
DECLARE #test varchar(255)
select #test = column_data from #output
Create a table variable & Use it like this :
create proc test55
as
select 55
declare #test table (Value Varchar(255))
insert into #test
exec test55
Select * from #test
Your sp2 stored procedure will return table, not varchar(255).
If you want to get a varchar(255) from sp2 you should be using function.
You can view in my example:
Define a function:
CREATE FUNCTION dbo.function1()
RETURNS varchar(255)
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE #returnVal varchar(255);
SET #returnVal = (SELECT top 1 [ProductName]
FROM [dbo].[Products])
RETURN(#returnVal);
END;
And alter SP1 like this:
ALTER PROCEDURE [dbo].[sp1]
#SMonth As Integer,
#SYear As Integer
AS
BEGIN
DECLARE #test varchar(255)
set #test = dbo.function1()
SET NOCOUNT ON;
SELECT [ProductId], #test
FROM [LearningKO].[dbo].[Products]
END

How do I combine result sets from two stored procedure calls?

I have a following stored procedure
CREATE PROCEDURE [dbo].[MyStored]
#state int
AS
SELECT blahblahblah WHERE StoredState=#state LotsOfJoinsFollow;
RETURN 0
and I'd like to call that stored procedure with #state being 0 and 1 and have the result sets returned by both calls combined with UNION semantics so that I have a new resultset that has rows from both the first call and the second call.
Something like (imaginary SQL):
(EXEC MyStored 0) UNION (EXEC MyStored 1);
How do I achieve that?
This may be oversimplifying the problem, but if you have control over the sp, just use in rather than =:
CREATE PROCEDURE [dbo].[MyStored]
AS
SELECT blahblahblah WHERE StoredState IN (0,1) LotsOfJoinsFollow;
RETURN 0
If this is not an option, just push the results of both sproc calls into a temp table:
/*Create a table with the same columns that the sproc returns*/
CREATE TABLE #tempblahblah(blahblahblah NVARCHAR(50))
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 0
INSERT #tempblahblah ( blahblahblah )
EXEC MyStored 1
SELECT * FROM #tempblahblah
create table #table (
.....
)
insert into #table exec MyStored 0
insert into #table exec MyStored 1
select * from #table
drop table #table
Alternatively to a series of statements like these:
INSERT INTO #YourTempTable
EXEC MyStored 0;
INSERT INTO #YourTempTable
EXEC MyStored 1;
you could use one INSERT ... EXEC statement like below:
INSERT INTO #YourTempTable
EXEC ('
EXEC MyStored 0;
EXEC MyStored 1;
');
The results of the two calls to MyStored would be UNIONed (or, rather, UNION ALLed), just like with the former method.
A long way would be to create a wrapper that does this - a function that takes a list of states and adds them to a final table that would be returned.
You could also have whatever technology is calling this procedure do the concatination of records (i.e. having .NET append the result set of each state you are looking into)
If you're fine with passing in a list of states to your 'state' param, you could create a dynamic sql query
CREATE PROCEDURE [dbo].[MyStored]
#state nvarchar(150)
AS
-- #state needs to be pre-formatted in a list for an in-clause
-- i.e. 1,2,10 (if it was a string list, you'd need to do use double single quotes around the items - ''1'',''2'',''10''
DECLARE #SQL nVarChar(5000) = '
SELECT blahblahblah
FROM LotsOfJoins
WHERE StoredState in (' + #state + ')'
exec sp_executeSql #sql
This works great for simple procedures; although, it can get take longer to maintain if changes are needed down the road.
.
Here is a CodeProject Article and a MS SQL Tips Article that does a better job going into details
.
EDIT: The param #state will need to be a nVarChar since your passing in a comma delimited list of int values
If the stored procedure you are calling has a temp table with the same name as one in the calling procedure you will get this error.
e.g. sp1 has temp table #results
sp2 create table #results(fields)
then trying to insert into #results in sp2 the result of calling sp1 would fail with this error. change temp table in sp2 to #result and try again and you should see this now works.

SQL Server Stored Procedure store return value

Helo,
My question is I have one Stored Procedure in SQL Server that returns counts of a field. I want to store the results of this Stored Procedure in a variable (scalar?) of a different stored procedure.
sp_My_Other_SP:
CREATE PROCEDURE [dbo].sp_My_Other_SP
#variable int OUTPUT -- The returned count
AS
BEGIN -- SP
SET NOCOUNT ON;
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
END -- SP
I currently do it like:
DECLARE #count int
EXEC sp_My_Other_SP #count OUTPUT
Then I use it like
IF (#count > 0)
BEGIN
...
END
However its returning the other Stored Procedure results as well as the main Stored Procedure results which is a problem in my .NET application.
-----------
NoColName
-----------
14
-----------
MyCol
-----------
abc
cde
efg
(Above is an attempted representation of the results sets returned)
I would like to know if there is a way to store the results of a Stored Procedure into a variable that doesn't also output it.
Thanks for any help.
You can capture the results of the stored procedure into a temp table so it is not returned by the calling stored procedure.
create table #temp (id int, val varchar(100))
insert into #temp
exec sp_My_Other_SP #value, #value, #value, #count OUTPUT
Well, the easiest way to fix this is to recode the stored proc so that the select statement that returns the 'other' result set you don't want in this case is conditionally extecuted, only when you are NOT asking for the count
Add another parameter called #GetCount
#GetCount TinyInt Defualt = 0 // or
#GetCount Bit Default = 0
Then
instead of just
Select ...
write
If #GetCount = 1
Select ...
Have you tried changing
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
to
SELECT #variable = COUNT(*) FROM blah"
-- don't do EXEC(#SQL)
?
THE FIRST PROCEDURE:
CREATE PROC DD43
#ID INT OUTPUT AS
(SELECT #ID=COUNT(*) FROM CS2)
SECOND PROCEDURE:
CREATE PROC DD45 AS
DECLARE #COUNT INT
DECLARE #COUN INT
EXEC DD43 #COUN OUT --CALLING THE FIRST PROCEDURE
SET #COUNT= (SELECT #COUN)
SELECT #COUNT
EXEC DD45