Execute stored procedure SQL Server 2016 - sql-server-2016

I am trying to execute the created stored procedure from inside the stored procedure. How can I do it ?
DROP PROCEDURE IF EXISTS dbo.ListAllPersonnel
Go
CREATE PROCEDURE ListAllPersonnel
AS
SELECT *
FROM personnel
EXECUTE dbo.ListAllPersonnel
GO

Related

Check if stored procedure exists accordingly create or alter

I want to check if a stored procedure already defined in db. If defined I have to execute alter script else create.
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'EmployeeInternalReferenceNumber_Copy')
BEGIN
DROP Procedure [dbo].[EmployeeInternalReferenceNumber_Copy]
END
Go
CREATE PROCEDURE [dbo].[EmployeeInternal]
Above script is not working as I do not want to drop proc just alter or create. I am using SP2
In SQL Server version 2016+, you can simply use:
CREATE OR ALTER PROCEDURE [dbo].[EmployeeInternal]
AS
You can use drop procedure if exists instead of if exists
DROP PROCEDURE IF EXISTS EmployeeInternalReferenceNumber_Copy;
CREATE PROCEDURE [dbo].[EmployeeInternal]
Or if you want to alter procedure EmployeeInternal then use alter instead of create
ALTER PROCEDURE [dbo].[EmployeeInternal]

SQL Server Alert on Missing Stored Procedure

I’m cleaning out a large database and am going to be removing a lot of stored procedures. I can be pretty certain but not 100% that the SP isn’t being used. How can I be alerted that something is calling a missing stored procedure, and throwing the error:
Could not find stored procedure ‘X’
You could create synonyms for each deleted procedure and substitute a different procedure (with matching parameters). In the example below the procedure doesn't take any parameters.
The code:
create "replacement" proc called 'dbo.test_replacement_proc'
exec 'dbo.test_replacement_proc'
create "test" proc called 'dbo.test_proc'
exec 'dbo.test_proc'
drop 'dbo.test_proc'
create synonym 'dbo.test_proc' for 'dbo.test_replacement_proc'
exec 'dbo.test_proc'
When #4 executes it's the original procedure. When #7 executes it's the replacement procedure.
drop proc if exists dbo.test_replacement_proc;
go
create proc dbo.test_replacement_proc
as
set nocount on;
select 'Could not find stored procedure' replacement_message
go
-- run the proc
exec dbo.test_replacement_proc;
-- Create proc
drop synonym if exists dbo.test_proc;
drop proc if exists dbo.test_proc;
go
create proc dbo.test_proc
as
set nocount on;
select 'This is the text' procedure_message
go
-- run the proc
exec dbo.test_proc;
-- drop procedure
drop proc if exists dbo.test_proc;
-- Create a synonym for the deleted procedure
create synonym dbo.test_proc
for dbo.test_replacement_proc;
go
-- run the proc (which is now a synonym for the replacement proc)
exec dbo.test_proc;
Output #2
replacement_message
Could not find stored procedure
Output #4
procedure_message
This is the text
output #7
replacement_message
Could not find stored procedure
Use SQL Server Agent to create an alert for error #2812.
You need to create an operator also, and then you can setup a notification for the alert.
See MSSQLTips on how to do it all.

Temporary stored procedure scope

SQL Server supports temporary tables(local and global).
Using dynamic-SQL (EXEC or dbo.sp_executesql) we could create new context and local temporary table is visible only in dynamic-SQL block but not outside.
-- Normal table
EXEC ('CREATE TABLE tab(i INT); INSERT INTO tab(i) VALUES (1)');
SELECT * FROM tab;
-- Global temporary table
EXEC ('CREATE TABLE ##tab(i INT); INSERT INTO ##tab(i) VALUES (2)');
SELECT * FROM ##tab;
-- Local temporary table
EXEC ('CREATE TABLE #tab(i INT); INSERT INTO #tab(i) VALUES (3)');
SELECT * FROM #tab;
-- Invalid object name '#tab'.
LiveDemo
Now let's try the same with stored procedures:
-- Normal procedure
EXEC ('CREATE PROCEDURE my_proc AS SELECT 1 AS col;');
EXEC my_proc;
-- Global temporary procedure
EXEC ('CREATE PROCEDURE ##my_proc AS SELECT 2 AS col;');
EXEC ##my_proc;
-- Local temporary procedure
EXEC ('CREATE PROCEDURE #my_proc AS SELECT 3 AS col;');
EXEC #my_proc;
LiveDemo2
The question is why local temporary procedure behaves differently and it is visible outside EXEC?
The reason it keeps it around outside the exec because SQL Server can reuse the proc's query plan.
The proc disappears as soon as the session where it was defined (via the exec) is closed.
MS is very careful indicate that temp 'tables' (one with one # sign) are local to the context and not visible to calling program.
See:
Any tables you create are visible to the EXEC() context in
https://technet.microsoft.com/en-us/library/aa175921%28v=sql.80%29.aspx

How to execute a stored procedure after it is created?

I'm trying to execute a stored procedure directly after its creation however it is not getting called. It looks like the stored procedure is not yet created during the execution call.
Here is how the script looks like:
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
EXEC sp_Transfer_RegionData
The script runs fine however the needed table is not populated. After replacing the execution part with:
IF OBJECT_ID('sp_Transfer_RegionData') IS NOT NULL
begin
exec [dbo].[sp_Transfer_RegionData]
print 'tada'
end
I could see that the stored procedure does not exist when it has to be executed. Couldn't find a solution for this in the internet...
So how to make the SQL script run sync so that the stored procedure would already exist during the execution part?
You need a GO after you created the SP otherwise you have created a recursive SP that calls itself "indefinitely" which is 32 times in SQL Server.
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
Try this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
GO
EXEC sp_Transfer_RegionData

How to create a stored procedure within another stored procedure in SQL Server 2008

I want to create a stored procedure to be used within a stored procedure something like shown below. Is this possible?
CREATE procedure parentSP
as
--child SP definition.
CREATE procedure childSP1 #inputArg varchar(50)
as
--do something.
Go
--call child sp within parentSP
execute childSP1 '10'
Go
You can use exec:
CREATE procedure parentSP
as
exec('CREATE procedure childSP1 #inputArg varchar(50)
as
--do something.')
--call child sp within parentSP
execute childSP1 '10'
Go