本文介绍了如何在sql中添加临时列而不创建过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为tblDailyMaintenceDetails的表

查询如下:

I have created a table named tblDailyMaintenceDetails
The query is as follows :

select * from tblDailyMaintenceDetails  where MaintenceDate between '28-Dec-2008'and '06-Apr-2013'



是否有可能在我提到的查询结果中显示''fromdate''和'todate''在哪里条件?

请尽早帮助我。


Is it possible to show the ''fromdate'' and ''todate'' into the query result that I mentioned in where condition?
Please help me as early as possible.

推荐答案

select *, CAST('28-Dec-2008' as DATE) as fromdate, CAST('06-Apr-2013' as DATE) as todate from tblDailyMaintenceDetails where MaintenceDate between '28-Dec-2008'and '06-Apr-2013'


using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT *, @STARTDATE AS StartDate. @ENDDATE AS EndDate FROM tblDailyMaintenceDetails WHERE MaintenceDate BETWEEN @STARTDATE AND @ENDDATE", con))
        {
        com.Parameters.AddWithValue("@STARTDATE", new DateTime(2008, 12, 28));
        com.Parameters.AddWithValue("@ENDDATE", new DateTime(2013, 4, 6));
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                ...
                }
            }
        }
    }

你不需要使用参数,它只是更容易在SQL的两个部分保持两个日期相同

You don''t need to use parameters, it just makes it easier to keep the two dates the same in both parts of the SQL


这篇关于如何在sql中添加临时列而不创建过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:27