我有一个存储过程,当我运行SQL硬编码参数值时,它运行良好并返回预期的记录。
但是当我以
身份运行时exec usp_ChangeFormReport
@CFType = -1,
@RG = 11,
@FromDate = '01/01/2017',
@ToDate = '11/26/2018',
@RC = 'Receive',
@CreatedByUser = -1,
@AssignedToUser = '-1',
@CFStatus = 'All',
@NewToPMS = 'A',
@execFromDate = NULL,
@execToDate = NULL,
@notifyMedPoint = 'All',
@requireTraining = 'All'
失败并显示错误:
消息245,级别16,状态1,过程usp_ChangeFormReport,第117行[批处理开始第0行]
将varchar值“ No”转换为数据类型int时,转换失败。
这是具有相同参数硬编码的存储过程。我不知所措,更不用说解释为什么它在某种情况下会失败了,但是可以作为具有完全相同参数的纯SQL脚本运行得很好。在尝试通过varchar
作为int
的地方,我看不到任何地方,并且返回的记录均未显示任何意外内容。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ALTER PROCEDURE [dbo].[usp_ChangeFormReport]
-- (
Declare @CFType INT = -1
Declare @RG INT = 11
Declare @FromDate Varchar (30) = '01/01/2017'
Declare @ToDate Varchar (30) = '11/26/2018'
Declare @RC Varchar (10) = 'Receive'
Declare @CreatedByUser INT = -1
Declare @AssignedToUser Varchar (20) = -1
Declare @CFStatus Varchar (20) = 'All'
Declare @NewToPMS Char(1) = 'A'
Declare @execFromDate Varchar (30) = NULL
Declare @execToDate Varchar (30) = NULL
Declare @notifyMedPoint Varchar (3) = 'ALL'
Declare @requireTraining Varchar (3) = 'ALL'
--)
--AS
-- validate date parameters
If (isdate (@execFromDate) = 0)
set @execFromDate = NULL
If (isdate (@execToDate) = 0)
set @execToDate = NULL
IF isdate ( @FromDate ) = 0
OR isdate ( @ToDate ) = 0
OR cast ( @FromDate AS Datetime ) > cast ( @ToDate AS Datetime )
OR cast ( @execFromDate AS Datetime ) > cast ( @execToDate AS Datetime )
BEGIN
SELECT NULL AS changeformid,
NULL AS isRush,
NULL AS providerNotificationDate,
NULL AS executedDate,
NULL AS notifyMedPoint,
NULL AS doesProviderRequireTraining,
NULL AS displaycontractid,
NULL AS contractentity,
NULL AS changeformtype,
NULL AS changeformstatus,
'Date Parameter Error' AS networklocname,
NULL AS routinggroup,
NULL AS receivedfromrg,
NULL AS resolutiondate,
NULL AS contractID,
NULL AS taxID,
NULL AS tat,
NULL AS isBrandNewProv,
NULL AS effectiveDate,
NULL AS agreementType,
NULL AS createdByUserName
RETURN
END
--create temp table to capture change forms that are completed per department
IF object_id ( 'tempdb..#CFcompletedlist', 'U' ) IS NOT NULL
DROP TABLE #cfcompletedlist
CREATE TABLE #cfcompletedlist
(
changeformid Int,
isRush char(1),
providerNotificationDate varchar(10),
executedDate varchar(10),
notifyMedPoint char(1),
doesProviderRequireTraining char(1),
displaycontractid Varchar (10),
contractentity Varchar (100),
changeformtype Varchar (100),
changeformstatus Varchar (20),
networklocname Varchar (1000),
--resolutionname varchar(100),
routinggroup Varchar (100),
--routinggroupuser varchar(100),
receivedfromrg Varchar (100),
resolutiondate Varchar (10),
contractID int,
taxID varchar(20),
createdByUserName varchar(100)
)
IF object_id ( 'tempdb..#CFnewassignment', 'U' ) IS NOT NULL
DROP TABLE #cfnewassignment
CREATE TABLE #cfnewassignment
(
changeformid Int,
isRush char(1),
providerNotificationDate varchar(10),
executedDate varchar(10),
notifyMedPoint char(1),
doesProviderRequireTraining char(1),
displaycontractid Varchar (10),
contractentity Varchar (100),
changeformtype Varchar (100),
changeformstatus Varchar (20),
networklocname Varchar (1000),
--resolutionname varchar(100),
routinggroup Varchar (100),
--routinggroupuser varchar(100),
receivedfromrg Varchar (100),
resolutiondate Varchar (10),
contractID int,
taxID varchar(20),
createdByUserName varchar(100),
tat Int,
isBrandNewProv char(1),
-- SUK/Ticket#63828/2010-12-09: Added parameters below
effectiveDate DATETIME,
agreementType Varchar(100)
-- end
)
IF @RC = 'Receive'
BEGIN
INSERT INTO #cfcompletedlist
SELECT DISTINCT
cf.changeformid,
IsNull(cf.isRush, 'N') AS isRush,
CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
contractentity,
changeformtypename,
cf.changeformstatus,
dbo.fungetcontractnetworklocations ( cf.contractid ) AS networklocname,
--ResolutionName,
rg.routinggroupname AS routinggroup,
--tu.firstname+', '+tu.lastname AS RoutingGroupUser,
rg2.routinggroupname AS receivedfromrg,
CONVERT(varchar(10), cfr.resolutiontime, 101) AS resolutiondate,
cf.contractID,
co.taxID,
tu.firstname + ' ' + tu.lastname AS createdByUserName
FROM tblchangeform cf
LEFT JOIN tblchangeformresolution cfr
ON cf.changeformid = cfr.changeformid
And cfr.resolutiontime = ( Select max(resolutiontime)
From tblchangeformresolution
Where changeformid = cfr.changeformid
and resolutionValue = cfr.resolutionValue)
LEFT JOIN tblchangeformtype cft
ON cft.changeformtypeid = cf.changeformtypeid
LEFT JOIN tblroutinggroup rg
ON rg.routinggroupid = cfr.resolutionvalue
LEFT JOIN tblroutinggroup rg2
ON rg2.routinggroupid = cfr.resolvedbygroupid
LEFT JOIN tblresolution re
ON re.resolutionid = cfr.resolutionid
LEFT JOIN tblcontracts co
ON co.contractid = cf.contractid
LEFT JOIN tblUsers tu ON tu.userid=cf.createdByUserID
WHERE
(
cf.changeformstatus = @CFStatus
OR @CFStatus = 'All'
)
AND
(
IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
OR @notifyMedPoint = 'All'
)
AND
(
(
@execFromDate IS NOT NULL AND
@execToDate IS NOT NULL AND
cf.executedDate BETWEEN cast ( @execFromDate AS Datetime )
AND cast ( @execToDate AS Datetime )
)
OR
(
@execFromDate IS NULL OR @execToDate IS NULL
)
)
AND
(
IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
OR @requireTraining = 'All'
)
AND cfr.resolutionid IN ( 18, 25, 27 )
AND
(
cft.changeformtypeid = @CFType
OR @CFType = - 1
)
AND cfr.resolutionvalue = @RG
AND cfr.resolutiontime BETWEEN
cast ( @FromDate AS Datetime )
AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
GROUP BY
cf.changeformid,
cf.changeformstatus,
cf.isRush,
cf.providerNotificationDate,
cf.executedDate,
cf.notifyMedPoint,
cf.doesProviderRequireTraining,
co.contractentity,
cft.changeformtypename,
cf.contractid,
co.taxID,
rg.routinggroupname,
rg2.routinggroupname,
tu.firstname,
tu.lastname,
cfr.resolutiontime
ORDER BY cf.changeformid, cf.contractid
INSERT INTO #cfnewassignment
SELECT a.*, tat = '', b.isBrandNewProvider,
-- SUK/Ticket#63828/2010-12-09: Added fields below
co.EffectiveDate,
at.Name as agreementType
FROM #cfcompletedlist a
LEFT JOIN tblchangeform b
ON a.changeformid = b.changeformid
-- SUK/Ticket#63828/2010-12-09: Added join below
LEFT JOIN tblcontracts co
ON co.contractid = b.contractid
LEFT JOIN dbo.tblAgreementTypes at
ON co.AgreementTypeID = at.AgreementTypeID
WHERE
(
b.createdbyuserid = @CreatedByUser
OR @CreatedByUser = - 1
) --OR @CreatedByUser=-2)
AND
(
b.isBrandNewProvider = @NewToPMS
OR @NewToPMS = 'A'
)
ORDER BY a.changeformid
SELECT distinct *
FROM #cfnewassignment
WHERE
@AssignedToUser = '-1'
OR changeformid IN
(
SELECT DISTINCT a.changeformid --,resolutionvalue
FROM #cfnewassignment a
INNER JOIN tblchangeformresolution b
ON a.changeformid = b.changeformid
WHERE resolutionid = 26
AND resolutionvalue = @AssignedToUser
)
END
ELSE
IF @RC = 'Create'
BEGIN
INSERT INTO #cfcompletedlist
SELECT DISTINCT
cf.changeformid,
IsNull(cf.isRush, 'N') AS isRush,
CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
contractentity,
changeformtypename AS changeformtype,
cf.changeformstatus,
dbo.fungetcontractnetworklocations ( cf.contractid ) AS
networklocname,
--ResolutionName,
rg.routinggroupname AS routinggroup,
--tu.firstname+', '+tu.lastname AS RoutingGroupUser,
NULL AS receivedfromrg,
min ( convert ( Varchar (10), cfr.resolutiontime, 101 )) AS resolutiondate, --, TAT =''
cf.contractid,
co.taxID,
tu.firstname + ' ' + tu.lastname AS createdByUserName
FROM tblchangeform cf
LEFT JOIN tblchangeformresolution cfr
ON cf.changeformid = cfr.changeformid
And cfr.resolutiontime = ( Select max(resolutiontime)
From tblchangeformresolution
Where changeformid = cfr.changeformid
and resolutionValue = cfr.resolutionValue)
LEFT JOIN tblchangeformtype cft
ON cft.changeformtypeid = cf.changeformtypeid
LEFT JOIN tblroutinggroup rg
ON rg.routinggroupid = cfr.resolvedbygroupid
LEFT JOIN tblresolution re
ON re.resolutionid = cfr.resolutionid
LEFT JOIN tblcontracts co
ON co.contractid = cf.contractid
LEFT JOIN tblUsers tu ON tu.userid = cf.createdByUserID
WHERE
(
cf.changeformstatus = @CFStatus
OR @CFStatus = 'All'
)
AND
(
IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
OR @notifyMedPoint = 'All'
)
AND
(
(
@execFromDate IS NOT NULL AND
@execToDate IS NOT NULL AND
cf.executedDate BETWEEN cast ( @execFromDate AS Datetime )
AND cast ( @execToDate AS Datetime )
)
OR
(
@execFromDate IS NULL OR @execToDate IS NULL
)
)
AND
(
IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
OR @requireTraining = 'All'
)
AND cfr.resolutionid = 27
AND (cft.changeformtypeid = @CFType OR @CFType = - 1 )
AND cfr.resolvedbygroupid = @RG
AND cfr.resolutiontime BETWEEN cast ( @FromDate AS Datetime )
AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
GROUP BY
cf.changeformid,
cf.changeformstatus,
cf.isRush,
cf.providerNotificationDate,
cf.executedDate,
cf.notifyMedPoint,
cf.doesProviderRequireTraining,
contractentity,
changeformtypename,
cf.contractid,
co.taxID,
rg.routinggroupname,
tu.firstname,
tu.lastname,
cfr.resolutiontime
ORDER BY cf.changeformid, cf.contractid
SELECT distinct
a.*,
tat = '', b.isBrandNewProvider,
-- SUK/Ticket#63828/2010-12-09: Added fields below
co.EffectiveDate,
at.Name as agreementType
FROM #cfcompletedlist a
LEFT JOIN tblchangeform b
ON a.changeformid = b.changeformid
-- SUK/Ticket#63828/2010-12-09: Added join below
LEFT JOIN tblcontracts co
ON co.contractid = b.contractid
LEFT JOIN dbo.tblAgreementTypes at
ON co.AgreementTypeID = at.AgreementTypeID
WHERE
(
b.createdbyuserid = @CreatedByUser
OR @CreatedByUser = - 1
) --OR @CreatedByUser=-2)
AND
(
b.isBrandNewProvider = @NewToPMS
OR @NewToPMS = 'A'
)
ORDER BY a.changeformid
END
ELSE
-- completed
BEGIN
INSERT INTO #cfcompletedlist
SELECT DISTINCT
cf.changeformid,
IsNull(cf.isRush, 'N') AS isRush,
CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
contractentity,
changeformtypename,
cf.changeformstatus,
dbo.fungetcontractnetworklocations ( cf.contractid ) AS networklocname,
--ResolutionName,
rg.routinggroupname AS routinggroup,
--tu.firstname+', '+tu.lastname AS RoutingGroupUser,
NULL AS receivedfromrg,
max ( convert ( Varchar (10), cfr.resolutiontime, 101 )) AS resolutiondate,
cf.contractid,
co.taxID,
tu.firstname + ' ' + tu.lastname AS createdByUserName
FROM tblchangeform cf
LEFT JOIN tblchangeformresolution cfr
ON cf.changeformid = cfr.changeformid
And cfr.resolutiontime = ( Select max(resolutiontime)
From tblchangeformresolution
Where changeformid = cfr.changeformid
and (
(resolutionValue = cfr.resolutionValue)
OR
(cfr.resolutionValue is null and cfr.resolutionid = 28) --completed
))
LEFT JOIN tblchangeformtype cft
ON cft.changeformtypeid = cf.changeformtypeid
LEFT JOIN tblroutinggroup rg
ON rg.routinggroupid = cfr.resolvedbygroupid
LEFT JOIN tblresolution re
ON re.resolutionid = cfr.resolutionid
LEFT JOIN tblcontracts co
ON co.contractid = cf.contractid
LEFT JOIN tblUsers tu ON tu.userid = cf.createdByUserID
WHERE
(
cf.changeformstatus = @CFStatus
OR @CFStatus = 'All'
)
AND
(
IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
OR @notifyMedPoint = 'All'
)
AND
(
(
@execFromDate IS NOT NULL AND
@execToDate IS NOT NULL AND
cf.executedDate BETWEEN cast ( @execFromDate AS Datetime )
AND cast ( @execToDate AS Datetime )
)
OR
(
@execFromDate IS NULL OR @execToDate IS NULL
)
)
AND
(
IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
OR @requireTraining = 'All'
)
AND
(
cfr.resolutionid IN ( 25, 28, 29 )
OR
(
cfr.resolutionid = 18
AND cf.changeformstatus = 'Canceled'
)
)
AND
(
cft.changeformtypeid = @CFType
OR @CFType = - 1
)
AND cfr.resolvedbygroupid = @RG
AND cfr.resolutiontime BETWEEN cast ( @FromDate AS Datetime )
AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
GROUP BY
cf.changeformid,
cf.changeformstatus,
cf.isRush,
cf.providerNotificationDate,
cf.executedDate,
cf.notifyMedPoint,
cf.doesProviderRequireTraining,
contractentity,
changeformtypename,
cf.contractid,
co.taxId,
rg.routinggroupname,
tu.firstname,
tu.lastname,
cfr.resolutiontime
ORDER BY cf.changeformid, cf.contractid
IF object_id ( 'tempdb..#CFgroupbyRG', 'U' ) IS NOT NULL
DROP TABLE #cfgroupbyrg
CREATE TABLE #cfgroupbyrg
(
changeformid Int,
routinggroupname Varchar (100),
tat Int
)
INSERT INTO #cfgroupbyrg
SELECT
changeformid,
routinggroupname,
sum ( isnull ( tad, 0 )) AS tad
FROM tblchangeformresolution a
INNER JOIN tblroutinggroup b
ON a.resolvedbygroupid = b.routinggroupid
--Where a.resolutionid IN ( 25, 27, 18 )
GROUP BY changeformid, routinggroupname
INSERT INTO #cfnewassignment
SELECT
a.*,
b.tat,
c.isBrandNewProvider,
-- SUK/Ticket#63828/2010-12-09: Added fields below
co.EffectiveDate,
at.Name as agreementType
FROM #cfcompletedlist a
INNER JOIN #cfgroupbyrg b
ON a.changeformid = b.changeformid
AND b.routinggroupname = a.routinggroup
LEFT JOIN tblchangeform c
ON c.changeformid = a.changeformid
-- SUK/Ticket#63828/2010-12-09: Added join below
LEFT JOIN tblcontracts co
ON co.contractid = c.contractid
LEFT JOIN dbo.tblAgreementTypes at
ON co.AgreementTypeID = at.AgreementTypeID
WHERE
(
c.createdbyuserid = @CreatedByUser
OR @CreatedByUser = - 1
) -- OR @CreatedByUser=-2)
AND
(
c.isBrandNewProvider = @NewToPMS
OR @NewToPMS = 'A'
)
ORDER BY a.changeformid
SELECT distinct *
FROM #cfnewassignment
WHERE
@AssignedToUser = '-1'
OR changeformid IN
(
SELECT DISTINCT a.changeformid --,resolutionvalue
FROM #cfnewassignment a
INNER JOIN tblchangeformresolution b
ON a.changeformid = b.changeformid
WHERE resolutionid = 26
AND resolutionvalue = @AssignedToUser
)
END