I have a seemingly simple request. I need a script to save all stored procedures in a database according to these criteria.
Scenarios should include dropping if proc exists, as shown below ...
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[myproc]') AND type in (N'P', N'PC'))
the script cannot have sp_executesql, so nothing like that ...
EXEC dbo.sp_executesql @statement = '....'
I need a separate script file for each stored procedure. So it will be [stored procedure name] .sql
I notice that when I try to create sql generate scripts, I can get procs in a separate file using the checkbox for script objects in separate files, and I can also get drop if exists. However, it uses sp_executesql, which they do not want.
So, I spent a little effort on SMO and found similar problems ...
a. Unfortunately, the following scripts are just pop-up statements. By no means am I going to combine it with creation. Therefore, I can get separate files and not sp_executesql, but I still can’t see # 1 above
Scripter scripter = new Scripter(); scripter.Options.ScriptDrops = true;
B. Secondly, the following parameter changes the output to use sp_executesql
scripter.Options.IncludeIfNotExists = true;
C. Finally, I can add the text manually. It has been successfully installed in TextHeader. However, scripter.Script () throws an exception "{" Script failed for StoredProcedure 'dbo.myproc'. "}
storedProcedure.TextHeader = string.Format("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'P', N'PC')) \r\nDROP PROCEDURE {0} \r\nGO\r\n{1}", storedProcedure.Name, storedProcedure.TextHeader); scripter.Options.FileName = Path.Combine(storedProceduresPath, storedProcedure.Name + ".sql"); scripter.Script(new Urn[] { storedProcedure.Urn }); //Exception! - Script failed for StoredProcedure
I can’t imagine that it’s all strange, that I do so, I wonder how people achieve this ??? Sadly, if you need to create separate files using sql tasks - generate scripts and then the application to clear the unwanted "EXEC dbo.sp_executesql @statement = N"