Hi there,
I'm sure it's something quite simple but can anyone explain why the following doesn't successfully run? The procedures code is:
CREATE PROCEDURE Proc_Test @Name VARCHAR(1000)
AS
BEGIN
DECLARE @Test TABLE ([Col1] VARCHAR(100)
,[Col2] VARCHAR(100))
INSERT INTO @Test([Col1],[Col2])
SELECT 'A','B'
DECLARE @Sql VARCHAR(MAX)
IF @Name = 'Value'
BEGIN
SET @Sql = 'SELECT [Col1], [Col2]
FROM ' + @Test + ''
END
EXEC(@Sql);
END
However it errors "Must declare the scalar variable "@Test"." when I execute:
EXEC Proc_Test 'Value'
I can get it to execute when it's not enclosed in dynamic SQL however I've tried all combination of quotes in the above query and still cannot get it to run dynamically.
TIA
↧