我编写了以下SQL查询,我希望返回日期范围的所有值,而不必指定其他列值。现在,我在运行查询时只评论了2行。我不想每次输入的值是InstrumentID。鉴于以下查询,如何做到这一点最好?我在ms sql server 2012上运行它,我只能读取访问权限,因此我无法使用udf或存储过程。
Use MyDB
DECLARE @First VARCHAR(15), @Second VARCHAR(15),
@DateLow datetime, @DateHigh datetime,
@InstrumentID Varchar(15)
SET @First = '<InstrumentID>'
SET @Second = '</InstrumentID>'
SET @DateLow = '2016-06-02'
SET @DateHigh = '2016-06-03'
SET @InstrumentID = 'Enter InstrumentID Here'
select * from
(
SELECT out_interface_id, msg_id ,
SUBSTRING(xml_msg, CHARINDEX(@First, xml_msg) + LEN(@First),
CHARINDEX(@Second, xml_msg) -
CHARINDEX(@First, xml_msg) -
LEN(@First)) as InstrumentID,
msg_type, xml_msg, CAST(xml_msg AS XML) as [Quick_XML],
date_received,status, last_modified, environment,
transaction_closed_date
from mytable
where msg_type in ('ABC','XYZ')
and date_received >= @DateLow
and date_received < @DateHigh
) x
where x.InstrumentID = @InstrumentID
order by date_received desc
答案 0 :(得分:1)
在回答问题时从注释中移出 - 如果您不想总是指定@InstrumentID,那么您可以在顶部将其设置为null并修改where语句以读取(@InstrumentID is null or x.InstrumentID = @InstrumentID)