Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine 2 select counts into 1 insert

Status
Not open for further replies.

excalibur78

IS-IT--Management
Jan 3, 2001
66
US
I have the following I'm trying to get 7 distinct counts from 7 different tables into this temporary table in a single record.

CREATE PROCEDURE [dbo].[Test] AS
CREATE TABLE #Temp
(
UPS int,
SW int,
PCLAN1 int,
PCLAN2 int,
POS int,
SCO1 int,
SCO2 int
)
insert into [#Temp] ([UPS]) select distinct count([store #]) from [ups cutovers] where [finished] = 'True'
insert into [#Temp] ([SW]) select distinct count([store #]) from [store wiring] where [finished] = 'True'
select * from [#temp]
GO


Thanks,


David
 
Basic idea is:

Code:
INSERT #temp (ups, sw, pclan1, pclan2, pos, sco1, sco2)
VALUES (
  (SELECT COUNT(DISTINCT [store #]) FROM [ups cutovers] WHERE finished = 'true'),
  (SELECT COUNT(DISTINCT [store #]) FROM [store wiring] WHERE finished = 'true'),
  ...
)
--James
 
I get "Error 1046: Subqueries are not allowed int his context. Only scalar expressions are allowed."


David
 
Oops, sorry. You need to use a SELECT rather than the VALUES clause:

Code:
INSERT #temp (ups, sw, pclan1, pclan2, pos, sco1, sco2)
SELECT
  (SELECT COUNT(DISTINCT [store #]) FROM [ups cutovers] WHERE finished = 'true'),
  (SELECT COUNT(DISTINCT [store #]) FROM [store wiring] WHERE finished = 'true'),
  ...
--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top