Вы находитесь на странице: 1из 19

62284 80082 24478 6313

CREATE TABLE [dbo].[NT_User](


[Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](12) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
[IsSuper] [int] NULL,
[RealName] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NULL,
[Tel] [nvarchar](50) NULL,
[Mobile] [nvarchar](50) NULL,
[Department] [nvarchar](50) NOT NULL,
[Authority] [nvarchar](1000) NOT NULL,
[Description] [nvarchar](50) NULL,
[CreatTime] [datetime] NOT NULL,
[UpdateTime] [datetime] NOT NULL,
[LoginTimes] [int] NULL,
[LastLoginDate] [datetime] NULL CONSTRAINT[DF_NT_User_LastLoginDate]
DEFAULT (getdate()),
CONSTRAINT [PK_NT_User] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Material](


[Id] [int] IDENTITY(1,1) NOT NULL,
[MaterialName] [nvarchar](50) NOT NULL,
[Type] [nvarchar](50) NOT NULL,
[Standard] [nvarchar](50) NULL,
[Properties] [nvarchar](200) NULL,
[Amount] [float] NOT NULL,
[AccountingUnit] [nvarchar](10) NOT NULL,
[LowLevel] [float] NULL,
[Description] [nvarchar](500) NULL,
[CreatTime] [datetime] NULL,
[UpdateTime] [datetime] NULL,
CONSTRAINT [PK_NT_Material] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Product](


[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductName] [nvarchar](50) NOT NULL,
[Type] [nvarchar](200) NOT NULL,
[Standard] [nvarchar](50) NULL,
[Amount] [float] NOT NULL,
[AccountingUnit] [nvarchar](10) NOT NULL,
[LowLevel] [float] NULL,
[Description] [nvarchar](200) NULL,
[CreatTime] [datetime] NULL,
[UpdateTime] [datetime] NULL,
CONSTRAINT [PK_NT_Product] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_EntryType](


[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductType] [int] NULL,
[EntryType] [int] NULL,
[EntryTitle] [nvarchar](50) NOT NULL
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Stat_Product](


[Id] [int] NOT NULL,
[ProductName] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[Standard] [nvarchar](50) NULL,
[Input] [float] NULL,
[Input_Produce] [float] NULL,
[Input_Return] [float] NULL,
[Input_Repair] [float] NULL,
[Input_Other] [float] NULL,
[Output] [float] NULL,
[Output_Send] [float] NULL,
[Output_Model] [float] NULL,
[Output_Other] [float] NULL
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Material_Entry](


[Id] [int] IDENTITY(1,1) NOT NULL,
[FormId] [nvarchar](50) NULL,
[MaterialId] [int] NOT NULL,
[Type] [int] NOT NULL,
[PartnerId] [int] NOT NULL,
[Amount] [float] NOT NULL,
[Balance] [float] NOT NULL,
[Executer] [nvarchar](50) NULL,
[ExecuteDate] [datetime] NULL,
[Description] [nvarchar](500) NULL,
[CreatTime] [datetime] NULL,
[UpdateTime] [datetime] NULL,
CONSTRAINT [PK_NT_Material_Entry] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Partner](


[Id] [int] IDENTITY(1,1) NOT NULL,
[PartnerName] [nvarchar](50) NOT NULL,
[Address] [nvarchar](200) NULL,
[Manager] [nvarchar](50) NULL,
[Tel] [nvarchar](50) NULL,
[Mobile] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
CONSTRAINT [PK_NT_Partner] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Product_Entry](


[Id] [int] IDENTITY(1,1) NOT NULL,
[FormId] [nvarchar](50) NULL,
[ProductId] [int] NOT NULL,
[Type] [int] NOT NULL,
[PartnerId] [int] NOT NULL,
[Amount] [float] NOT NULL,
[Balance] [float] NOT NULL,
[Executer] [nvarchar](50) NULL,
[ExecuteDate] [datetime] NULL,
[Description] [nvarchar](500) NULL,
[CreatTime] [datetime] NULL,
[UpdateTime] [datetime] NULL,
CONSTRAINT [PK_NT_Product_Entry] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Product_Material](


[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductId] [int] NOT NULL,
[MaterialId] [int] NOT NULL,
[MaterialValue] [float] NOT NULL,
CONSTRAINT [PK_NT_Product_Material] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
;

CREATE TABLE [dbo].[NT_Stat_Material](


[Id] [int] NULL,
[MaterialName] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[Standard] [nvarchar](50) NULL,
[Properties] [nvarchar](100) NULL,
[Input] [float] NULL,
[Output] [float] NULL,
[Output_Produce] [float] NULL,
[Output_Return] [float] NULL,
[Output_Other] [float] NULL
) ON [PRIMARY]
;
CREATE VIEW NT_View_Material_Entry
AS
SELECT Id, FormId,
(SELECT MaterialName FROM dbo.NT_Material WHERE (Id =
dbo.NT_Material_Entry.MaterialId)) AS MaterialName,
(SELECT EntryTitle FROM dbo.NT_EntryType WHERE (Id =
dbo.NT_Material_Entry.Type)) AS EntryTitle,
(CASE PartnerId WHEN 0 THEN '无' ELSE ISNULL ((SELECT PartnerName FROM
dbo.NT_Partner WHERE (Id = NT_Material_Entry.PartnerId)), '<font
color=red>记录已被删除</font>') END) AS PartnerName,
Amount, Balance, Executer, CONVERT(varchar(12), ExecuteDate, 23) AS
ExecuteDate, Description, CreatTime, UpdateTime
FROM dbo.NT_Material_Entry
;

CREATE VIEW NT_View_Product_Entry


AS
SELECT Id, FormId,
(SELECT ProductName FROM dbo.NT_Product WHERE (Id =
dbo.NT_Product_Entry.ProductId)) AS ProductName,
(SELECT EntryTitle FROM dbo.NT_EntryType WHERE (Id =
dbo.NT_Product_Entry.Type)) AS EntryTitle,
(CASE PartnerId WHEN 0 THEN '无' ELSE ISNULL ((SELECT PartnerName FROM
dbo.NT_Partner WHERE (Id = dbo.NT_Product_Entry.PartnerId)), '<font
color=red>记录已被删除</font>') END) AS PartnerName,
Amount, Balance, Executer, CONVERT(varchar(12), ExecuteDate, 23) AS
ExecuteDate, Description, CreatTime, UpdateTime FROM
dbo.NT_Product_Entry
;

//查找指定Id的用户
CREATE PROCEDURE NT_User_Password_Select
@Id int
as
select Password from NT_User where Id=@Id
;

CREATE PROCEDURE NT_User_Password_Update


@Id int,
@Password nvarchar(50)
as
update NT_User set Password=@Password where Id=@Id
;

CREATE PROCEDURE NT_User_ExistUserName


@UserName nvarchar(50)
as
select count(Id) from NT_User where UserName=@UserName
;

CREATE PROCEDURE NT_Material_SelectList


@Keyword nvarchar(20)
as
if @Keyword = ''
begin
Select Id, MaterialName,Type, Standard, Properties, Amount,
AccountingUnit, Description, CreatTime, UpdateTime From NT_Material
end
else
begin
Select Id, MaterialName,Type, Standard, Properties, Amount,
AccountingUnit, Description, CreatTime, UpdateTime From NT_Material
where (Id like '%'+@Keyword + '%') or (MaterialName like '%'+@Keyword +
'%') or (Type like '%'+@Keyword + '%') or (Standard like '%'+@Keyword +
'%') or (Properties like '%'+@Keyword + '%')
end
;

CREATE PROCEDURE NT_EntryType_List


@Type int
as
select Id, EntryTitle from NT_EntryType where ProductType = @Type order
by ProductType, EntryType desc, Id
;

CREATE PROCEDURE NT_EntryType_SelectList


@ProductType INT
AS
if @ProductType = 0
begin
select Id, ProductType, EntryType, EntryTitle from NT_EntryType order
by ProductType, EntryType desc, Id
end
else
begin
select Id, ProductType, EntryType, EntryTitle from NT_EntryType where
ProductType = @ProductType order by ProductType, EntryType desc, Id
end
;

CREATE PROCEDURE NT_EntryType_EntryTypeById


@Id INT
as
select EntryType from NT_EntryType where Id = @Id
;

CREATE PROCEDURE NT_EntryType_Insert


@ProductType int,
@EntryType int,
@EntryTitle nvarchar(50)
as
insert into NT_EntryType(ProductType, EntryType, EntryTitle)
values(@ProductType, @EntryType, @EntryTitle)
;

CREATE PROCEDURE NT_EntryType_Update


@Id INT,
@ProductType int,
@EntryType int,
@EntryTitle nvarchar(50)
as
update NT_EntryType set ProductType = @ProductType, EntryType =
@EntryType, EntryTitle = @EntryTitle where Id = @Id
;

CREATE PROCEDURE NT_EntryType_Delete


@Id int
as
if exists(select * from NT_Material_Entry where Type=@Id)
begin
return 0
end
if exists(select * from NT_Product_Entry where Type=@Id)
begin
return 0
end
delete NT_EntryType where Id = @Id
;

CREATE PROCEDURE NT_Material_Entry_Update


@Id int,
@FormId nvarchar(50),
@MaterialId int,
@Type int,
@PartnerId int,
@Amount float,
@Balance float,
@Executer nvarchar(20),
@ExecuteDate DateTime,
@Description nvarchar(500),
@CreatTime DateTime,
@UpdateTime DateTime
as
update NT_MaterialEntry set FormId=@FormId, MaterialId=@MaterialId,
Type=@Type, PartnerId=@PartnerId, Amount=@Amount, Balance=@Balance,
Executer=@Executer,
ExecuteDate=@ExecuteDate, Description=@Description,
CreatTime=@CreatTime, UpdateTime=@UpdateTime
;

CREATE PROCEDURE NT_EntryType_SelectTopOne


@Id INT
as
select top 1 Id, ProductType, EntryType, EntryTitle from NT_EntryType
where Id = @Id
;

CREATE PROCEDURE NT_Material_Entry_SelectList


@Keyword nvarchar(20)
as
select Id, FormId, MaterialName,EntryTitle,PartnerName,Amount, Balance,
Executer, ExecuteDate, Description, CreatTime, UpdateTime from
NT_View_Material_Entry
where (Id like '%'+@Keyword+'%') or (FormId like '%'+@Keyword+'%') or
(MaterialName like '%'+@Keyword+'%') or (EntryTitle like
'%'+@Keyword+'%') or (PartnerName like '%'+@Keyword+'%') or
(Executer like '%'+@Keyword+'%') or (ExecuteDate like '%'+@Keyword+'%')
;

CREATE PROCEDURE NT_Product_SelectList


@Keyword nvarchar(20)
as
if @Keyword = ''
begin
Select Id, ProductName,Type, Standard, Amount, AccountingUnit,
Description, CreatTime, UpdateTime From NT_Product
end
else
begin
Select Id, ProductName,Type, Standard, Amount, AccountingUnit,
Description, CreatTime, UpdateTime From NT_Product
where (Id like '%'+@Keyword + '%') or (ProductName like '%'+@Keyword +
'%') or (Type like '%'+@Keyword + '%' ) or (Standard like
'%'+@Keyword + '%')
end
;

CREATE PROCEDURE NT_Product_Entry_SelectList


@Keyword nvarchar(20)
as
select Id, FormId, ProductName,EntryTitle,PartnerName,Amount, Balance,
Executer, ExecuteDate, Description, CreatTime, UpdateTime from
NT_View_Product_Entry
where (Id like '%'+@Keyword+'%') or (FormId like '%'+@Keyword+'%') or
(ProductName like '%'+@Keyword+'%') or (EntryTitle like
'%'+@Keyword+'%') or (PartnerName like '%'+@Keyword+'%') or
(Executer like '%'+@Keyword+'%') or (ExecuteDate like '%'+@Keyword+'%')
;

Create Procedure NT_Material_Name_SelectList


as
select Id, MaterialName from NT_Material order by Id
;

CREATE PROCEDURE NT_Product_Name_SelectList


as
Select Id, ProductName From NT_Product order by Id
;

CREATE PROCEDURE NT_User_Delete


@Id int
as
delete NT_User where Id=@Id
;

CREATE PROCEDURE NT_User_SelectOne


@Id int
as
select Id, UserName, Password, IsSuper, RealName, Email, Tel, Mobile,
Department, Authority, Description, CreatTime, UpdateTime
from NT_User where Id=@Id
;

CREATE PROCEDURE NT_User_Authority_Select


@Id int
as
select Authority from NT_User where Id=@Id
;

CREATE PROCEDURE NT_User_Authority_Update


@Id int,
@Authority nvarchar(500)
as
update NT_User set Authority=@Authority where Id=@Id
;

CREATE PROCEDURE NT_User_RealName


@Id int
as
select RealName from NT_User where Id=@Id
;

CREATE PROCEDURE NT_User_Insert


@Id int,
@UserName nvarchar(20),
@Password nvarchar(20),
@IsSuper int,
@RealName nvarchar(20),
@Email nvarchar(20),
@Tel nvarchar(20),
@Mobile nvarchar(20),
@Department nvarchar(20),
@Description nvarchar(20),
@Authority varchar(200),
@CreatTime DateTime,
@UpdateTime DateTime,
@LoginTimes int,
@LastLoginDate DateTime
as
insert into NT_User(UserName, Password, IsSuper, RealName, Email, Tel,
Mobile, Department, Authority, Description, CreatTime, UpdateTime,
LoginTimes, LastLoginDate)
values(@UserName, @Password, 0, @RealName, @Email, @Tel, @Mobile,
@Department, '', @Description, @CreatTime, @UpdateTime, 1, getdate())
;

CREATE PROCEDURE NT_User_Update


@Id int,
@UserName nvarchar(20),
@Password nvarchar(20),
@IsSuper int,
@RealName nvarchar(20),
@Email nvarchar(20),
@Tel nvarchar(20),
@Mobile nvarchar(20),
@Department nvarchar(20),
@Description nvarchar(20),
@Authority varchar(200),
@CreatTime DateTime,
@UpdateTime DateTime,
@LoginTimes int,
@LastLoginDate DateTime
as
update NT_User set RealName=@RealName, Email=@Email, Tel=@Tel,
Mobile=@Mobile, Department=@Department, Description=@Description,
UpdateTime=@UpdateTime where Id=@Id
;

CREATE PROCEDURE NT_User_UserLoginSelect


@UserName nvarchar(50),
@Password nvarchar(50)
as
declare @num int
set @num = (select Id from NT_User where UserName=@UserName and
Password=@Password)
if @num is null
begin
select 0
end
else
begin
update NT_User set LoginTimes=LoginTimes+1, LastLoginDate=getdate()
where UserName = @UserName
select @num
end
;

CREATE PROCEDURE NT_Material_Delete


@Id int
as
delete NT_Material where Id=@Id
delete NT_Material_Entry where MaterialId=@Id
;

CREATE PROCEDURE [dbo].[NT_Material_SelectOne]


@Id int
AS
Select Id, MaterialName,Type, Standard, Properties, Amount,
AccountingUnit, LowLevel,Description, CreatTime, UpdateTime From
NT_Material where Id = @Id
;

CREATE PROCEDURE NT_Material_Update


@Id int,
@MaterialName nvarchar(50),
@Type nvarchar(20),
@Standard nvarchar(50),
@Properties nvarchar(200),
@Amount float,
@AccountingUnit nvarchar(10),
@LowLevel float,
@Description nvarchar(500),
@CreatTime datetime,
@UpdateTime datetime
as
update NT_Material set MaterialName = @MaterialName, Standard =
@Standard, Type = @Type, Properties = @Properties, AccountingUnit =
@AccountingUnit, LowLevel=@LowLevel, Description = @Description,
UpdateTime = @UpdateTime where Id = @Id
;

CREATE PROCEDURE NT_Material_Insert


@Id int,
@MaterialName nvarchar(50),
@Type nvarchar(20),
@Standard nvarchar(50),
@Properties nvarchar(200),
@Amount float,
@AccountingUnit nvarchar(10),
@LowLevel float,
@Description nvarchar(500),
@CreatTime datetime,
@UpdateTime datetime
as
insert into NT_Material(MaterialName, Type, Standard, Properties,
Amount, AccountingUnit, LowLevel, Description, CreatTime, UpdateTime)
values(@MaterialName, @Type, @Standard, @Properties, 0,
@AccountingUnit, @LowLevel, @Description, @CreatTime, @UpdateTime)
;

CREATE PROCEDURE NT_Stat_PrepareData


@BeginDate Nvarchar(20),
@EndDate Nvarchar(20)
as
delete NT_Stat_Material--删除旧记录

insert into NT_Stat_Material(Id, MaterialName, Type, Standard,


Properties, Input, Output, Output_Produce, Output_Return, Output_Other)
select Id, MaterialName, Type, Standard, Properties,
Input=(select IsNull(sum(Amount),0) from NT_Material_Entry where
MaterialId = M.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 1)),
Output=(select IsNull(sum(Amount),0) from NT_Material_Entry where
MaterialId = M.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type in (2,3,4))),
Output_Produce=(select IsNull(sum(Amount),0) from NT_Material_Entry
where MaterialId = M.Id and (ExecuteDate between @BeginDate and
@EndDate) and (Type = 2)),
Output_Return=(select IsNull(sum(Amount),0) from NT_Material_Entry where
MaterialId = M.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 3)),
Output_Other=(select IsNull(sum(Amount),0) from NT_Material_Entry where
MaterialId = M.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 4))
from NT_Material M

update NT_Stat_Material set


Output = Output * (-1),
Output_Produce = Output_Produce * (-1),
Output_Return = Output_Return * (-1),
Output_Other = Output_Other * (-1)
;

CREATE PROCEDURE NT_Stat_Material_Entry_List


@MaterialId int, --原材料编号, 0为不限
@BeginDate nvarchar(30), --统计的开始日期, 时分秒均为零
@EndDate nvarchar(30) --统计的结束日期
as

Create table NT_StatTable(Id int, [原材料名称] nvarchar(50))


if(@MaterialId<>0)
begin
insert into NT_StatTable(Id, [原材料名称]) select Id, MaterialName
from NT_Material where Id = @MaterialId
end
else
begin
insert into NT_StatTable(Id, [原材料名称]) select Id, MaterialName
from NT_Material
end
DECLARE EntryTitle_cursor scroll CURSOR FOR select Id,EntryTitle from
NT_EntryType where ProductType=1 order by ProductType, EntryType desc,
Id
open EntryTitle_cursor
declare @Id int
declare @EntryTitle nvarchar(50)
declare @sql nvarchar(1000)
FETCH Next FROM EntryTitle_cursor into @Id, @EntryTitle
set @sql = 'if not exists (select name from syscolumns where
id=object_id(''NT_StatTable'') and name = '''+@EntryTitle+''') ALTER
TABLE NT_StatTable ADD '+@EntryTitle+' float'
exec (@sql)
set @sql = 'update NT_StatTable set ['+@EntryTitle+'] = isnull((select
sum(Amount) from NT_Material_Entry E where E.MaterialId =
NT_StatTable.Id and (ExecuteDate between '''+@BeginDate+''' and
'''+@EndDate+''') and E.Type='+convert(varchar(10),@Id)+'),0)'
exec (@sql)
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM EntryTitle_cursor into @Id, @EntryTitle
set @sql = 'if not exists (select name from syscolumns where
id=object_id(''NT_StatTable'') and name = '''+@EntryTitle+''') ALTER
TABLE NT_StatTable ADD '+@EntryTitle+' float'
exec (@sql)
set @sql = 'update NT_StatTable set ['+@EntryTitle+'] = isnull((select
sum(Amount) from NT_Material_Entry E where E.MaterialId =
NT_StatTable.Id and (ExecuteDate between '''+@BeginDate+''' and
'''+@EndDate+''') and E.Type='+convert(varchar(10),@Id)+'),0)'
exec (@sql)
END
CLOSE EntryTitle_cursor
DEALLOCATE EntryTitle_cursor
ALTER TABLE NT_StatTable ADD [总收入] float
ALTER TABLE NT_StatTable ADD [总支出] float
update NT_StatTable set [总收入]=(select IsNull(sum(Amount),0) from
NT_Material_Entry where (ExecuteDate between @BeginDate and @EndDate)
and (MaterialId = NT_StatTable.Id) and (Type in (select Id from
NT_EntryType where ProductType=1 and EntryType=1)))
update NT_StatTable set [总支出]=(select IsNull(sum(Amount),0) from
NT_Material_Entry where (ExecuteDate between @BeginDate and @EndDate)
and (MaterialId = NT_StatTable.Id) and (Type in (select Id from
NT_EntryType where ProductType=1 and EntryType=-1)))
ALTER TABLE NT_StatTable DROP COLUMN Id
select * from NT_StatTable
drop table NT_StatTable
;

CREATE PROCEDURE NT_Stat_Product_Entry_List


@ProductId int, --成品编号, 0为不限
@BeginDate nvarchar(30), --统计的开始日期, 时分秒均为零
@EndDate nvarchar(30) --统计的结束日期
as
Create table NT_StatTable(Id int, [成品名称] nvarchar(50))
if(@ProductId<>0)
begin
insert into NT_StatTable(Id, [成品名称]) select Id, ProductName from
NT_Product where Id = @ProductId
end
else
begin
insert into NT_StatTable(Id, [成品名称]) select Id, ProductName from
NT_Product
end
DECLARE EntryTitle_cursor scroll CURSOR FOR select Id,EntryTitle from
NT_EntryType where ProductType=2 order by ProductType, EntryType desc,
Id
open EntryTitle_cursor
declare @Id int
declare @EntryTitle nvarchar(50)
declare @sql nvarchar(1000)
FETCH Next FROM EntryTitle_cursor into @Id, @EntryTitle
set @sql = 'if not exists (select name from syscolumns where
id=object_id(''NT_StatTable'') and name = '''+@EntryTitle+''') ALTER
TABLE NT_StatTable ADD '+@EntryTitle+' float'
exec (@sql)
set @sql = 'update NT_StatTable set ['+@EntryTitle+'] = isnull((select
sum(Amount) from NT_Product_Entry E where E.ProductId = NT_StatTable.Id
and (ExecuteDate between '''+@BeginDate+''' and '''+@EndDate+''') and
E.Type='+convert(varchar(10),@Id)+'),0)'
exec (@sql)
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM EntryTitle_cursor into @Id, @EntryTitle
set @sql = 'if not exists (select name from syscolumns where
id=object_id(''NT_StatTable'') and name = '''+@EntryTitle+''') ALTER
TABLE NT_StatTable ADD '+@EntryTitle+' float'
exec (@sql)
set @sql = 'update NT_StatTable set ['+@EntryTitle+'] = isnull((select
sum(Amount) from NT_Product_Entry E where E.ProductId = NT_StatTable.Id
and (ExecuteDate between '''+@BeginDate+''' and '''+@EndDate+''') and
E.Type='+convert(varchar(10),@Id)+'),0)'
exec (@sql)
END
CLOSE EntryTitle_cursor
DEALLOCATE EntryTitle_cursor
ALTER TABLE NT_StatTable ADD [总收入] float
ALTER TABLE NT_StatTable ADD [总支出] float
update NT_StatTable set [总收入]=(select IsNull(sum(Amount),0) from
NT_Product_Entry where (ExecuteDate between @BeginDate and @EndDate) and
(ProductId = NT_StatTable.Id) and (Type in (select Id from NT_EntryType
where ProductType=2 and EntryType=1)))
update NT_StatTable set [总支出]=(select IsNull(sum(Amount),0) from
NT_Product_Entry where (ExecuteDate between @BeginDate and @EndDate) and
(ProductId = NT_StatTable.Id) and (Type in (select Id from NT_EntryType
where ProductType=2 and EntryType=-1)))

ALTER TABLE NT_StatTable DROP COLUMN Id


select * from NT_StatTable
drop table NT_StatTable
;

CREATE PROCEDURE NT_Product_Insert


@Id int,
@ProductName nvarchar(50),
@Type nvarchar(20),
@Standard nvarchar(50),
@Amount float,
@AccountingUnit nvarchar(10),
@LowLevel float,
@Description nvarchar(500),
@CreatTime datetime,
@UpdateTime datetime
as
insert into NT_Product(ProductName, Type, Standard, Amount,
AccountingUnit, LowLevel, Description, CreatTime, UpdateTime)
values(@ProductName, @Type, @Standard, 0, @AccountingUnit, @LowLevel,
@Description, @CreatTime, @UpdateTime)
;

CREATE PROCEDURE NT_Product_SelectOne


@Id int
AS
Select Id, ProductName,Type, Standard, Amount, AccountingUnit,
LowLevel, Description, CreatTime, UpdateTime From NT_Product where Id =
@Id
;

CREATE PROCEDURE NT_Product_Update


@Id int,
@ProductName nvarchar(50),
@Type nvarchar(20),
@Standard nvarchar(50),
@Amount float,
@AccountingUnit nvarchar(10),
@LowLevel float,
@Description nvarchar(500),
@CreatTime datetime,
@UpdateTime datetime
as
update NT_Product set ProductName = @ProductName, Type = @Type,
Standard=@Standard, AccountingUnit = @AccountingUnit,
LowLevel=@LowLevel, Description = @Description, UpdateTime =
@UpdateTime where Id = @Id
;

CREATE PROCEDURE NT_Product_Delete


@Id int
as
delete NT_Product where Id=@Id
delete NT_Product_Entry where ProductId=@Id
delete NT_Product_Material where ProductId=@Id
;

CREATE PROCEDURE NT_Stat_Product_PrepareData


@BeginDate Nvarchar(20),
@EndDate Nvarchar(20)
as
delete NT_Stat_Product--删除旧记录

insert into NT_Stat_Product(Id, ProductName, Type, Standard, Input,


Input_Produce, Input_Return, Input_Repair, Input_Other,Output,
Output_Send, Output_Model, Output_Other)
select Id, ProductName, Type, Standard,
Input=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type in (5,6,7,8))),
Input_Produce=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 5)),
Input_Return=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 6)),
Input_Repair=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 7)),
Input_Other=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 8)),
Output=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type in (9,10,11))),
Output_Send=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 9)),
Output_Model=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 10)),
Output_Other=(select IsNull(sum(Amount),0) from NT_Product_Entry where
ProductId = P.Id and (ExecuteDate between @BeginDate and @EndDate) and
(Type = 11))
from NT_Product P
update NT_Stat_Product set
Output = Output * (-1),
Output_Send = Output_Send * (-1),
Output_Model = Output_Model * (-1),
Output_Other = Output_Other * (-1)
;

CREATE PROCEDURE NT_Material_Entry_Insert


@Id int,
@FormId nvarchar(50),
@MaterialId int,
@Type int,
@PartnerId int,
@Amount float,
@Balance float,
@Executer nvarchar(20),
@ExecuteDate DateTime,
@Description nvarchar(500),
@CreatTime DateTime,
@UpdateTime DateTime

as
declare @OldBalance float
declare @NewBalance float
set @OldBalance = (select Amount from NT_Material where Id =
@MaterialId)
set @NewBalance = @OldBalance + @Amount
insert into NT_Material_Entry(FormId, MaterialId, Type, PartnerId,
Amount, Balance, Executer, ExecuteDate, Description, CreatTime,
UpdateTime)
values(@FormId, @MaterialId, @Type, @PartnerId, @Amount, @NewBalance,
@Executer, @ExecuteDate, @Description, @CreatTime, @UpdateTime)

update NT_Material set Amount = @NewBalance where Id = @MaterialId


;

Create Procedure NT_stat_Material_ById


@MaterialId int, --原材料编号, 0为不限
@BeginDate datetime, --统计的开始日期, 时分秒均为零
@EndDate datetime --统计的结束日期
as
declare @Input float --入库
declare @Output float --支出
declare @Output_Return float --退货
declare @Output_Send float --出货
declare @Output_Model float --做样
declare @Output_XiuBan float --修敢

set @Input = (select sum(Amount) from NT_Material_Entry where


(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type = 1))
set @Output = (select sum(Amount) from NT_Material_Entry where
(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type in (3,4,5,6)))
set @Output_Return = (select sum(Amount) from NT_Material_Entry where
(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type = 3))
set @Output_Send = (select sum(Amount) from NT_Material_Entry where
(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type = 4))
set @Output_Model = (select sum(Amount) from NT_Material_Entry where
(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type = 5))
set @Output_XiuBan = (select sum(Amount) from NT_Material_Entry where
(ExecuteDate between @BeginDate and @EndDate) and (MaterialId =
@MaterialId) and (Type = 6))
--编号,原材料名称,规格,入库量,退货量,出货量,做样量,修板量
select Id, MaterialName, Type, @Input, @Output, @Output_Return,
@Output_Send, @Output_Model, @Output_XiuBan from NT_Material where Id =
@MaterialId
;

CREATE PROCEDURE NT_BasicData


as
declare @num int
declare @StatTable table(Title nvarchar(50), Number nvarchar(200))
insert into @StatTable(Title, Number)
select '原材料', count(Id) as Number from NT_Material
insert into @StatTable(Title, Number)
select '成品', count(Id) as Number from NT_Product
insert into @StatTable(Title, Number)
select '用户', count(Id) as Number from NT_User
insert into @StatTable(Title, Number)
select '供应商', count(Id) as Number from NT_Partner
insert into @StatTable(Title, Number)
select '原材料出入库记录数', count(Id) as Number from NT_Material_Entry
insert into @StatTable(Title, Number)
select '成品出入库记录数', count(Id) as Number from NT_Product_Entry
insert into @StatTable(Title, Number)
select TOP 1 '原材料出入库最近操作时间' AS Title,
Convert(varchar(12),ExecuteDate,23 ) as Number from NT_Material_Entry
order by CreatTime desc
insert into @StatTable(Title, Number)
select TOP 1 '成品出入库最近操作时间' AS Title,
Convert(varchar(12),ExecuteDate,23 ) as Number from NT_Product_Entry
order by CreatTime desc
insert into @StatTable(Title, Number)
select TOP 1 '最后登录用户' AS Title, UserName +
'['+Convert(varchar(12),LastLoginDate,23 )+']' as Number from NT_User
order by LastLoginDate desc
select Title, Number from @StatTable
;

CREATE PROCEDURE NT_Partner_Insert


@Id int,
@PartnerName nvarchar(50),
@Address nvarchar(100),
@Manager nvarchar(50),
@Tel nvarchar(50),
@Mobile nvarchar(50),
@Email nvarchar(50)
as
insert into NT_Partner(PartnerName, Address, Manager, Tel, Mobile,
Email)
values(@PartnerName, @Address, @Manager, @Tel, @Mobile, @Email)
;

CREATE PROCEDURE NT_Partner_Update


@Id int,
@PartnerName nvarchar(50),
@Address nvarchar(100),
@Manager nvarchar(50),
@Tel nvarchar(50),
@Mobile nvarchar(50),
@Email nvarchar(50)
as
update NT_Partner set PartnerName = @PartnerName, Address=@Address,
Manager=@Manager, Tel=@Tel, Mobile=@Mobile, Email=@Email where Id=@Id
;

CREATE PROCEDURE NT_Partner_SelectOne


@Id int
as
select Id, PartnerName, Address, Manager, Tel, Mobile, Email from
NT_Partner where Id=@Id
;

CREATE Procedure NT_Partner_Delete


@Id int
as
delete NT_Partner where Id=@Id
;

CREATE PROCEDURE NT_Partner_SelectList


as
select Id, PartnerName from NT_Partner
;

CREATE PROCEDURE NT_Product_Entry_Insert


@Id int,
@FormId nvarchar(50),
@ProductId int,
@Type int,
@PartnerId int,
@Amount float,
@Balance float,
@Executer nvarchar(20),
@ExecuteDate DateTime,
@Description nvarchar(500),
@CreatTime DateTime,
@UpdateTime DateTime
as
declare @OldBalance float
declare @NewBalance float
set @OldBalance = (select Amount from NT_Product where Id = @ProductId)
set @NewBalance = @OldBalance + @Amount
insert into NT_Product_Entry(FormId, ProductId, Type, PartnerId,
Amount, Balance, Executer, ExecuteDate, Description, CreatTime,
UpdateTime)
values(@FormId, @ProductId, @Type, @PartnerId, @Amount, @NewBalance,
@Executer, @ExecuteDate, @Description, @CreatTime, @UpdateTime)

update NT_Product set Amount = @NewBalance where Id = @ProductId


;

CREATE PROCEDURE NT_Product_Material_Insert


@Id int,
@ProductId int,
@MaterialId int,
@MaterialValue float
as
insert into NT_Product_Material(ProductId, MaterialId, MaterialValue)
values(@ProductId, @MaterialId, @MaterialValue)
;

CREATE PROCEDURE NT_Product_Material_SelectList


@ProductId int
as
select pm.Id, ProductId,
ProductName=(select ProductName from NT_Product where Id = ProductId),
MaterialId, MaterialName, m.Type, Properties, AccountingUnit,
MaterialValue from NT_Product_Material pm, NT_Material m where
pm.MaterialId=m.Id and ProductId=@ProductId
;

CREATE PROCEDURE NT_Product_Material_Delete


@Id int
as
delete NT_Product_Material where Id = @Id
;

CREATE PROCEDURE NT_User__Function_SelectList


as
select Id, FunctionName, ParentId, OrderId from NT_Function order by
OrderId
;

CREATE PROCEDURE NT_User__Function_Parent_SelectList


as
select Id, FunctionName, ParentId, OrderId from NT_Function where
ParentId = 0 order by OrderId
;

CREATE PROCEDURE NT_User_Function_Insert


@Id int,
@FunctionName nvarchar(50),
@ParentId int,
@OrderId int
as
insert into NT_Function(FunctionName, ParentId, OrderId)
values(@FunctionName, @ParentId, @OrderId)
;

CREATE PROCEDURE NT_User__Function_Child_SelectList


@Id int
as
select Id, FunctionName, ParentId from NT_Function where ParentId = @Id
;

CREATE Procedure NT_BackUpDB


as
declare @filePath nvarchar(100)
set @filePath = 'DefaultDir'
declare @datanvarchar(50)
set @data = @filePath + convert(varchar(10),getdate(),112) + '.bak'
BACKUP DATABASE DefaultDataBase TO DISK = @data with init
;

用于向用户表中中添加一个新的管理员
修改Admin表中指定adminId的记录
删除指定adminId的记录

根据adminId判断数据是否存在
获得指定adminId的管理员详细信息

判断指定ID的管理员角色是否存在
添加一条新的管理员角色记录

修改指定ID的管理员角色记录
删除指定ID的管理员角色记录
根据Id获得管理员角色的详细信息
查询AdminRole表中的所有记录
用于判断指定文档的收藏记录是否存在
添加一条收藏记录