查询的奇怪问题... decimal.parse在存储过程中舍入,但不在查询中 [英] weird problem with query...decimal.parse rounding in stored procedure, but not in query

查看:72
本文介绍了查询的奇怪问题... decimal.parse在存储过程中舍入,但不在查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,它根据第2列的值从第1列中提取值。然后第1列的值输出到显示在标签中的网页。



i有输入变量bc和输出变量fileName。例如,如果我输入10.00作为bc值,则fileSymbol会吐出正确的值,在这种情况下它是'0'。



如果我使用9.00作为输入,fileSymbol是正确的。



如果我使用9.7,但是,sproc最多为10,然后给我INCORRECT fileSymbol



相反,如果我使用9.3作为输入,sproc将其视为9.0,再次,我得到错误的符号。



我不太清楚如何解决这个问题。



这是我的代码,谢谢你的帮助...



存储过程:



i have a stored procedure which pulls the value from column 1 based on the value of column 2. the value from column 1 is then output to the webpage which displays it in a label.

i have input variable bc and output variable fileName. if, for example, i type in 10.00 for the bc value, the fileSymbol spits out the correct value, in this case it's '0'.

if i use 9.00 as the input, the fileSymbol is correct.

if i use 9.7, however, the sproc rounds up to 10 and then gives me the INCORRECT fileSymbol

conversely, if i use 9.3 as the input, the sproc treats this as 9.0 and again, i get the incorrect symbol.

i'm very unsure of how to fix this.

here's my code and thank you for any help...

stored proc:

<pre lang="SQL">
ALTER PROCEDURE getFilename
(
	@bc decimal,
	@fileSymbol varchar(50) output
)

AS
SELECT @fileSymbol=fileSymbol
FROM tempSymTab
WHERE base = @bc





网页:



webpage:

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
                        <asp:Label ID="Label1" runat="server"></asp:Label>  
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>





codebehind(是的它很草率,我正在尝试测试并确保我先得到正确的值)



codebehind (yes it's sloppy, i'm trying to test and make sure i get the right value first)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string connSTR = ConfigurationManager.ConnectionStrings["connStr"].ToString();

            SqlConnection conn = new SqlConnection(connSTR);

            Class1 c1 = new Class1();



            SqlCommand cmd = new SqlCommand("dbo.getFileName", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@bc", SqlDbType.Decimal).Value = decimal.Parse(TextBox1.Text);

            SqlParameter parm2 = new SqlParameter("@fileSymbol", SqlDbType.VarChar);
            parm2.Size = 50;
            parm2.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(parm2);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            string outputValue = cmd.Parameters["@fileSymbol"].Value.ToString();
            Label1.Text = outputValue;
        
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connSTR = ConfigurationManager.ConnectionStrings["connStr"].ToString();

        SqlConnection conn = new SqlConnection(connSTR);

        Class1 c1 = new Class1();
        
        

        SqlCommand cmd = new SqlCommand("dbo.getFileName", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@bc", SqlDbType.Decimal).Value = decimal.Parse(TextBox1.Text);

        SqlParameter parm2 = new SqlParameter("@fileSymbol", SqlDbType.VarChar);
        parm2.Size = 50;
        parm2.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(parm2);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        string outputValue = cmd.Parameters["@fileSymbol"].Value.ToString();
        Label1.Text = outputValue;
        

    }

推荐答案

检查:

Check this:
DECLARE @bc VARCHAR(10)

SET @bc = '9.1'
SELECT CONVERT(DECIMAL, @bc) As NewBC
SELECT CONVERT(DECIMAL(8,0), @bc) As NewBC
SELECT CONVERT(DECIMAL(8,2), @bc) As NewBC





结果:



Results:

9
9
9.1





你看到了区别吗?



请看看这里:数据类型(T-SQL) [ ^ ]


这篇关于查询的奇怪问题... decimal.parse在存储过程中舍入,但不在查询中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆