對於給定的,STAFFID計算countexel和scoreexel你可以重新寫你的存儲過程爲:
create table TbStudentSurvey (STAFF_ID int,FEEDBACK varchar(20));
insert into TbStudentSurvey values (1,'excellent'),(1,'excellent'),(2,'excellent');
create table TbStaffScoreMaster (Score int,[Status] varchar(20));
insert into TbStaffScoreMaster values(100,'Excellent');
Go
create procedure [dbo].[staffscorecard]
@STAFF_ID INT = NULL,
@countexel int output,-- Explicitly declare output variables to fetch these values
@scoreexel int output
as
Begin
select
@countexel = count(STAFF_ID)
from
TbStudentSurvey
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(@STAFF_ID, STAFF_ID)
select
@scoreexel = Score
from
TbStaffScoreMaster
where
Status = 'Excellent'
End
GO
,然後代替使用臨時表使用表變量,因爲當您使用臨時表時,表必須與存儲過程的確切列布局相匹配。
--CREATE TABLE #temp (countexel int, scoreexel int)
--GO
--Create a table variable:
declare @temp table (countexel int, scoreexel int)
declare @countexel int, @scoreexel int,@STAFF_ID int;
--set value of staff Id for which you want to get countexel and scoreexel.
set @STAFF_ID = 1;
EXEC [dbo].[staffscorecard] @STAFF_ID ,@countexel output,@scoreexel output
INSERT @temp values (@countexel ,@scoreexel);
SELECT *
FROM @temp
GO
方法2: 你也可以寫爲:
alter procedure [dbo].[staffscorecard]
@STAFF_ID INT = NULL
as
Begin
select
count(STAFF_ID) as countexel , Score as scoreexel
from
TbStudentSurvey TSS
inner join TbStaffScoreMaster TSM on TSM.Status = TSS.FEEDBACK
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(@STAFF_ID, STAFF_ID)
group by STAFF_ID,Score
End
GO
declare @temp table (countexel int, scoreexel int)
declare @STAFF_ID int;
set @STAFF_ID = 1;--set value of staff Id for which you want to get countexel and scoreexel.
INSERT @temp EXEC [dbo].[staffscorecard] @STAFF_ID
SELECT *
FROM @temp
GO
希望這有助於!
你能解釋清楚你的問題嗎 –
我已經創建了一個存儲過程,現在我想插入這個過程到一個新表。我該怎麼做? –
如果您可以使用表值函數,則可以在函數中執行此操作。而不是臨時表,使用表變量類型。 http://technet.microsoft.com/en-us/library/ms191165%28v=sql.105%29.aspx這看起來像'Select * from dbo.myFunctionName(@StaffId)',這將返回一個價值表。 http://odetocode.com/articles/365.aspx – ps2goat