Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I instert into temptable with returned values from multiple query statement in a same row?

I tried below code, but get result of
----------------------
OPC PFC BCFC
10 NULL NULL
11 NULL NULL
12 NULL NULL
NULL NULL 0
NULL NULL 0
NULL NULL 0
----------------------

What I have tried:

ALTER procedure [dbo].[proc_test]
(@currentdate datetime)
as 
begin
create table #tempRearRight
(OPC INT, PFC INT, BCFC INT)

INSERT INTO #tempRearRight (opc)
select Count ( * ) as OPC  from D_Aktion
where Zeitstempel between (dateadd(day, -1,(@currentdate)) + '' + '23:30:00.000') and ( (@currentdate) + '' + '00:30:00.000')
and Formular = 'ASSEMBLY_ORDER_2PARTS' and Druckstatus = 1
and Drucker = 'BO_RR'

INSERT INTO #tempRearRight (opc)
select Count ( * ) as OPC from D_Aktion
where Zeitstempel between  ((@currentdate) + '' + '00:30:00.000') and ((@currentdate) + '' + '01:30:00.000')
and Formular = 'ASSEMBLY_ORDER_2PARTS' and Druckstatus = 1
and Drucker = 'BO_RR'

INSERT INTO #tempRearRight (opc)
select Count ( * ) as OPC from D_Aktion
where Zeitstempel between  ((@currentdate) + '' + '01:30:00.000') and ((@currentdate) + '' + '02:30:00.000')
and Formular = 'ASSEMBLY_ORDER_2PARTS' and Druckstatus = 1
and Drucker = 'BO_RR'

INSERT INTO #tempRearRight (BCFC)
Select count ( * ) as BCFC from qspruefauftrag_kopf
where Statuszeitstempel between (dateadd(day, -1,(@currentdate)) + '' + '23:30:00.000') and ( (@currentdate) + '' + '00:30:00.000')
and (status = 2  
	OR status = 3) --Status = 3 means 'OK', Status = 2 means 'NOK'
and QSPruefprofil = 'TSV HR'

INSERT INTO #tempRearRight (BCFC)
Select count ( * ) as BCFC from qspruefauftrag_kopf
where Statuszeitstempel between ((@currentdate) + '' + '00:30:00.000') and ( (@currentdate) + '' + '01:30:00.000')
and (status = 2  
	OR status = 3) --Status = 3 means 'OK', Status = 2 means 'NOK'
and QSPruefprofil = 'TSV HR'

INSERT INTO #tempRearRight (BCFC)
Select count ( * ) as BCFC from qspruefauftrag_kopf
where Statuszeitstempel between ((@currentdate) + '' + '01:30:00.000') and ( (@currentdate) + '' + '02:30:00.000')
and (status = 2  
	OR status = 3) --Status = 3 means 'OK', Status = 2 means 'NOK'
and QSPruefprofil = 'TSV HR'
	select * from #tempRearRight
end

exec proc_test '2022-03-16'
Posted
Updated 3-Aug-22 19:55pm

1 solution

Each time you execute an INSERT statement, it creates a new row: if you supply just one column value with the INSERT then all other columns get defaulted - so because you issue six INSERT commands, you get six rows.
The simplest way round this is to pregenerate all the values into SQL variables in your SP, then issue three INSERT commands:
SQL
CREATE TABLE #tempRearRight
(OPC INT, PFC INT, BCFC INT)

DECLARE @OPC int = (SELECT COUNT(*) FROM ...);
DECLARE @BCFC int = (SELECT COUNT(*) FROM ...);

INSERT INTO #tempRearRight (opc, bcfc)
VALUES (@OPC, @BCFC);
... change @OPC and @BCFC ...
INSERT INTO #tempRearRight (opc, bcfc)
VALUES (@OPC, @BCFC);
... change @OPC and @BCFC ...
INSERT INTO #tempRearRight (opc, bcfc)
VALUES (@OPC, @BCFC);

SELECT * FROM #tempRearRight
DROP TABLE #tempRearRight
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900