MSSQL说我正在尝试将varchar转换为字符串,而不是

时间:2016-05-25 10:44:22

标签: sql stored-procedures sql-server-2012

所以我在工作中有这个相当长的程序,我刚才做的。它的作用并不重要,但最终结果才是最重要的。

我需要在表中计算一些不同类型的描述,并且工作正常。然后,我需要将我计算的两件事放在一个字符串中,然后返回到我的软件中。但是,每次我运行此程序时,它都会给我这个:

  

Msg 245,Level 16,State 1,Procedure WorkDays,Line 43 Conversion   转换varchar值时失败   'FlightDeck:161,CabinCrew:189'到数据类型int。

我无法弄清楚为什么当我没有尝试将varchar转换为int而是将int转换为单个varchar时,它一直告诉我这个。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[WorkDays] @requestedDate nchar(10)

AS
SET ANSI_WARNINGS OFF
DECLARE @date as nchar(10) = ''
DECLARE @returnVal as varchar(30) = ''
DECLARE @flightDeck as int = 0
DECLARE @cabinCrew as int = 0
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
SET @date = @requestedDate

SELECT
    @flightDeck = SUM(CASE WHEN dbo.Crew_Category.Description LIKE 'Flight Deck' THEN 1 END),
    @cabinCrew = SUM(CASE WHEN dbo.Crew_Category.Description LIKE 'Cabin Crew' THEN 1 END)
FROM
    dbo.CrewMember INNER JOIN
    dbo.Crew_Category ON dbo.CrewMember.CrewCategorySeqNo = dbo.Crew_Category.CrewCategorySeqno
WHERE        
    (dbo.Crew_Category.Description = N'Flight Deck' OR
    dbo.Crew_Category.Description = N'Cabin Crew') AND
    (dbo.CrewMember.EmploymentEndDate > @date)
    AND dbo.CrewMember.CrewSeqno NOT IN (
        SELECT
            CrewMember_1.CrewSeqno
        FROM 
            dbo.CrewMember AS CrewMember_1 INNER JOIN
            dbo.CrewReqAsg ON CrewMember_1.CrewSeqno = dbo.CrewReqAsg.crewSeqno INNER JOIN
            dbo.activity ON dbo.CrewReqAsg.act_seqno = dbo.activity.act_seqno INNER JOIN
            dbo.ActivityType ON dbo.activity.actType_seqno = dbo.ActivityType.actType_seqno INNER JOIN
            dbo.ActivityCategory ON dbo.ActivityType.ActCat_seqno = dbo.ActivityCategory.actCat_seqno INNER JOIN
            dbo.Crew_Category AS Crew_Category_1 ON CrewMember_1.CrewCategorySeqNo = Crew_Category_1.CrewCategorySeqno
        WHERE (
            dbo.ActivityCategory.Category = N'Ferie' OR
            dbo.ActivityCategory.Category = N'Fridage' OR
            dbo.ActivityCategory.Category = N'Sygdom') AND (Crew_Category_1.Description = N'Flight Deck' OR
            Crew_Category_1.Description = N'Cabin Crew') AND (LEFT(dbo.activity.Start,10) LIKE @date));
            SET @returnVal = 'FlightDeck:'+CAST(@flightDeck AS varchar);
            SET @returnVal += ',CabinCrew:'+CAST(@cabinCrew AS varchar);
END
RETURN @returnVal

已经有一段时间了,因为我不得不这样做,所以也许我只是忘了一些基本的东西。请帮我弄清楚为什么会这样? :)

1 个答案:

答案 0 :(得分:1)

是的,你遗忘了一些基本的东西。要将数据返回给调用者,请使用SELECT,而不是RETURN。

你需要

SELECT @returnVal