我的存储过程接受一个参数,它是一个日期时间。如果不传参数,我默认给getdate()
函数:
ALTER PROCEDURE [dbo].[myexport]
(
@start datetime = null
)
AS
BEGIN
SET NOCOUNT ON;
set @start = isnull(@start, getdate())
当我运行不带参数的存储过程时,我的结果是正确的。当我将日期参数传递给它时,我收到以下错误:
exec myexport '2013-04-22 00:00:00:00'
Msg 245, Level 16, State 1, Procedure myexport, line 16
Conversion failed when converting the varchar value '(-) 0mms' to data type int.
这是我的@start 日期的上下文:
select
count(distinct pid),
'Inpatient Hosp'
from
form_names fn
inner join form_items fi on fn.id = fi.form_name
inner join form_records fr on fn.id = fr.form_name
left outer join form_answers fa on fi.id = fa.form_item and fa.form_record = fr.id
where
fn.name like '%Transfer of Health Information%'
and dateadd(dd, 0, datediff(dd, 0, fr.date)) = dateadd(dd, 0, datediff(dd, 0, @start))
and fi.text like '%date of return%'
and fa.id is NULL
编辑 -
我的存储过程 id 相当大,有很多子查询,但我想我发现了有问题的查询:
select
count(*),
'PPD +'
from
form_records fr
inner join form_names fn on fn.id = fr.form_Name
inner join form_items fi on fn.id = fi.form_name
inner join form_answers fa on fr.id = fa.form_record
and fa.form_item = fi.id
where
fn.id = 3
and fa.form_item = 30
and fa.text not like '%neg%'
and dateadd(dd, 0, datediff(dd, 0, fa.entered_date)) = dateadd(dd, 0, datediff(dd, 0, @start))
and CAST(fa.text AS INT) >= 10
当我用 手动运行这个查询时getdate()
,我的结果是正确的,但是当我用 运行它时'2013-04-22 00:00:'00
,我得到了错误。
的数据类型fa.entered_date
是datetime
.
任何帮助,将不胜感激。