views:

291

answers:

2

I've created an ADO.NET connection manager, and a DataReader source with the following SQL Command:

select 
   'test' as testcol
INTO
   #tmp

select * from #tmp

If I click the refresh button in the DataReader component, I get SqlException "Invalid object name #tmp". The SQL statment itself is clearly valid and executes properly in sql server management studio. I've also tried setting DelayValidation on the connection manager, to no avail.

+2  A: 

is the error on the INSERT or the SELECT?

if you are issuing only one command that contains both the INSERT and SELECT, try putting a semicolon before the SELECT.

EDIT after OP comment
encapsulate all the logic within a stored procedure:

CREATE PROCEDURE YourProcedureName
AS
select 
   'test' as testcol
INTO
   #tmp

select * from #tmp

GO

the have your application run this single SQL command:

exec YourProcedureName

EDIT after next OP comment

OP doesn't say which SQL Server version they are using, if 2005 or up, try a CTE:

;with CTEtemp as
(
select 
   'test' as testcol
)
select * from CTEtemp
KM
Semicolon doesn't appear to help.
Jeremy
True, a stored proc would be so much easier. Unfortunately our server group prevents me from creating stored procedures on that sql server, and if I put a stored proc on another server, the cross server joins would be sub-optimal.
Jeremy
Fortunately, I was able to rework the query to not require temp tables, but I'm still interested in how one would go about doing this in SSIS because this seems like a definite shortfall if you ask me.
Jeremy
A: 

Why couldn't this be replaced with a "SELECT 'test' as testcol"? The SSIS query parser may be having trouble with it because there's a temp table involved and it expects a single statement, not an actual SQL script. Or, if what you're sharing above is only an example for illustration, maybe something like this:

SELECT *
  FROM (SELECT 'test' AS testcol)

Can you elaborate on what you're trying to accomplish here and, if it is, why the temp table is required?

rwmnau
This was just a simplified example. In my real situation I require the temp table, but I used this example for simplicity.
Jeremy