ALTER PROCEDURE [dbo].[SP_Hello]
@portfolio nvarchar(5),
@discount numeric(18,2),
@lowerlimit numeric(18,2),
@midlimit numeric(18,2)
AS
BEGIN
SET NOCOUNT ON;
SELECT distinct
EntityDescription,
d.AmountDueInCurr * @discount as [1st part],
d.AmountDueInCurr * (1 - @discount) as [2nd Part],
CASE WHEN d.AmountDueInCurr <= 0 THEN 'Solution 1'
WHEN d.AmountDueInCurr between 0 and @lowerlimit THEN 'Solution 2'
WHEN d.AmountDueInCurr between (@lowerlimit + 0.01) and @midlimit THEN CONCAT(@portfolio, '-01')
WHEN d.AmountDueInCurr > (@midlimit+0.01) THEN CONCAT(@portfolio, '-02')
ELSE 'Error'
END AS Solution_type
FROM
T1 d
我希望从Excel传递参数,使用VBA作为这一个:
Private Sub CommandButton1_Click()
Dim Portfolio As String
Dim Discount As Single
Dim lowerlimit As Single
Dim midlimit As Single
Portfolio = Sheets("Input").Range("B2").Value
Popust = Sheets("Input").Range("B3").Value
lowerlimit = Sheets("Input").Range("B4").Value
midlimit = Sheets("Input").Range("B5").Value
With ActiveWorkbook.Connections("Hello_data").OLEDBConnection
.CommandText = "exec SP_Hello '" & Portfolio & "','" & Discount & "','" & lowerlimit & "','" & midlimit & "'"
ActiveWorkbook.Connections("Hello_data").Refresh
End With
Worksheets(2).Activate
End Sub
似乎无论我做什么(格式化,声明或暗淡变量/参数),我总是在Excel中得到转换数据类型错误。我在Excel中尝试了从String到Double的所有内容以及SQL中的所有数字组合,但这些组合似乎都不起作用。
如果我只传递数据库中nvarchar的Portfolio参数(字符串),那就顺利了。只要我在组合中添加数字或日期,就会出现转换错误。
答案 0 :(得分:0)
您的存储过程参数已定义数值数据类型,但您构建的TSQL字符串是以字符串形式传递的。您需要删除参数@discount
,@lowerlimit
和@midlimit
周围的单引号。
要查看此问题,只需添加以下代码:
Dim tsql As String
tsql = "exec SP_Hello '" & Portfolio & "','" & Discount & "','" & lowerlimit & "','" & midlimit & "'"
打印出tsql,并在SSMS中执行。您应该立即看到问题。