Calling a stored procedure in the SELECT command - sql

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

Related

Use (insert and select) temp table in the store procedure

I use below code to create procedure to using temp table
Go
create procedure testTempTable
as
INSERT INTO #resultTbl (code,userName) SELECT code,userName FROM Customer
select * from #resultTbl
Go
When I want to run the procedure with exec testTempTable says
Invalid object name '#resultTbl'.
How can I use temp table in the procedure?
Because your temp table might not be created, so you can't get result set from #resultTbl. you can try to use SELECT ... INTO temp table or create a temp table before you use it.
create procedure testTempTable
as
BEGIN
SELECT code,userName
INTO #resultTbl
FROM Customer
SELECT *
FROM #resultTbl
END
Go

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.

Inserting output parameters from stored procedure directly

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.

How to selectively return rows inside a stored procedure on SQL Server?

I have a base stored procedure simply returning a select from the database, like this:
CREATE PROCEDURE MyProcedure
AS
BEGIN
SELECT * FROM MyTable
END
GO
But now I need to execute some logic for every row of my select. According to the result I need to return or not this row. I would have my select statement running with a cursor, checking the rule and return or not the row. Something like this:
CREATE PROCEDURE MyProcedure
AS
BEGIN
DECLARE CURSOR_MYCURSOR FOR SELECT Id, Name FROM MyTable
OPEN CURSOR_MYCURSOR
FETCH NEXT FROM CURSOR_MYCURSOR INTO #OUTPUT1, #OUTPUT2
WHILE (##FETCH_STATUS=0)
BEGIN
IF (SOME_CHECK)
SELECT #OUTPUT1, #OUTPUT2
ELSE
--WILL RETURN SOMETHING ELSE
END
END
GO
The first problem is that everytime I do SELECT #OUTPUT1, #OUTPUT2 the rows are sent back as different result sets and not in a single table as I would need.
Sure, applying some logic to a row sounds like a "FUNCTION" job. But I can't use the result of the function to filter the results being selected. That is because when my check returns false I need to select something else to replace the faulty row. So, I need to return the faulty rows so I can be aware of them and replace by some other row.
The other problem with this method is that I would need to declare quite a few variables so that I can output them through the cursor iteration. And those variables would need to follow the data types for the original table attributes and somehow not getting out of sync if something changes on the original tables.
So, what is the best approach to return a single result set based on a criteria?
Thanks in advance.
I recommend use of cursors but easy solution to your question would be to use table variable or temp table
DECLARE #MyTable TABLE
(
ColumnOne VARCHAR(20)
,ColumnTwo VARCHAR(20)
)
CREATE TABLE #MyTable
(
ColumnOne VARCHAR(20)
,ColumnTwo VARCHAR(20)
)
than inside your cursors you can insert records that match your logic
INSERT INTO #MyTable VALUES (#Output1, #Output2)
INSERT INTO #MyTable VALUES (#Output1, #Output2)
after you done with cursor just select everything from table
SELECT * FROM #MyTable
SELECT * FROM #MyTable

Create SQL Server tables and stored procedures in one script?

I have a SQL script that is setting up two database tables with their keys and constraints without any problem. I won't include the whole code but the 'skeleton' of it looks like this:
BEGIN
CREATE TABLE [table] (
)
CREATE TABLE [table2] (
)
ALTER TABLE table...
ALTER TABLE table2....
END
I am stuck trying to add stored procedures to this script though, ideally I would like to include this all within the same script. Could someone tell me how to include the following stored procedure into the above script?
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM table
END
GO
I have tried putting it towards the end of the script and have also tried with and without the BEGIN, END and GO tags but I keep getting an error that says 'incorrect syntax near PROCEDURE'.
Try it like this:
USE BDNAME
GO
BEGIN
CREATE TABLE [table] (
)
CREATE TABLE [table2] (
)
ALTER TABLE table...
ALTER TABLE table2....
END
USE BDNAME
GO
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM table
END
GO
Instead of using BEGIN END, put GO between all your Statements like Create, Alter. Also I would like to inform you that putting GO will create blocks in your script, so if you create some local variable in one block, it is not accessible in another.
CREATE Table Table1(
--Your Code
)
GO
CREATE PROCEDURE Test
#x int
AS
BEGIN
SELECT COUNT(*)
FROM Table1
END
GO
--Continue your script
Hope this helps.