【问题标题】:Stored Procedure with date parameters带有日期参数的存储过程
【发布时间】:2013-03-17 06:44:30
【问题描述】:

我正在尝试创建一个在执行时具有日期参数的存储过程。我希望能够搜索在特定日期之间发货的订单。我有这个:

create procedure sp_orders_by_dates
        @startdate smalldatetime,
        @enddate smalldatetime
as
select  OrderID,
        o.CustomerID,

        c.CompanyName as CustomerCompany,
        s.ShipperID,
        s.CompanyName as ShipperCompany,
        ShippedDate

from    Orders o join Customers c
on      o.CustomerID = c.CustomerID join Shippers s
on      s.ShipperID = o.ShipperID
where @startdate = ShippedDate,
        @enddate = ShippedDate
order by ShippedDate

要执行,我必须这样做:

EXEC sp_orders_by_dates '1991-07-01', '1991-08-31'

我知道这部分出了什么问题,但我就是不知道如何在这里制作“介于”声明:

where @startdate = ShippedDate,
        @enddate = ShippedDate

【问题讨论】:

标签: date stored-procedures parameters


【解决方案1】:
where ShippedDate BETWEEN @startdate and @enddate

【讨论】:

    【解决方案2】:

    这是 C# 中的抓取:

    DataTable t = new DataTable();
    //set up your connectionString beforhand
    using(SqlConnection cn = new SqlConnection(conn))
            {
                //and isolating the work from everything else
                try
                {
                    //configure the query apparatus, using the stored procedure
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "<StoredProcName>";
    
                    //set up the parameters
                    SqlParameter prmFrom = cmd.CreateParameter();
                    prmFrom.Direction = ParameterDirection.Input;
                    prmFrom.ParameterName = "@FromDate";
                    prmFrom.IsNullable = true;
                    SqlParameter prmTo = cmd.CreateParameter();
                    prmTo.Direction = ParameterDirection.Input;
                    prmTo.ParameterName = "@ToDate";
                    prmTo.IsNullable = true;
    
                    prmFrom.DbType = DbType.DateTime;
                    prmFrom.SqlValue = from;
    
                    prmTo.DbType = DbType.DateTime;
                    prmTo.SqlValue = to;
    
                    //make sure the command and the params go together from the app
                    cmd.Parameters.Add(prmFrom);
                    cmd.Parameters.Add(prmTo);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                    //finally, fill the table so you can pass it back to the app
                    da.Fill(t);
                }
                catch(Exception ex)
                {
                   //error handling goes here
                }
            }
    

    【讨论】:

      【解决方案3】:

      “from”和“to”是调用此函数时需要创建为输入的值。 上面的 c# 代码中没有显示函数的标头,应该如下所示: public HttpResponseMessage Get(string from, string to)

      【讨论】:

      • 请检查您是否将此作为预期问题的答案发布 - 我,一方面,不要建立联系。 (你指的是什么c# code?)
      猜你喜欢
      • 1970-01-01
      • 2013-06-24
      • 2020-11-11
      • 2016-05-13
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2011-11-21
      • 2018-01-27
      相关资源
      最近更新 更多