If SQL variable exists

Viewed 12356

I'm writing some code that uses a significant quantity of dynamic SQL and as a result, there are some instances when a third party application may or may not have a SQL variable declared.

Is there a way to test if a variable has been declared. Some psuedo code would be:

IF OBJECT_ID(N'@var' 
) IS NOT NULL
BEGIN
    DECLARE @var AS varchar(max)
END

Also, is there a way to list all of the variables currently declared like a local watch window?

1 Answers
Related