mahesh
mahesh

Reputation: 1390

Ambiguous Sql Error From VS-2005

It is serious matters: is it fault in Sql Query Analyzer?.

I have face strange situation that I have tested following query on Sql Query Analyzer:

insert into PLCommonTotal(CmnTotal)
select
case
when p.NetpurTot > s.NetsalTot then
(
select NetpurTot from PLPurchaseTotal
)
else
(
Select NetsalTot from PLSaleTotal
)
end
from PLPurchaseTotal p
join PLSaleTotal s
on p.companyID=s.companyID

select * from PLCommonTotal

It is working very well no error from Sql Query Analyzer.

Now look at the same query in VS-2005 :

string tot5 = " insert into PLCommonTotal(CmnTotal)" +
                " select" +
                " case" +
                " when p.NetpurTot > s.NetsalTot then" +
                " (" +
                " select NetpurTot from PLPurchaseTotal" +
                " )" +
                " else" +
                " (" +
                " Select NetsalTot from PLSaleTotal" +
                " )" +
                " end" +
                " from PLPurchaseTotal p" +
                " join PLSaleTotal s" +
                " on p.companyID=s.companyID";
            SqlCommand comcmd = new SqlCommand(tot5, con);
            comcmd.Transaction = trans;
            comcmd.ExecuteNonQuery();

This is throw Sql Error : “ subquery return more than one value. This is not permitted when the subquery follows = != < <= > >= or when the subquery used an expression. The statement has been termited”.

I want to ask the question if it is not permitted than why it is executed from SQL Query analyzer without any error?.

Is there fault of SQL Team?.

Upvotes: 0

Views: 84

Answers (1)

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

As you have written the query this will happen if you have more than one row in either PLPurchaseTotal or PLSaleTotal. I think that you are not using the same DB in SSMS and in VS-2005.

On the other hand i suspect that this is the query you are aiming for.

insert into PLCommonTotal(CmnTotal)
select
  case
    when p.NetpurTot > s.NetsalTot
    then p.NetpurTot
    else s.NetsalTot
  end
from PLPurchaseTotal p
  join PLSaleTotal s
    on p.companyID=s.companyID

Upvotes: 2

Related Questions