Comments
2 comments
-
Hi @SkipSailors!
The recommendation is to use sp_executesql instead of exec https://documentation.red-gate.com/codeanalysis/best-practice-rules/bp013
However can I just confirm if that wouldn't work in this case? (Apologies as I'm not a SQL developer myself!)
-
hi @SkipSailors - if you have SQL Server 2016 SP1 or onwards you can use the CREATE OR ALTER PROCEDURE statement.
example below:CREATE OR ALTER PROCEDURE dbo.sp_testASSELECT 1;GOEXEC dbo.sp_test;goCREATE OR ALTER PROCEDURE dbo.sp_testASSELECT 2goEXEC dbo.sp_test
Add comment
Please sign in to leave a comment.
So, my pattern is that I have an if statement that checks the sys.objects for the procedure, and it it isn't there, creates a minimal one that the ALTER can use.
<div> if not exists (select * from sys.objects where object_id = OBJECT_ID(N'whatever') and type in (N'P', N'PC')) </div><div> exec ('CREATE PROC whatever AS SELECT 1')</div><div> go</div> alter procedure whatever ...<br>SQL Prompt explains to me (BP013) that I should avoid exec('...'). What other ways do I have to create a generic minimal procedure if there is not one present?