有一张员工工作表work。有工作的起止时间和工作日统计列starttime,endtime,datenum。现在要查询一段时间内员工的工作日。我写了个存储过程来计算查询时间内员工的工作日:datecut。
输入参数是starttime(工作开始时间),endtime(工作结束时间),qstarttime(查询开始时间),qendtime(查询结束时间)。输出参数是datenum(查询时间段内员工的工作日)。
可是我新建存储过程调用
create [dbo].[query]
@qstarttime datetime,
@qendtime datetime
as
declare dn int
select
exec datecut starttime,endtime,@qstarttime,@qendtime,dn as 天数
from work
不行,为什么?
是不是要建临时表,再计算工作日?
输入参数是starttime(工作开始时间),endtime(工作结束时间),qstarttime(查询开始时间),qendtime(查询结束时间)。输出参数是datenum(查询时间段内员工的工作日)。
可是我新建存储过程调用
create [dbo].[query]
@qstarttime datetime,
@qendtime datetime
as
declare dn int
select
exec datecut starttime,endtime,@qstarttime,@qendtime,dn as 天数
from work
不行,为什么?
是不是要建临时表,再计算工作日?
‘exec datecut starttime,endtime,@qstarttime,@qendtime,dn as 天数’ 这行我用
‘starttime - endtime as 天数’能执行!
exec datecut starttime,endtime,@qstarttime,@qendtime,dn as 天数
from work
--哪有这个用法?
你datecut过程丢出来
ALTER proc [dbo].[datecut]
@starttime datetime,
@endtime datetime,
@qstarttime datetime,
@qendtime datetime,
@datenum real output
as
declare @st datetime,@et datetime
set @datenum = 0
if datediff(day,@starttime,@qstarttime) > 0
set @st = @qstarttime
else
set @st = @starttime
if datediff(day,@endtime,@qendtime) >= 0
set @et = @endtime
else
set @et = @qendtime
exec datenum @st,@et,@datenum = @datenum outputdatenum就是计算两个日期间的工作日天数。去掉holiday表中的节假日,workday表中上班的周末。ALTER proc [dbo].[datenum]
@starttime datetime,
@endtime datetime,
@datenum real output
as
declare @dt datetime
set @dt = @starttime
set @datenum = 0
while (datediff(day,@dt,@endtime) >= 0)
begin
if(datepart(dw,@dt) in (6,2,3,4,5))
if (select count(*) from holiday where (datediff(day,@dt,holiday) = 0)) = 0
set @datenum = @datenum + 1
else
if (select count(*) from workday where (datediff(day,@dt,workday) = 0)) > 0
set @datenum = @datenum + 1
set @dt = dateadd(day,1,@dt)
continue
end
select exec procedure from...
来获得数据.
必须建一个临时表或表变量,用这样的方法从存储过程获得记录集:
insert into @t
exec procedure .....
GO
IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL
DROP PROCEDURE Production.usp_GetList;
GO
CREATE PROCEDURE Production.usp_GetList @product varchar(40)
, @maxprice money
, @compareprice money OUTPUT
, @listprice money OUT
AS
SELECT p.name AS Product, p.ListPrice AS 'List Price'
FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice;
-- Populate the output variable @listprice.
SET @listprice = (SELECT MAX(p.ListPrice)
FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice);
-- Populate the output variable @compareprice.
SET @compareprice = @maxprice;
GO
另一个存储过程调用的时候:Create Proc Test
as
DECLARE @compareprice money, @cost money
EXECUTE Production.usp_GetList '%Bikes%', 700,
@compareprice OUT,
@cost OUTPUT
IF @cost <= @compareprice
BEGIN
PRINT 'These products can be purchased for less than
$'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
PRINT 'The prices for all products in this category exceed
$'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二种方法:创建一个临时表create proc GetUserName
as
begin
select 'UserName'
endCreate table #tempTable (userName nvarchar(50))
insert into #tempTable(userName)
exec GetUserNameselect #tempTable--用完之后要把临时表清空
drop table #tempTable--需要注意的是,这种方法不能嵌套。例如: procedure a
begin
...
insert #table exec b
end
procedure b
begin
...
insert #table exec c
select * from #table
end
procedure c
begin
...
select * from sometable
end --这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
第三种方法:声明一个变量,用exec(@sql)执行:1);EXEC 执行SQL语句declare @rsql varchar(250)
declare @csql varchar(300)
declare @rc nvarchar(500)
declare @cstucount int
declare @ccount int
set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
--exec(@rsql)
set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
set @rc=@csql+@rsql
exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法
--select @csql+@rsql
--select @cstucount本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx