我正在制作SSRS报告,并允许用户输入关于加入两个表的决定。如果他们选择No(0)
,则运行此查询:
select distinct case when book.PROPCODE = '00' then 'R6' else book.PROPCODE end as PROPCODE,
BKNAME,
BKARRIVE,
BKDEPART,
BKRMNUM,
book.BOOKID
from NGFMBOOK book
inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,'/','0'),'-',0))) = ltrim(rtrim(str(book.BOOKID)))
where BKARRIVE between @StartDate and @EndDate
and book.PROPCODE in (@PropCode)
Order By BookID
如果他们选择Yes(1)
,则会在查询中添加其他联接:
select distinct case when book.PROPCODE = '00' then 'R6' else book.PROPCODE end as PROPCODE,
BKNAME,
BKARRIVE,
BKDEPART,
BKRMNUM,
book.BOOKID
from NGFMBOOK book
inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,'/','0'),'-',0))) = ltrim(rtrim(str(book.BOOKID)))
inner join [RPV1Data\RPV1Data].RP_V1Data.dbo.NGFUNCT func ON book.BOOKID = func.BOOKID --this is the extra join
where BKARRIVE between @StartDate and @EndDate
and book.PROPCODE in (@PropCode)
Order By BookID
有没有办法让Case语句或类似的东西,以便我不必有两个完全不同的查询?我正在尝试
set @queryHeader = 'select distinct case when book.PROPCODE = ''00'' then ''R6'' else book.PROPCODE end as PROPCODE, BKNAME, BKARRIVE, BKDEPART, BKRMNUM, book.BOOKID from NGFMBOOK book inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID)))'
set @option1 = 'inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID)))'
set @option2 = 'inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID))) inner join [RPV1Data\RPV1Data].RP_V1Data.dbo.NGFUNCT func ON book.BOOKID = func.BOOKID'
set @queryFooter = 'where BKARRIVE between @StartDate and @EndDate and book.PROPCODE in (@PropCode) Order By BookID'
set @query = @queryHeader + case when @SearchFun = 1 then @option2 else @option1 end + @queryFooter
exec(@query)
但是这给了我一个错误(语法不正确' BKARRIVE')
答案 0 :(得分:2)
让我们说用户选择Yes(1)/ No(0)的参数是@parameter。然后在你的第二个连接条件中添加@parameter = 1,这样当在@parameter中选择yes时,该连接将使用有效数据执行,否则它将会执行。我没有测试过查询,但逻辑上它应该适合你。
select distinct case when book.PROPCODE = '00' then 'R6' else book.PROPCODE end as PROPCODE,
BKNAME,
BKARRIVE,
BKDEPART,
BKRMNUM,
book.BOOKID
from NGFMBOOK book
inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,'/','0'),'-',0))) = ltrim(rtrim(str(book.BOOKID)))
left join [RPV1Data\RPV1Data].RP_V1Data.dbo.NGFUNCT func ON book.BOOKID = func.BOOKID AND @parameter = 1 --this is the extra join
where BKARRIVE between @StartDate and @EndDate
and book.PROPCODE in (@PropCode)
and ((func.BOOKID IS NOT NULL and @parameter = 1)
OR @parameter = 0)
Order By BookID
答案 1 :(得分:0)
在where子句
之前使用空格 set @queryFooter = ' where BKARRIVE between @StartDate and @EndDate and book.PROPCODE in (@PropCode) Order By BookID'
使用select @query在执行之前查看创建的语句
Declare @queryHeader nvarchar(1000),
@option1 nvarchar(1000),@option2 nvarchar(1000),@queryFooter nvarchar(500),@query nvarchar(max),@SearchFun int
set @SearchFun=1
set @queryHeader = 'select distinct case when book.PROPCODE = ''00'' then ''R6'' else book.PROPCODE end as PROPCODE, BKNAME, BKARRIVE, BKDEPART, BKRMNUM, book.BOOKID from NGFMBOOK book inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID)))'
set @option1 = 'inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID)))'
set @option2 = 'inner join NGFMBOOK2 on ltrim(rtrim(replace(replace(Uniqueid2,''/'',''0''),''-'',0))) = ltrim(rtrim(str(book.BOOKID))) inner join [RPV1Data\RPV1Data].RP_V1Data.dbo.NGFUNCT func ON book.BOOKID = func.BOOKID'
set @queryFooter = ' where BKARRIVE between @StartDate and @EndDate and book.PROPCODE in (@PropCode) Order By BookID'
set @query = @queryHeader + case when @SearchFun = 1 then @option2 else @option1 end + @queryFooter
select @query