如何解决这个错误 [英] How to solve this error
问题描述
我收到此错误..
子查询返回的值超过1。当子查询跟随=,!=,<,< =,>,>时,不允许这样做; =或当子查询用作表达式时。
我写了这个程序..
////////////// ////////////////////////////////////////////////// ///////////////
I am getting this error..
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
I have written this procedure..
///////////////////////////////////////////////////////////////////////////////
alter procedure myprocedure
(
@flag varchar(1),
@id varchar(25),
@amt decimal(10,2)
)
as
begin
if @flag='D'
begin
declare @balance decimal(10,2)
set @balance=(select balance from mytransaction where emailid=@id);
if (@balance IS NULL)
begin
set @balance=@amt;
end
else
begin
set @balance=@balance+@amt;
end
insert into mytransaction values(@id,@balance,GETDATE(),'DEPOSIT',@amt)
end
else if @flag='W'
begin
declare @chkbalance decimal(10,2)
set @chkbalance=(select balance from mytransaction where emailid=@id);
if (@amt>@chkbalance)
begin
SELECT 'Amount cannot be greater than balance.' AS 'Result'
end
else
begin
declare @wbalance decimal(10,2)
set @wbalance=(select balance from mytransaction where emailid=@id);
set @wbalance=@wbalance-@amt;
insert into mytransaction values(@id,@wbalance,GETDATE(),'WITHDRAW',@amt)
SELECT 'You have withdraw ammount of -->' + str(@amt) AS 'Result'
end
end
end
//////////////// ////////////////////////////////////////////////// ////////////
并在代码文件中写下以下代码行
/ ////////////////////////////////////////////////// ////////////////////////////
//////////////////////////////////////////////////////////////////////////////
and have written follwing line of code in code file
///////////////////////////////////////////////////////////////////////////////
protected void btntransaction_Click(object sender, EventArgs e)
{
cn.Open();
if (rdbwithdraw.Checked == true)
{
cmd = new SqlCommand("myprocedure", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", SqlDbType.VarChar).Value = "W";
cmd.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = hdnname.Value;
cmd.Parameters.AddWithValue("@amt",SqlDbType.Decimal).Value = txtamt.Text;
da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
Response.Write(dt.Rows[0]["Result"].ToString());
}
else
{
cmd = new SqlCommand("myprocedure", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", SqlDbType.VarChar).Value = "D";
cmd.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = hdnname.Value;
cmd.Parameters.AddWithValue("@amt", SqlDbType.Decimal).Value = txtamt.Text;
cmd.ExecuteNonQuery();
Response.Write("You have deposited ammount of -->" + txtamt.Text);
txtamt.Text = "";
}
cn.Close();
}
推荐答案
从mytransaction中选择余额,其中emailid = @ id
这可能会返回多个结果。因此,你得到这个错误。
select balance from mytransaction where emailid=@id
This is possibly returning more than one result. Thus you are getting this error.
如果你有任何列,如dateTime,transactionid将给你当前的最后一笔交易....然后与前1 ..u必须使用订单通过desc date,transaction id ...它将给你最后一笔交易行
exa。
if u have any column such as dateTime,transactionid which will give u current last transaction....then with top 1 ..u have to use order by desc date,transaction id ...it will give u last transaction row
exa.
"select top 1 balance from myTransaction where emailid=@id order by dateTime1 desc "
这篇关于如何解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!