Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Checking If A Proc Exists
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01245040
Message ID:
01245069
Views:
21
Try building the SQL command as a string then use sp_executesql to execute the string .
DECLARE @SProcName varchar(100),
        @Command   nvarchar(250)

IF EXISTS (SELECT *
               FROM dbo.sysobjects
	       WHERE id = object_id(@SProcName) AND
	       OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
       SELECT @Command = N'DROP PROCEDURE ' + @SProcName 

       EXEC sp_executesql @stmt = @command
END
Kurt

>Getting an error:
>
>
>CREATE PROCEDURE sp_ProcExists
>	@SProcName nvarchar(100
>AS
>
>  IF EXISTS (SELECT *
>               FROM dbo.sysobjects
>	       WHERE id = object_id(@SProcName) AND
>	       OBJECTPROPERTY(id, N'IsProcedure') = 1)
>
>     DROP PROCEDURE @SProcName                 <=========== Errors here
>
>
>
>
>
>
>
>>Yes
>>
>>... @sproc nvarchar(100)
>>...
>>WHERE id = object_id(@sproc)
>>...
>>
>>>
>>>Can I make that a call to a SPROC, passing the proc name I want to check?
>>>
>>>
>>>
>>>
>>>>
>>>>IF exists (SELECT * from dbo.sysobjects
>>>>	WHERE id = object_id(N'dbo.sp_MyProc')
>>>>		and OBJECTPROPERTY(id, N'IsProcedure') = 1)
>>>>
>>>>>I can't seem to get this syntax right:
>>>>>
>>>>>
>>>>>USE SC
>>>>>GO
>>>>>
>>>>>IF EXISTS(SELECT * FROM SysObjects WHERE Id = "sp_MyProc")
>>>>>	DROP PROCEDURE sp_MyProc
>>>>>
>>>>>GO
>>>>>
>>>>>CREATE PROC sp_MyProc....
>>>>>
>>>>>
>>>>>
>>>>>What am I doing wrong here?
Previous
Reply
Map
View

Click here to load this message in the networking platform