Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
plz help i want to check store-p in my database if exist store-p with same name the drop store-procedure and Not Exist then create store-p. plz tell me query
Posted
Updated 19-Apr-17 23:58pm

Management studio can generate the script for you. Right-click on your database -> Tasks -> Generate Scripts... In script options you need to set "DROP and CREATE" to be generated and set "Check for object existence" to true. Here is what it looks like:
SQL
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[MyStoredProc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [MyStoredProc]
GO
CREATE PROCEDURE [MyStoredProc]
...
 
Share this answer
 
Comments
Nishant.Chauhan80 23-Dec-14 7:06am    
i want to query c# if- else condition
SQL
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'SPNAME')
    BEGIN
        DROP  Procedure SPNAME
    END
 Go
 Create Procedure SPName
 As
 BEGIN
---   Your statements here
 END
GO
 
Share this answer
 
v2
Comments
Nishant.Chauhan80 23-Dec-14 7:07am    
i want to query c# if- else condition
 
Share this answer
 
Comments
Nishant.Chauhan80 23-Dec-14 7:07am    
i want to query c# if- else condition
Zoltán Zörgő 23-Dec-14 7:21am    
1) You haven't tol this before.
2) Why?
3) You can use following query from code also: SELECT * FROM sys.procedures WHERE Name = 'GetCustomers'. And if the result is not empty, the procedure exists. Assuming yo know how to execute a query from c#...
Nishant.Chauhan80 23-Dec-14 7:32am    
how to use if else condition ..
Zoltán Zörgő 23-Dec-14 7:34am    
Have you read my links? I doubt. All of them had if-else condition as you wanted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900