3

実行時に日付パラメータを持つストアドプロシージャを作成しようとしています。特定の日付の間に発送された注文を検索できるようにしたい。私はこれを持っています:

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'

私はこの部分が間違っていることを知っていますが、ここで「between」ステートメントを作成する方法を理解できません。

where @startdate = ShippedDate,
        @enddate = ShippedDate
4

3 に答える 3

9
where ShippedDate BETWEEN @startdate and @enddate
于 2013-03-17T06:48:33.973 に答える
3

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
            }
        }
于 2014-01-10T00:05:38.893 に答える