必须声明标量变量“ @s”

时间:2018-06-22 22:40:59

标签: c# sql-server

CmdString = "SELECT s.TotallBill - s.Advance - SUM(iph.Amount) AS RemainingBalance FROM SalesInvoice s INNER JOIN InstallmentPaymentHistory iph ON iph.SalesInvoiceId = s.SalesInvoiceID WHERE  [s.SalesInvoiceID] = @s.SalesInvoiceID";

try
{
    SqlCommand cmd = new SqlCommand(CmdString, con);
    cmd.Parameters.AddWithValue("@SalesInvoiceID", txtSIN2.Text);

    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
        DataTable dt = new DataTable("SalesInvoice");
        sda.Fill(dt);

        foreach (DataRow dr in dt.Rows)
        {
            txtBalance2.Text = (dr["RemainingBalance"].ToString());
        }
    }
}
catch (Exception exp)
{
    MessageBox.Show(exp.ToString());
}

我收到错误

  

必须声明标量变量“ @s”

在一个文本框中,我使用s.SalesInvoiceID的值。我该如何解决错误?请回答我正确的查询

1 个答案:

答案 0 :(得分:1)

不太清楚意图是什么,但是您的Select语句缺少与Sum一起使用的Group by或Over子句。此外,您的Select中的参数名称必须与您在Parameters.AddWithValue中使用的名称匹配。也许是这样的。

"SELECT 
s.SalesInvoideID
s.TotallBill, 
s.Advance, 
sum(iph.Amount) as RemainingBalance 
from SalesInvoice s 
inner join InstallmentPaymentHistory iph 
on 
s.SalesInvoiceID =iph.SalesInvoiceId
where 
[s.SalesInvoiceID]=@SalesInvoiceID"
Group By s.SalesInvoiceID;"

该联接向后看向我。我不是Transact SQL的专家,但我认为Inner实际上是Left联接,或者仅仅是Join和s.SalesInvoiceID属于左侧。