【发布时间】:2016-03-15 11:35:46
【问题描述】:
我创建了一个带有 3 个参数的存储过程。见下文
ALTER PROCEDURE [dbo].[Leave_Allocation_Mar_2016]
@Type nvarchar(10),
@Month int,
@Year int
AS
BEGIN
declare @emp_card_no numeric(9)
declare @emp_name varchar(20)
declare @dt_of_join datetime
declare @Total_days numeric(5,2)
declare @Days_worked numeric(5,2)
declare @Final_PaidDayLop numeric(5,2)
declare @TotalRecord int
declare @actualMonth int
declare @actualYear int
declare @actuallastdate varchar(20)
IF(@Type = 'C')
BEGIN
Print 'Yes I am in the Current process';
DECLARE daily_Allocate CURSOR FOR
Select distinct c.emp_card_no, c.emp_name, c.Dt_Of_Join from emp_mst c
join emp_mon_day d
on c.emp_card_no=d.emp_mkey
WHERE Dt_Of_Join = CAST(FLOOR(CAST( DATEADD(month, -6, GETDATE()) AS FLOAT ))AS DATETIME)
OPEN daily_Allocate
FETCH NEXT FROM daily_Allocate INTO
@emp_card_no, @emp_name, @Dt_Of_Join
WHILE @@FETCH_STATUS = 0
BEGIN
select @Total_days = Sum(total_day),@Days_worked = Sum(days_worked)
from emp_mon_day a
where a.emp_mkey = 2519
group by emp_mkey
PRINT 'Employee Card no = ' + cast(@emp_card_no as char)
PRINT 'Total days = ' + cast(@Total_days as char)
PRINT 'Days Worked = ' + cast(@Days_worked as char)
set @Final_PaidDayLop = 0;
set @TotalRecord = 0;
Select @Final_PaidDayLop =
isnull(sum(days),0)
from P_Emp_Del_App_Hdr c join P_Emp_Del_App_trl d on c.mkey=d.mkey
where c.delete_flag='N' and app_flag='Y' and c.year = @actualYear
and c.emp_mkey = @emp_card_no
Select @TotalRecord = ((1.75 * 6) / @Total_days) * (@Days_worked + @Final_PaidDayLop)
from emp_mon_day a where a.emp_mkey = @emp_card_no group by a.emp_mkey
PRINT 'Final Paid LOP ' + cast(coalesce(@Final_PaidDayLop,0) as char)
PRINT 'Total Record ' + cast(coalesce(@TotalRecord,0) as char)
FETCH NEXT FROM daily_Allocate INTO
@emp_card_no, @emp_name, @Dt_Of_Join
END
CLOSE daily_Allocate
DEALLOCATE daily_Allocate
END END
这给了我如下输出
现在我想要的是,我想将上面的数据插入到临时表中。
我尝试如下
CREATE TABLE #tmp1111
(
Sr_no int identity(1,1),
Current_Status nvarchar(255),
Emp_card_no int,
Total_days int,
days_worked int,
final_lop_paid int,
total_record int
)
首先我创建了一个临时表并尝试插入如下数据
INSERT INTO #tmp1111 exec Leave_Allocation_Mar_2016 'C', '2', '2016'
它执行成功,但是当我运行 select 语句时,临时表中没有记录。
我正在使用 SQL server 2005
【问题讨论】:
-
存储过程没有直接输出,只有变量的打印。您需要返回正在打印的这些项目,可能作为 SELECT @VarName。但是不清楚您在寻找什么,因为光标内可能有多个这些?
-
@Paddy:我只想将记录插入到临时表中,这是通过在存储过程中打印得到的。如果除了打印之外我将值插入到表中然后让我知道..希望你现在清楚
-
为什么要从外部执行存储过程?如果只需要这些参数,则传递到存储过程
标签: sql-server stored-procedures sql-server-2005