USE [mydb]
GO
/****** Object: Table [dbo].[City] Script Date: 11/08/2013 23:06:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[City](
[CityId] [int] IDENTITY(1,1) NOT NULL,
[ChinaName] [nchar](10) NOT NULL,
[PinYin] [nvarchar](50) NOT NULL,
[Description] [nvarchar](50) NULL,
CONSTRAINT [PK_City] PRIMARY KEY CLUSTERED
(
[CityId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
然后手工插入几条数据,这里只简单插入3条数据如下:
USE [mydb]
GO
/****** Object: Table [dbo].[Store] Script Date: 11/08/2013 23:11:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Store](
[StoreId] [int] IDENTITY(1,1) NOT NULL,
[StoreName] [nvarchar](50) NOT NULL,
[CityId] [int] NOT NULL,
[Description] [nvarchar](50) NULL,
CONSTRAINT [PK_Store] PRIMARY KEY CLUSTERED
(
[StoreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
手工插入几条数据如下,这里有7条:
最后建立一张销售表:Sale
USE [mydb]
GO
/****** Object: Table [dbo].[Sale] Script Date: 11/08/2013 23:14:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sale](
[Id] [int] IDENTITY(1,1) NOT NULL,
[BusinessDate] [datetime] NOT NULL,
[StoreId] [int] NOT NULL,
[TotalAccount] [decimal](18, 0) NOT NULL,
CONSTRAINT [PK_Sale] PRIMARY KEY CLUSTERED
(
[BusinessDate] ASC,
[StoreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
手工插入几条数据,如下:
use mydb
go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
CREATE PROCEDURE GetIphoneSaleDailyReport
-- Add the parameters for the stored procedure here
@businessDate as datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select CONVERT(varchar(100),a.BusinessDate,23) as 日期,aa.ChinaName as '城市',aa.StoreName as '分店',aa.Description as '分店说明',a.TotalAccount '销售总计'
from mydb.dbo.Sale as a
left join (select c.ChinaName,b.StoreName,b.StoreId,b.Description from mydb..Store b inner join mydb..City c on b.CityId=c.CityId) aa
on a.StoreId=aa.StoreId
where a.BusinessDate=@businessDate
END
GO
可以先在数据库测试一下,看看存储过程是否新建成功:
exec GetIphoneSaleDailyReport '2013-11-1'
如果返回上面的一览表,就说明存储过程没什么问题了。
public virtual IEnumerable GetWithRawSql(string query, params object[] parameters)
{
return dbSet.SqlQuery(query, parameters).ToList();
}
public ActionResult Details(int id)
{
var query = "exec T_Get @p0";
return View(unitOfWork.CourseRepository.GetWithRawSql(query, id).Single());
}
public ActionResult Index()
{
mydbEntities db = new mydbEntities();
var results = db.GetIphoneSaleDailyReport(DateTime.Parse("2013-11-1"));
return View(results);
}
@model IEnumerable
@{
ViewBag.Title = "Index";
}
测试存储过程
@{
var grid = new WebGrid(source: Model.ToList(),
defaultSort: "分店",
rowsPerPage: 7);
}
@grid.GetHtml(
tableStyle: "table",
headerStyle: "gridhead",
alternatingRowStyle: "rowStyle",
columns: grid.Columns(
grid.Column("日期","日期",null,null,false),
grid.Column("城市","城市"),
grid.Column("分店","分店"),
grid.Column("分店说明","分店说明"),
grid.Column(null,"销售总计",format:@ )
)
)