Question

I'm trying to figure out a way that I can pass a filename into a stored procedure that can import a file. However, after setting up the SQL dynamically, the object can't be found.

DECLARE @fileName varchar(200), 
    @sql varchar(max);

SET @fileName = 'C:\file.csv';

SET @sql = 'SELECT *
    INTO #import
    FROM OPENROWSET(BULK ''' + @fileName + ''',
    FORMATFILE=''C:\format.xml'',
    FIRSTROW = 2) AS a';

EXEC(@sql);

SELECT * INTO #stage
FROM #import;

The result

(27823 row(s) affected)
Msg 208, Level 16, State 0, Line 29
Invalid object name '#tmtImport'.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top