【发布时间】:2013-12-06 01:07:24
【问题描述】:
您好,我使用的是 SQL Server 2008 R2。想做以下事情:
declare
-- only one of these @ vars will be used
@specificAccountNumber int = NULL
@specificAccountNumber int = 123456
select
account
,accountinfo1
,accountinfo2
from tableA
where
if @specificAccountNumber is NULL
then use the whole table
else /* @specificaAccountNumber is not null */
account = @specificAccountNumber
select
account
,accountinfo3
,accountinfo4
from tableB
where
if @specificAccountNumber is NULL
then use the whole table
else /* @specificAccountNumber is not null */
account = @specificAccountNumber
如果我没有指定帐号(或将 @specificAccountNumber 设置为 NULL),则目标是查询选择所有行,但如果我指定了,那么它只会为该帐号带来数据。
【问题讨论】:
-
任何答案都解决了您的问题吗?如果是这样,请考虑接受它。如果没有,我建议发布您自己的答案。 (StackOverflow 会让您在几天后接受自己的答案。)
标签: sql-server-2008-r2 global-variables where-clause case-when