SQL Server WHERE clause default to all unless variable entered -
i have table of 10 columns of data , i'd able make stored procedure pulls relevant data based on user-defined parameters. simplified version of fact.spend
table looks this:
location | year | spendyear ----------+--------+----------- new york | 2015 | 25.00 new york | 2016 | 23.20 dallas | 2015 | 29.30 dallas | 2016 | 25.32 san fran | 2015 | 23.33 san fran | 2016 | 23.97
a basic version of i'm trying is:
create procedure sppullspenddata (@location varchar(20), @spendyear smallint) select * fact.spend location = @location , spendyear = @spendyear
but i'd @spendyear
parameter optional, defaulting years if there no user input. tried few variations using subqueries, far nothing's worked out quite right.
select * fact.spend (location = @location) , (spendyear = @spendyear or @spendyear null)
wiki
Comments
Post a Comment