Transact-SQL 程序:为什么我的 else 循环不起作用?
Transact-SQL procedures : why does my else loop not work?
当用户输入国家名称时,我需要打印国家列表以及其他客户数量等。我的 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
if 中的这行代码总是 return 一些东西,因为你没有 where 子句
select shipcountry from Sales.Orders
尝试这样的事情
select 1 from Sales.Orders where shipcountry = @land
并删除 else 上的条件,不改变 maintianable 以在两个地方反转相同的条件。
当用户输入国家名称时,我需要打印国家列表以及其他客户数量等。我的 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
if 中的这行代码总是 return 一些东西,因为你没有 where 子句
select shipcountry from Sales.Orders
尝试这样的事情
select 1 from Sales.Orders where shipcountry = @land
并删除 else 上的条件,不改变 maintianable 以在两个地方反转相同的条件。