I need to store the result set of a stored procedure in a temporary table (using SQL Server 2000). From what I've read, this (poorly constructed example) should work:
create table #tempTable (TempId int primary key, Column1 varchar(100),
Column2 varchar(100), DateCreated datetime)
insert into #tempTable (TempId, Column1, Column2, DateCreated)
exec sproc_Select_Stuff_By_DateCreated @Date1 = '1/1/2009', @Date2 = '1/2/2009'
But I get: "Insert Error: Column name or number of supplied values does not match table definition."
Examining the procedure (which I cannot edit) reveals this:
CREATE PROCEDURE sproc_Select_Stuff_By_DateCreated
@Date1 datetime,
@Date2 datetime
AS
BEGIN
SELECT TempId, Column1, Column2, DateCreated
FROM ReallyHugeMessOfJoinsAndCalculatedColumns
WHERE DateCreated between @Date1 and @Date2
SELECT @Date1 as Date1, @Date2 as Date2
END
So it's actually echoing back the parameters passed into it as a second result set. (I have no idea why; I'd figure that anything calling the procedure would know what data it was passing in.)
My testing leads me to think that the second result set is what's causing the insert failure - like SQL is trying to union the result sets together and failing.
I only need the first result set saved to my temporary table. How can I do that?
Edit
Thanks for pointing out CLR stored proecedures, but that feature was introduced in SQL 2005 - it won't work for 2000. (But I wasn't previously aware of them, and they look like they'll be useful when we upgrade.)
As the only other answers look to be "you can't" - I guess it's back to the drawing board for me.