sql server - Passing parameters from Excel to SQL -
alter procedure [dbo].[sp_hello] @portfolio nvarchar(5), @discount numeric(18,2), @lowerlimit numeric(18,2), @midlimit numeric(18,2) begin set nocount on; select distinct entitydescription, d.amountdueincurr * @discount [1st part], d.amountdueincurr * (1 - @discount) [2nd part], case when d.amountdueincurr <= 0 'solution 1' when d.amountdueincurr between 0 , @lowerlimit 'solution 2' when d.amountdueincurr between (@lowerlimit + 0.01) , @midlimit concat(@portfolio, '-01') when d.amountdueincurr > (@midlimit+0.01) concat(@portfolio, '-02') else 'error' end solution_type t1 d
and pass parameters excel, using vba one:
private sub commandbutton1_click() dim portfolio string dim discount single dim lowerlimit single dim midlimit 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 activeworkbook.connections("hello_data").oledbconnection .commandtext = "exec sp_hello '" & portfolio & "','" & discount & "','" & lowerlimit & "','" & midlimit & "'" activeworkbook.connections("hello_data").refresh end worksheets(2).activate end sub
seems no matter (format, declare or dim variables/parameters), conversion data type error in excel. have tried string double in excel numerical combinations in sql, none of combinations seem work.
if pass portfolio parameter (string) nvarchar in database, goes well. add number, or date combination, conversion errors appear.
your stored proc parameters have numeric data types defined, tsql string built passing in strings. need remove single quote placing around parameters @discount
, @lowerlimit
, @midlimit
.
to see issue, add code:
dim tsql string tsql = "exec sp_hello '" & portfolio & "','" & discount & "','" & lowerlimit & "','" & midlimit & "'"
print out tsql, , execute in ssms. should see issue right away.
Comments
Post a Comment