存储过程中的SQL Server浮点比较

时间:2017-05-26 15:53:14

标签: sql-server stored-procedures floating-point

不幸的是,我有两个表来比较之间的float数据类型。我已经阅读了关于尝试演员,转换,使用一个小差异并尝试了所有。

奇怪的是,这只在我执行存储过程时失败。如果我将存储过程的主体剪切并粘贴到SSMS窗口中,它的效果非常好。

示例SQL:

{"coupon":[{"amount":"60","endDate":"05/09/2020","id":"5","message":"warranty for 1 year","price":"200.99","startDate":"20/02/2014","title":"sunglasses","type":"FASHION"},{"amount":"149","endDate":"04/12/2020","id":"6","message":"only big sizes","price":"79.9","startDate":"07/08/2014","title":"pajamas","type":"FASHION"}]}  

我的<input name="rdSchedule" type="radio" id="rdSchedule" value="SCHEDULE" />&nbsp; <label>Schedule</label>&nbsp;&nbsp; <input name="rdProgram" type="radio" id="rdProgram" value="PROGRAM" />&nbsp; <label>Program</label> <input name="rdZeroFee" type="radio" id="rdZeroFee" value="ZEROFEE" />&nbsp; <label>Zero Fee</label> <script type="text/javascript"> function ScheduleEdits() { var regfee1 = $('#<%=Inputmask_RegFee1.ClientID%>').val().replace(/_/g, ""); var regfee2 = $('#<%=Inputmask_RegFee2.ClientID%>').val().replace(/_/g, ""); var regfee3 = $('#<%=Inputmask_RegFee3.ClientID%>').val().replace(/_/g, ""); var regfee4 = $('#<%=Inputmask_RegFee4.ClientID%>').val().replace(/_/g, ""); var regfee5 = $('#<%=Inputmask_RegFee5.ClientID%>').val().replace(/_/g, ""); var regfee6 = $('#<%=Inputmask_RegFee6.ClientID%>').val().replace(/_/g, ""); var regfee7 = $('#<%=Inputmask_RegFee7.ClientID%>').val().replace(/_/g, ""); var regfee8 = $('#<%=Inputmask_RegFee8.ClientID%>').val().replace(/_/g, ""); var regfee9 = $('#<%=Inputmask_RegFee9.ClientID%>').val().replace(/_/g, ""); var regfee10 = $('#<%=Inputmask_RegFee10.ClientID%>').val().replace(/_/g, ""); var progTypeSched = $("#rdSchedule").prop("checked"); alert("Fee 1 = " + regfee1); alert("Program Type = " + programtype); if (progTypeSched && regfee1 == '0' && regfee2 == '0' && regfee3 == '0' && regfee4 == '0' && regfee5 == '0' && regfee6 == '0' && regfee7 == '0' && regfee8 == '0' && regfee9 == '0' && regfee10 == '0') { alert('You must enter at lease one fee value for the schedule program.'); $('#<%=Inputmask_RegFee1.ClientID%>').focus(); return false; } else { return true; } } </script> 变量也是set @newEnvRiskLevel = -1 select @newEnvRiskLevel = rl.RiskLevelId from LookupTypes lt inner join RiskLevels rl on lt.LookupTypeId = rl.RiskLevelTypeFk where lt.Code = 'RISK_LEVEL_ENVIRONMENTAL' and convert(numeric(1, 0), rl.RiskFactor) = @newEnvScore set @errorCode = @@ERROR if (@newEnvRiskLevel = -1 or @errorCode != 0) begin print 'newEnvScore = ' + cast(@newEnvScore as varchar) + ' and risk level = ' + cast(isnull(@newEnvRiskLevel, -1) as varchar) print 'ERROR finding environmental risk level for code ' + @itemCode + ', skipping record' set @recordsErrored = @recordsErrored + 1 goto NEXTREC end 转换为@newEnvScore。我已经验证了RiskFactor列中的值只有0,1,2和3,并且(通过调试)float的值为2.我还验证了我的查询有一个代码='RISK_LEVEL_ENVIRONMENTAL'和RiskFactor = 2的行。

我已通过调试验证失败是由于@newEnvRiskLevel保持在-1并且@errorCode为0。

我也尝试过cast到decimal和int,转换为int,并在我的where子句中使用“rl.RiskFactor - @newEnvScore&lt; 1”,其中没有一个设置newEnvRiskLevel。

正如我所说的那样,当它作为存储过程运行时,只有 才会发生故障,这是我真正不理解的部分。我希望SQL Server是确定性的,无论SQL是运行存储过程的主体,还是在SSMS选项卡中运行完全相同的SQL。

1 个答案:

答案 0 :(得分:0)

很遗憾,您既不发布存储过程也不发布完整脚本。没有有用的演示很难诊断出问题。但是我看到使用&#34; goto&#34;这在许多方面都有关系。我还看到使用select语句来分配局部变量 - 这通常是一个问题,因为开发人员可能假设总是发生一个赋值。为了证明 - 最后有奖金

set nocount on;
declare @risk smallint; 
declare @risklevels table (risklevel float primary key, code varchar(10)); 
insert @risklevels(risklevel, code) values (1, 'test'), (2, 'test'), (-5, 'test'); 

-- here is your assignment logic. Notice that @risk is 
-- never changed because there are no matching rows. 
set @risk = 0; 
select @risk = risklevel from @risklevels where code = 'zork'; 
select @risk; 

-- here is a better IMO way to make the assignment. Note that
-- @risk is set to NULL when there are no matching rows. 
set @risk = -1; 
set @risk = (select risklevel from @risklevels where code = 'zork'); 
select @risk; 

-- and a last misconception. What value is @risk set to? and why? 
set @risk = -1; 
select @risk = risklevel from @risklevels where code = 'test'; 
select @risk; 

这是否是您的问题的根源(或有助于它)我不能说。但这是一种可能性。并且以浮点数据类型存储整数通常只是一个问题。即使您无法更改表,也可以更改局部变量并强制使用更合适的数据类型。所以也许这是你应该考虑的另一个变化。