Excel中的SQL Server存储过程数据类型转换

时间:2016-11-14 15:26:54

标签: sql-server excel stored-procedures

我有一个存储过程可以执行我期望它在SQL中返回我输入的任何年份的数据,但是我很难将存储过程绑定到Excel电子表格,这将不允许我这样做由于在尝试将varchar转换为int时返回的错误。这是一个相对较小的SQL,但实际上我想将@Year参数绑定到最终用户的Excel单元格。

它声明在Excel中将数据类型varchar转换为tinyint“时出错。任何帮助都很受欢迎。以下是存储过程。

我在Excel中的定义字符串:

  

执行dbo.StoredProcedure'年'

存储过程:

@Year Tinyint
AS 

SELECT DBO.users.loginname,
       Cast(Datename(M, DBO.contractdate.contractdate) AS VARCHAR) AS DatePeriod,
       Sum(CASE
             WHEN DBO.contract.contract = 1 THEN 1
             ELSE 0
           END)                                                    AS Contract,
       Sum(CASE
             WHEN DBO.contract.contract = 0 THEN 1
             ELSE 0
           END)                                                    AS Permanant
FROM   DBO.contractskill
       INNER JOIN DBO.users
                  INNER JOIN DBO.contract
                          ON DBO.users.userid = DBO.contract.userid
                  INNER JOIN DBO.contractdate
                          ON DBO.contract.contractid = DBO.contractdate.contractid
               ON DBO.contractskill.contractid = DBO.contract.contractid
WHERE  ( DBO.contractdate.dns = 0 )
       AND ( DBO.contractskill.skillid = 30960 )
       AND Year(DBO.contractdate.contractdate) = @Year
GROUP  BY Cast(Datename(M, DBO.contractdate.contractdate) AS VARCHAR),
          Month(DBO.contractdate.contractdate),
          DBO.users.loginname
ORDER  BY Month(DBO.contractdate.contractdate)  

0 个答案:

没有答案