返回日期之间的所有列,而没有来自另一列的特定值

时间:2016-06-02 16:07:58

标签: sql-server sql-server-2008 tsql sql-server-2012 sql-server-2008-r2

我编写了以下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

1 个答案:

答案 0 :(得分:1)

在回答问题时从注释中移出 - 如果您不想总是指定@InstrumentID,那么您可以在顶部将其设置为null并修改where语句以读取(@InstrumentID is null or x.InstrumentID = @InstrumentID)

的位置