I’m trying to write a stored procedure for Crystal Reports by combining multiple queries into a single resultset (Crystal doesn’t support multiple results in one report).
The result set I’m trying to get combines columns from both tables.
In the SP, I declare @temptable and the columns (because the two tables i’m querying have different columns).
DECLARE @TEMPNEWBILLING TABLE
(
ACCOUNT DECIMAL null,
CLIENT NVARCHAR null,
TIMESTAMP INT null,
BILLING DECIMAL null,
CALLKIND INT null,
HITK1 DECIMAL null,
HITK2 DECIMAL null,
HIDISC DECIMAL null,
HITALK DECIMAL null,
HIPTCH DECIMAL null,
HICONF DECIMAL null,
HIHOLD DECIMAL null,
PTCH DECIMAL null,
SUPERTIME DECIMAL null
)
I then SELECT from both tables INTO the temp table:
SELECT Account, Client, Timestamp, Billing, CallKind, HiTk1, HiTk2, HiDisc, HiTalk, HiPtch, HiConf, HiHold, Ptch
INTO TEMPNEWBILLING
FROM
mCallEnd
WHERE billing = cast(@BILLINGNUMBER as decimal)
AND Timestamp > @STARTITIME
AND Timestamp < @ENDITIME
AND CallKind in (0,1,2,3,4,16)
SELECT
Billing, SuperTime
INTO TEMPNEWBILLING
FROM
mClientMaint
WHERE billing = cast(@BILLINGNUMBER as decimal)
AND Timestamp > @STARTITIME
AND Timestamp < @ENDITIME
And finally, I just get all data from the temp table.
SELECT * FROM @TEMPNEWBILLING
Unfortunately, something is going wrong, as when I run the SP, I get an error that
There is already an object named ‘TEMPNEWBILLING’ in the database.
I’ve checked it out and it seems that the first query is running, but the error gets thrown at the second Select Into. I must be doing this wrong, as I get the same error if I use # tables or @ tables (i.e. delcare the table vs. create the table).
Is the prospect of filling a temp table with the results of two queries simply not possible? Am I using the wrong tool for the job?
In your code, you are not using the variable table youe defined, instead you are trying to put the results into the same physical table. Try this instead: