Inserting output parameters from stored procedure directly - sql

I have a stored procedure that returns a pair of output parameters - the ID and the computed value. Is it possible to use a trigger with an insert statement, that inserts those two values directly? Something like this
CREATE TRIGGER Trig_FirstTable ON SecondTable AFTER UPDATE
AS
BEGIN
INSERT INTO FirstTable (OtherID, OtherValue)
VALUES (#otherID, #otherValue)
FROM StoredProcedure inserted.ID, inserted.Value, #otherID OUTPUT, #otherValue OUTPUT
END

According to the MSDN documentation you can use INSERT into with EXEC.
They give the following example:
--INSERT...EXECUTE procedure example
INSERT author_sales EXECUTE get_author_sales
But I think your stored procedure needs a SELECT statement to return the data instead of only filling the output parameters.

You can insert from SP like that:
drop procedure uspTest
GO
create procedure uspTest
AS
select 1 as id, 'x' as val
union all
select 2,'y'
GO
drop table #temp
GO
create table #temp (
id int,
val char(1)
)
GO
insert into #temp (id,val)
EXECUTE uspTest
GO
select
*
from #temp
But you cannot select a subset of columns, so this method will obviously fail if you add more outputs to your SP in the future:
insert into #temp (id)
EXECUTE uspTest
Another way is to store SP results in variables, and then use them for insert.

Related

I want to Execute stored procedure into temporary table but it gives me an error You must specify a table where to make the selection

This my code where i want to insert the result of my stored procedure into #TempTable
SELECT * INTO #Tempannuelguiftrecap
EXECUTE PSGetDetailMensuelPPM #Year,#Mois
You can't SELECT from a Stored Procedure. Something like SELECT * FROM EXECUTE dbo.MySP isn't going to work.
You can still INSERT the data from a Stored Procedure into a table, however, you must first define the table, and then INSERT the data. This is Pseudo-SQL as we have no definitions of your objects, however, this should get you on the right path:
CREATE TABLE #MyTempTable({Column1} {Data Type}[,
{Other Column(s)} {Column DataType(s)} ..... ]);
INSERT INTO #MyTempTable ({Columns List})
EXECUTE dbo.MyStoredProcedure #MyParam;

Nested Stored Procedure

How can i insert values of a nested stored procedure into a table.
For example if i created a stored procedure like this.
Create procedure New
begin
create table #tmp
(
id int,
name varchar(50)
);
insert into #tmp
exec stored_procedure 1,2;
insert into #tmp
exec stored_procedure 1,2;
select * from #tmp
end
Now if I execute this command, SQL Server will display error:
insert into #table
exec New;
Does anyone have a solution for this problem? Please share
Right now you’re not returning any data from your procedure New. If you want it to return data for your calling code to insert you’ll need a select statement in it.
Eg add a line at the end:
SELECT * FROM #tmp;
Also, you can’t nest INSERT EXEC statements. See this answer for more details
You can't do a insert with de values returned by a Stored Procedure.
In your case the easiest way is to change de stored procedures by table functions.
You can find more about table functions here
My opinion is to make it simple if u can.

Calling a stored procedure in the SELECT command

I have a stored procedure ModifiedName, is it possible to list the name of all the rows in a table and display the "modified name" next to them?
Something along:
SELECT name, EXEC ModifiedName name
FROM table
but which would work...
First you have to write a function instead of procedure. You cannot call a procedure inside a select statement ,But a function can.
Inside the function write your logic to modify the name and use it in the select query.
For example write a function like below.(This will append 'Mr.' with name )
CREATE FUNCTION dbo.ModifiedName ( #name VARCHAR(50))
RETURNS VARCHAR(150)
AS
BEGIN
DECLARE #ModifiedName VARCHAR(150)
SET #ModifiedName= 'Mr.'+#name
RETURN #ModifiedName
END
And use the below script to get the original name and modified name in the select list.
SELECT name, dbo.ModifiedName (name)
FROM table
You can do
CREATE TABLE SomeName(... appropriate definitions ...);
INSERT INTO SomeName
EXEC YourSP;
SELECT * FROM SomeName;
But - to be honest - I think you are trying something you should solve in another way...
Create a function
Try to inline your logic
UPDATE This is for #NEER
Hi NEER, don't know what you mean with your comment, but try this:
CREATE PROCEDURE dbo.tmpTest AS
BEGIN
SELECT TOP 3 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
END
GO
CREATE TABLE #tmpTable(ColName NVARCHAR(MAX));
INSERT INTO #tmpTable
EXEC dbo.tmpTest;
SELECT * FROM #tmpTable;
GO
DECLARE #tmpTbl TABLE(ColName NVARCHAR(MAX));
INSERT INTO #tmpTbl
EXEC dbo.tmpTest;
SELECT * FROM #tmpTbl;
GO
DROP TABLE #tmpTable;
DROP PROCEDURE dbo.tmpTest;
But - in general - if you just want to read data one should rather use a VIEW or an inline TVF. Procedures are designed to do something...
If parameterless sp, you can with a temp table. Otherwise you should use a function.
CREATE TABLE #tmp (Name INT)
INSERT INTO #tmp
EXEC ModifiedName
SELECT name, (SELECT TOP 1 Name FROM #tmp) name
FROM table

Updating a Table after some values are inserted into it in SQL Server 2008

I am trying to write a stored procedure in SQL Server 2008 which updates a table after some values are inserted into the table.
My stored procedure takes the values from a DMV and stores them in a table. In the same procedure after insert query, I have written an update query for the same table.
Insert results are populated fine, but the results of updates are getting lost.
But when I try to do only inserts in the stored procedure and I execute the update query manually everything is fine.
Why it is happening like this?
there should not be a problem in this.
below code working as expected.
create procedure dbo.test
as
begin
create table #temp (
name varchar(100) ,
id int
)
insert #temp
select name ,
id
from master..sysobjects
update #temp
set name='ALL Same'
from #temp
select * from #temp
drop table #temp
end
go
Best approach is to use Trigger, sample of AFTER UPDATE trigger is below:
ALTER TRIGGER [dbo].[tr_MyTriggerName]
ON [dbo].[MyTableName] AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
--if MyColumnName is updated the do..
IF UPDATE (MyColumnName)
BEGIN
UPDATE MyTableName
SET AnotherColumnInMyTable = someValue
FROM MyTableName
INNER JOIN Inserted
ON MyTableName.PrimaryKeyColumn = Inserted.PrimaryKeyColumn
END
END

Insert into Table select result set from stored procedure but column count is not same

I need something like that which is of course not working.
insert into Table1
(
Id,
Value
)
select Id, value from
(
exec MySPReturning10Columns
)
I wanted to populate Table1 from result set returned by MySPReturning10Columns. Here the SP is returning 10 columns and the table has just 2 columns.
The following way works as long as table and result set from SP have same number of columns but in my case they are not same.
INSERT INTO TableWith2Columns
EXEC usp_MySPReturning2Columns;
Also, I want to avoid adding "." as linked server just to make openquery and openrowset work anyhow.
Is there a way not to have define table strucutre in temp table (all columns with datatypes and lenght)? Something like CTE.
You could use a temporary table as a go-between:
insert into #TempTable exec MySP
insert into Table1 (id, value) select id, value from #TempTable
You could solve the problem in two steps by doing the insert from the stored procedure into a temporary table, then do the insert selecting just the columns you want from the temporary table.
Information on temporary tables: http://www.sqlteam.com/article/temporary-tables
-- Well, declare a temp table or a table var, depending on the number of rows expected
-- from the SP. This table will be basically the result set of your SP.
DECLARE #spResult AS TABLE
(
ID INT,
VALUE FLOAT,
....
);
-- Get the result set of the SP into the temp table.
INSERT #spResult EXEC STORED_PROC;
-- Now you can query the SP's result set for ID and Value;
INSERT Table1 (ID, VALUE)
SELECT ID, VALUE FROM #spResult;
You dont need to create a temporary table, you can do it with single query by creating temporary view like this
with tempView as EXEC MySPReturning10Columns insert into Table1 select id, value from tempView
The temporary view disappears as soon as the statement finishes execution