I want to generate the script using Tasks | Generate Scripts
with IF Exist Drop Stored Procedure
.
But I don't want to include IF NOT EXISTS - Create Stored Procedure
while generating Script then what should I do?
Current
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPNAME]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPNAME]
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPNAME]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[SPNAME] AS'
END
GO
Required
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPNAME]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPNAME]
GO
CREATE PROCEDURE [dbo].[SPNAME] AS
Can anyone please help me!
If the table does not exist and you do not include the IF EXISTS clause, the statement will return an error. Before dropping a table, you must first remove any stored procedures that reference the table.
We use the SQL DROP Table command to drop a table from the database. It completely removes the table structure and associated indexes, statistics, permissions, triggers and constraints. You might have SQL Views and Stored procedures referencing to the SQL table.
Using the OBJECT_ID and the IF ELSE statement to check whether a table exists or not. Alternative 2 : Using the INFORMATION_SCHEMA. TABLES and SQL EXISTS Operator to check whether a table exists or not.
There is no direct option available to do this thing. Follow below step.
Generate DROP scripts for all objects.
Generate CREATE scripts for all objects.
2nd step will append contents of drops scripts which is generated in 1st step.
If you want to merge all files in one file then use bellow command
for %f in (*.sql) do type "%f" >> c:\Test\output.sql
You can enable the "Check for object existence" option to true and generate the drop create script. It should work for 2017 or Azure SQL DB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With