我收到错误Error converting data type varchar to float.
我只使用浮动一个而它在@total
。我需要使用float进行数学计算。
我在评论中显示,如果我双击,它会突出显示。
我不确定为什么会收到错误。
CREATE PROCEDURE [dbo].[p_TargetStatus]
(
@Year INT,
@Quarter NVARCHAR(max),
@LeadAssessor NVARCHAR(max),
@ORG_ID INT
)
AS
BEGIN
Declare @Server As nvarchar(Max)
Declare @serverMan As nvarchar(Max)
DECLARE @Tsql1 AS nvarchar(MAX)
DECLARE @Tsql2 AS nvarchar(MAX)
DECLARE @MANOwner AS NVARCHAR(10)
DECLARE @MANDB AS NVARCHAR(200)
--Server details
SET NOCOUNT ON;
IF OBJECT_ID('#tmp1') IS NOT NULL
DROP TABLE #tmp1
SET NOCOUNT ON
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE] @Year,@Quarter
DECLARE @Total AS FLOAT
SET @Tsql1 = 'SELECT '+@Total+' = COUNT(*)
FROM ( --If I double click it shows error here
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN ''Over Due''
ELSE ''On Time'' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t2.AUD_Deleted = 0
AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@LeadAssessor as nvarchar(max))+'''))
AND AUD_Year = '+CAST(@Year as varchar)+'
AND t2.AUD_ORGID IN (Select Org_ID From ['+ @MANDB +'].['+ @MANOwner +'].fx_Rights_ORGIDs( '+ CAST(@ORG_ID AS VARCHAR) + '))
AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues]('''+CAST(@Quarter as nvarchar(max))+''')))DER'
SET @Tsql2 = 'SELECT ([Target Status] + '''' + CAST(COUNT(*) AS NVARCHAR(255)) + '' of '' ('+ CAST(@Total AS NVARCHAR(255))+') AS TargetStatus, CAST(COUNT(*) AS FLOAT) /'+ CAST(@Total AS FLOAT)+' AS [Count]
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) > CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101))--AND t1.[Status] in (''Open'',''Closed'')
THEN ''Over Due: ''
ELSE ''On Time: '' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t2.AUD_Deleted = 0
AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@LeadAssessor as nvarchar(max))+'''))
AND t2.AUD_Year = '+CAST(@Year as varchar)+'
AND t2.AUD_ORGID IN (Select Org_ID From ['+ @MANDB +'].['+ @MANOwner +'].fx_Rights_ORGIDs( '+ CAST(@ORG_ID AS VARCHAR) + '))
AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@Quarter as nvarchar(max))+''')))DER
GROUP BY [Target Status]'
EXEC sp_executesql
@Tsql1
END
GO
我尝试了@Total
和Cast(@Total)
等内容。
答案 0 :(得分:0)
看一下这个链接,你无法通过你使用它的方式获得动态查询的输出。您必须使用IN和OUT参数
How to get sp_executesql result into a variable?
CREATE PROCEDURE [dbo].[p_TargetStatus]
(
@Year INT,
@Quarter NVARCHAR(max),
@LeadAssessor NVARCHAR(max),
@ORG_ID INT
)
AS
BEGIN
Declare @Server As nvarchar(Max)
Declare @serverMan As nvarchar(Max)
DECLARE @Tsql1 AS nvarchar(MAX)
DECLARE @Tsql2 AS nvarchar(MAX)
DECLARE @MANOwner AS NVARCHAR(10)
DECLARE @MANDB AS NVARCHAR(200)
--Server details
SET NOCOUNT ON;
IF OBJECT_ID('#tmp1') IS NOT NULL
DROP TABLE #tmp1
SET NOCOUNT ON
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE] @Year,@Quarter
DECLARE @Total AS FLOAT
SET @Tsql1 = 'SELECT @retvalOUT = COUNT(*)
FROM ( --If I double click it shows error here
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN ''Over Due''
ELSE ''On Time'' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t2.AUD_Deleted = 0
AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@LeadAssessor as nvarchar(max))+'''))
AND AUD_Year = '+CAST(@Year as varchar)+'
AND t2.AUD_ORGID IN (Select Org_ID From ['+ @MANDB +'].['+ @MANOwner +'].fx_Rights_ORGIDs( '+ CAST(@ORG_ID AS VARCHAR) + '))
AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues]('''+CAST(@Quarter as nvarchar(max))+''')))DER'
DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@retvalOUT int OUTPUT';
EXEC sp_executesql @Tsql1, @ParmDefinition, @retvalOUT=@Total OUTPUT;
SET @Tsql2 = 'SELECT ([Target Status] + '''' + CAST(COUNT(*) AS NVARCHAR(255)) + '' of '' ('+ CAST(@Total AS NVARCHAR(255))+') AS TargetStatus, CAST(COUNT(*) AS FLOAT) /'+ CAST(@Total AS varchar(100))+' AS [Count]
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) > CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101))--AND t1.[Status] in (''Open'',''Closed'')
THEN ''Over Due: ''
ELSE ''On Time: '' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t2.AUD_Deleted = 0
AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@LeadAssessor as nvarchar(max))+'''))
AND t2.AUD_Year = '+CAST(@Year as varchar)+'
AND t2.AUD_ORGID IN (Select Org_ID From ['+ @MANDB +'].['+ @MANOwner +'].fx_Rights_ORGIDs( '+ CAST(@ORG_ID AS VARCHAR) + '))
AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('''+CAST(@Quarter as nvarchar(max))+''')))DER
GROUP BY [Target Status]'
END
GO