Monday, March 19, 2012

insert stored procedure result into temporary table ?

I'm trying to insert the results of a stored procedure call into a temporary table, which is not working. It does work if I use a non-temporary table. Can anyone tell me if this is supported or what I am doing wrong.

Here is an example:

-- DROP PROCEDURE testProc
CREATE PROCEDURE testProc AS
BEGIN
SELECT '1111' as col1, '2222' as col2
END

-- this call will fail with message Invalid object name '#tmpTable'.
INSERT INTO #tmpTable EXEC testProc

-- DROP TABLE testTable
CREATE TABLE testTable (col1 varchar(5), col2 varchar(5))

-- this call will succeed
INSERT INTO testTable EXEC testProchow about defining your temp table before inserting into it?|||I'd really prefer to not create a hard dependency on the exact columns returned from the procedure. For instance, if I add another column to the resultset of the procedure, then the 'INSERT INTO EXEC' call will fail unless the predefined table for it is also updated.
I'm basically making a passthough procedure which will call another procedure and return the results as XML. So the base procedure may be called directly or via the passthrough.|||It is considered poor programming practice to use "SELECT *", which is essentially what you are doing when you don't pre-define your table layout.

If you add a column to your stored procedure, then your statement SHOULD fail. That ensures that you review your code to catch any other problems that might arise from the schematic change, and that is what the "testing" phase of development is all about.|||I don't think you know enough about what I'm doing to make that statement blindman. I am making a procedure that is just a passthough call to another procedure. The base procedure that is being called is where all the work is done. The passthrough procedure only exists because some clients will be calling the procedure via HTTP and expecting XML results rather than calling the procedure 'directly' via ODBC or JDBC. If I need to make a change to the base procedure, I don't want to have to also make that change to the passthough version. That creates more dependencies and unnecessary maintenance.
Ideally, a better design might be to add a parameter to the base procedure to tell it whether the results should be returned as XML or not... But I need more restrictive control over the HTTP-XML version for security reasons and it would be cleaner to go the passthrough route.|||I don't think you know enough about what I'm doing to make that statement blindman. I am making a procedure that is just a passthough call to another procedure. The base procedure that is being called is where all the work is done. The passthrough procedure only exists because some clients will be calling the procedure via HTTP and expecting XML results rather than calling the procedure 'directly' via ODBC or JDBC.That's exactly what I thought you were doing. Good coding practice calls for enumerating your datasets.

No comments:

Post a Comment