當用戶輸入國家名稱時,我需要列印一個國家串列以及其他數量的客戶等。我的 if 回圈有效,但 else 回圈無效。當用戶輸入不在列中的國家/地區時,應執行 else 回圈。它應該列印訊息和可供選擇的國家/地區串列。有人可以說我的代碼有什么問題嗎?
create or alter proc [usp_sales per country total plus different customer]
@land varchar(100)
as
if exists (select shipcountry from Sales.Orders)
begin
select distinct
shipcountry,
count(orderid) as CustomerTotal,
count(distinct custid) as [Different Customer]
from
sales.orders
where
@land = shipcountry
group by
shipcountry
end
else if not exists (select shipcountry from Sales.Orders)
begin
print 'Land nicht gefunden - W?hlen Sie eines aus der Liste aus'
select shipcountry
from Sales.orders
end
exec [usp_sales per country total plus different customer] china
uj5u.com熱心網友回復:
if 中的這行代碼總是會回傳一些東西,因為你沒有 where 子句
select shipcountry from Sales.Orders
嘗試這樣的事情
select 1 from Sales.Orders where shipcountry = @land
并洗掉其他條件,不改變可維護性以在兩個地方反轉相同的條件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/467829.html
上一篇:為什么我需要創建一個子查詢而不是在這種情況下進行比較?
下一篇:NOTIN與連接列
