Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-02-18 : 21:12:28
|
| David writes "What I'd like to do is give the user the capability of choosing records using multiple and's and or's basically something like thisselect * from table where (name1= tbl.name1 and unit1= tbl.unit1) OR (name2 = tbl.name2 and unit1= tbl.unit1) OR ...etchow can i do this?" |
|
|
JamesH
Posting Yak Master
149 Posts |
Posted - 2002-02-19 : 12:34:12
|
| Here's an example of what I think you're trying to do. It can get very complicated and I would suggest possibly having your app use seperate procs depending on the application and requirements instead of creating a monster proc like this. create proc sp_GetSalesCheck( @ReportType varchar(20) = 'Bookings',-- Report Time Frame Parameters (default to fiscal 2001) @ReportBeginDate varchar(8) = '20000326', @ReportEndDate varchar(8) = '20010331', @Period01 varchar(6) = '200101', @Period02 varchar(6) = '200102', @Period03 varchar(6) = '200103', @Period04 varchar(6) = '200104', @Period05 varchar(6) = '200105', @Period06 varchar(6) = '200106', @Period07 varchar(6) = '200107', @Period08 varchar(6) = '200108', @Period09 varchar(6) = '200109', @Period10 varchar(6) = '200110', @Period11 varchar(6) = '200111', @Period12 varchar(6) = '200112',-- Material Master Fields @vcMaterial varchar(25)= '', @vcMaterialFamily varchar(50)= '', @vcDivisionDescription varchar(50)= '', @vcProductLine varchar(25)= '', @vcProductManager varchar(50)= '', @vcMaterialPlant varchar(25)= '', @vcMaterialGroupDescription varchar(50)= '', @vcMaterialType varchar(25)= '', @vcMaterialTechnology varchar(50)= '', @vcMaterialCatalogCatagory varchar(50)= '', @vcMaterialPackageType varchar(25)= '', @vcPlannerName varchar(50)= '', @vcApplicationDescription varchar(50)= '', @vcApplicationEngineer varchar(50)= '', @vcProductEngineer varchar(50)= '', @vcMarketingManager varchar(50)= '', @vcCustomerQualityEngineer varchar(50)= '',-- Customer Master fields @vcCustomerID varchar(15)= '', @vcCustomerDescription varchar(50)= '', @vcForecastParentID varchar(50)= '', @vcForecastParentDescription varchar(50)= '', @vcCustomerHeirarchyID varchar(50)= '', @vcCustomerHeirarchyDescription varchar(50)= '', @vcSalesDivision varchar(50)= '', @vcSalesDistrictDescription varchar(50)= '', @vcSalesOfficeDescription varchar(50)= '', @vcSalesTerritoryDescription varchar(50)= '', @vcCustomerState varchar(50)= '', @vcCustomerCountry varchar(50)= '', @vcCustomerCity varchar(50)= '', @vcCustomerPostalCode varchar(50)= '', @vcDistributionChannel varchar(50)= '', @vcSalesRepFirm varchar(50)= '', @vcAccountGroup varchar(50)= '', -- Filters @vcFilters varchar(1000)='' ) ASdeclare @sql varchar(8000) set nocount onset @sql= ' SELECT ''SalesQuantity'' = sum(case when S.vcType = ''Shipments'' then S.iQuantity else 0 end), ''BookQuantity'' = sum(case when S.vcType = ''Bookings'' then S.iQuantity else 0 end), ''BLCRDQuantity'' = sum(case when S.vcType = ''BLCRD'' then S.iQuantity else 0 end), ''BLFCDQuantity'' = sum(case when S.vcType = ''BLFCD'' then S.iQuantity else 0 end), ''IHCRDQuantity'' = sum(case when S.vcType = ''IHCRD'' then S.iQuantity else 0 end), ''IHFCDQuantity'' = sum(case when S.vcType = ''IHFCD'' then S.iQuantity else 0 end)FROM tActuals S (nolock) left outer join tCustomerMaster C (nolock) on S.vcCustKey = C.vcCustKey left outer join tMaterialMaster M (nolock) on S.vcMaterial = M.vcMaterial left outer join tFiscalCalendar F (nolock) on S.CreateDate = F.vcDateWHERE S.CreateDate >= ''' + @ReportBeginDate + ''' and S.CreateDate <= ''' + @ReportEndDate + ''''if @vcMaterial <> '' begin set @sql = @sql + ' and M.vcMaterial = ''' + @vcMaterial + '''' endif @vcMaterialFamily <> '' begin set @sql = @sql + ' and M.vcMaterialFamily = ''' + @vcMaterialFamily + '''' endif @vcDivisionDescription <> '' begin set @sql = @sql + ' and M.vcDivisionDescription = ''' + @vcDivisionDescription + '''' endif @vcProductLine <> '' begin set @sql = @sql + ' and M.vcProductLine = ''' + @vcProductLine + '''' endif @vcProductManager <> '' begin set @sql = @sql + ' and M.vcProductManager = ''' + @vcProductManager + '''' endif @vcMaterialPlant <> '' begin set @sql = @sql + ' and M.vcMaterialPlant = ''' + @vcMaterialPlant + '''' endif @vcMaterialGroupDescription <> '' begin set @sql = @sql + ' and M.vcMaterialGroupDescription = ''' + @vcMaterialGroupDescription + '''' endif @vcMaterialType <> '' begin set @sql = @sql + ' and M.vcMaterialType = ''' + @vcMaterialType + '''' endif @vcMaterialTechnology <> '' begin set @sql = @sql + ' and M.vcMaterialTechnology = ''' + @vcMaterialTechnology + '''' endif @vcMaterialCatalogCatagory <> '' begin set @sql = @sql + ' and M.vcMaterialCatalogCatagory = ''' + @vcMaterialCatalogCatagory + '''' endif @vcMaterialPackageType <> '' begin set @sql = @sql + ' and M.vcMaterialPackageType = ''' + @vcMaterialPackageType + '''' endif @vcPlannerName <> '' begin set @sql = @sql + ' and M.vcPlannerName = ''' + @vcPlannerName + '''' endif @vcApplicationDescription <> '' begin set @sql = @sql + ' and M.vcApplicationDescription = ''' + @vcApplicationDescription + '''' endif @vcApplicationEngineer <> '' begin set @sql = @sql + ' and M.vcApplicationEngineer = ''' + @vcApplicationEngineer + '''' endif @vcProductEngineer <> '' begin set @sql = @sql + ' and M.vcProductEngineer = ''' + @vcProductEngineer + '''' endif @vcMarketingManager <> '' begin set @sql = @sql + ' and M.vcMarketingManager = ''' + @vcMarketingManager + '''' endif @vcCustomerQualityEngineer <> '' begin set @sql = @sql + ' and M.vcCustomerQualityEngineer = ''' + @vcCustomerQualityEngineer + '''' end-- **** Customer Master Filters ****if @vcCustomerID <> '' begin set @sql = @sql + ' and C.vcCustomerID = ''' + @vcCustomerID + '''' endif @vcCustomerDescription <> '' begin set @sql = @sql + ' and C.vcCustomerDescription = ''' + @vcCustomerDescription + '''' end if @vcForecastParentID <> '' begin set @sql = @sql + ' and C.vcForecastParentID = ''' + @vcForecastParentID + '''' endif @vcForecastParentDescription <> '' begin set @sql = @sql + ' and C.vcForecastParentDescription = ''' + @vcForecastParentDescription + '''' endif @vcCustomerHeirarchyID <> '' begin set @sql = @sql + ' and C.vcCustomerHeirarchyID = ''' + @vcCustomerHeirarchyID + '''' endif @vcCustomerHeirarchyDescription <> '' begin set @sql = @sql + ' and C.vcCustomerHeirarchyDescription = ''' + @vcCustomerHeirarchyDescription + '''' endif @vcSalesDivision <> '' begin set @sql = @sql + ' and C.vcSalesDivision = ''' + @vcSalesDivision + '''' endif @vcSalesDistrictDescription <> '' begin set @sql = @sql + ' and C.vcSalesDistrictDescription = ''' + @vcSalesDistrictDescription + '''' endif @vcSalesOfficeDescription <> '' begin set @sql = @sql + ' and C.vcSalesOfficeDescription = ''' + @vcSalesOfficeDescription + '''' endif @vcSalesTerritoryDescription <> '' begin set @sql = @sql + ' and C.vcSalesTerritoryDescription = ''' + @vcSalesTerritoryDescription + '''' endif @vcCustomerState <> '' begin set @sql = @sql + ' and C.vcCustomerState = ''' + @vcCustomerState + '''' endif @vcCustomerCountry <> '' begin set @sql = @sql + ' and C.vcCustomerCountry = ''' + @vcCustomerCountry + '''' endif @vcCustomerCity <> '' begin set @sql = @sql + ' and C.vcCustomerCity = ''' + @vcCustomerCity + '''' endif @vcCustomerPostalCode <> '' begin set @sql = @sql + ' and C.vcCustomerPostalCode = ''' + @vcCustomerPostalCode + '''' endif @vcDistributionChannel <> '' begin set @sql = @sql + ' and C.vcDistributionChannel = ''' + @vcDistributionChannel + '''' endif @vcSalesRepFirm <> '' begin set @sql = @sql + ' and C.vcSalesRepFirm = ''' + @vcSalesRepFirm + '''' endif @vcAccountGroup <> '' begin set @sql = @sql + ' and C.vcAccountGroup = ''' + @vcAccountGroup + '''' end--*********************** Filters *******************if @vcFilters <> '' begin set @sql = @sql + ' and ' + @vcFilters endexec (@sql)--select left(@sql,256),right(@sql,len(@SQL)-256)GOJamesH |
 |
|
|
nizmaylo
Constraint Violating Yak Guru
258 Posts |
Posted - 2002-02-19 : 14:03:18
|
| If you don't need to perform a pattern search with LIKE,you may try using COALESCE to avoid building dynamic SQL.something like thisSELECT *FROM tblTestWHERE field1=COALESCE(@field1, field1)AND field2=COALESCE(@field2, field2)Try searching developers forum on COALESCE or robvolk - it has a few good examples of how dynamic SQL or COALESCE work.helena |
 |
|
|
|
|
|
|
|