2016-10-24 48 views
0
CREATE PROCEDURE [SSIS].[usp_LifeCount] 
AS 
BEGIN 

-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 
-- Clear the table to prevent duplicate data. 
TRUNCATE TABLE SSIS.LivingMales 
-- Insert statements for procedure here 
INSERT INTO SSIS.LivingMales 
(Males) 
SELECT COUNT(SEX) 
FROM SSIS.Marvels 
WHERE (ALIVE = 'Living Character' AND SEX = 'Male Character'); 


-- Clear the table to prevent duplicate data. 
TRUNCATE TABLE SSIS.LivingFemales 
-- Insert statements for procedure here 
INSERT INTO SSIS.LivingFemales 
(Females) 
SELECT COUNT(SEX) 
FROM SSIS.Marvels 
WHERE (ALIVE = 'Living Character' AND SEX = 'Female Character'); 

-- Clear the table to prevent duplicate data. 
TRUNCATE TABLE SSIS.DeadMales 
-- Insert statements for procedure here 
INSERT INTO SSIS.DeadMales 
(Males) 
SELECT COUNT(SEX) 
FROM SSIS.Marvels 
WHERE (ALIVE = 'Deceased Character' AND SEX = 'Male Character'); 

-- Clear the table to prevent duplicate data. 
TRUNCATE TABLE SSIS.DeadFemales 
-- Insert statements for procedure here 
INSERT INTO SSIS.DeadFemales 
(Females) 
SELECT COUNT(SEX) 
FROM SSIS.Marvels 
WHERE (ALIVE = 'Deceased Character' AND SEX = 'Female Character'); 
END 

我試圖得到有多少男性/女性角色活着以及死亡的總數。我知道這是我的where子句。當我把它們拿出來時,它會返回所有人而不是個人。 Alive是SSIS中的一個列名。尺寸存儲過程不斷爲每個表返回零。何處條款?

+1

你已經確定問題是你的where子句。所以你需要調整你的where子句,以便它是正確的。我們不可能告訴你它應該是什麼,因爲我們不知道你的數據。 –

+1

在你的SP之外,你是否得到了'SELECT COUNT(SEX)FROM SSIS.Marvels的結果WHERE(ALIVE ='Living Character'AND SEX ='Male Character');'? – JNevill

+2

這是一個非常可怕的方式來獲取此信息。截斷一個只有總計的表,並且在每個「ALIVE」狀態和「SEX」有一個表的情況下,這應該是對第一個表的簡單查詢,帶有「GROUP BY」 – Lamak

回答

0

運行此命令可以查看您的數據實際上的樣子。

SELECT DISTINCT ALIVE, SEX FROM SSIS.Marvels 

然後你就可以解決您的where子句來匹配您的數據。我們無法爲您修復它們,因爲我們不知道您的數據是什麼樣的。