开发者

sybase 'drop procedure'. can I get it to take a parameter instead of a naked proc name?

开发者 https://www.devze.com 2022-12-28 19:00 出处:网络
Happy Monday o/ I\'m looking to do the following within a stored proc select @parameter=\"fooproc\" drop procedure @parameter

Happy Monday o/

I'm looking to do the following within a stored proc

  select @parameter="fooproc"
  drop procedure @parameter

instead of

  drop procedure fooproc

But it's choking with a syntax error. Is there some way I can bend this to my wil开发者_如何学Pythonl? I would've expected to be able to do this.

I've tried a few permutations of type declaration of @parameter to no avail.

Thoughts?


Only with dynamic SQL:

DECLARE @sql AS VARCHAR(2000)

SELECT @sql = STR_REPLACE('drop procedure {@parameter}', '{@parameter}', @parameter)

EXEC ( @sql )

I like to validate object names against database metadata before doing things like this. This protects against errors up front and even injection in the later dynamic section.

i.e. something like:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = @parameter)
BEGIN
    DECLARE @sql AS VARCHAR(2000)
    SELECT @sql = STR_REPLACE('drop procedure {@parameter}', '{@parameter}', @parameter)
    EXEC ( @sql )
END


EXECUTE IMMEDIATE 'drop procedure ' || @parameter;
0

精彩评论

暂无评论...
验证码 换一张
取 消