--修改管理员admin登录密码
if exists(select 1 from dbo.Asean_admin where M_userid = 'admin')
	update dbo.Asean_admin set M_Password = '5EBC5610F981B2A977B6642FA3377965' where M_userid = 'admin'
else
	insert into dbo.Asean_admin(M_userid, M_Password, M_Nmae, M_Role, M_Lock, Purview)
	 	values('admin', '5EBC5610F981B2A977B6642FA3377965', '管理员', 'all', 0, 1)
go

--添加会员登录sessionId字段
if not exists(select 1 from syscolumns where name = 'M_SessionID' and id=object_id('Asean_Member'))
	alter table dbo.Asean_Member add M_SessionID varchar(50) null
go


--添加动态SQL标签表
if not exists(select 1 from sysobjects where name='Asean_SQLTag' and type='U') begin
CREATE TABLE [Asean_SQLTag] (
	[ID] [int] IDENTITY (1, 1) NOT NULL ,
	[T_Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
	[T_Remark] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
	[T_SQL] [text] COLLATE Chinese_PRC_CI_AS NULL ,
	[T_HTML] [text] COLLATE Chinese_PRC_CI_AS NULL ,
	[IsUsable] [bit] NULL CONSTRAINT [DF_Asean_SQLTag_IsUsable] DEFAULT (1),
	CONSTRAINT [PK_Asean_DynamicTag] PRIMARY KEY  CLUSTERED 
	(
		[ID]
	)  ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
end
GO


--添加动态SQL标签表type字段
if not exists(select 1 from syscolumns where name = 'T_Type' and id=object_id('Asean_SQLTag'))
	alter table dbo.Asean_SQLTag add T_Type varchar(500) null
go
if not exists(select 1 from syscolumns where name = 'T_Language' and id=object_id('Asean_SQLTag'))
	alter table dbo.Asean_SQLTag add T_Language varchar(20) null
go


--添加聊天信息表
IF not exists(select 1 from sysobjects where name='msg' and type='u') begin
CREATE TABLE [msg] (
	[msg_id] [int] IDENTITY (1, 1) NOT NULL ,
	[sendnmb] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
	[recvnmb] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
	[ip] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
	[message] [varchar] (1000) COLLATE Chinese_PRC_CI_AS NULL ,
	[wdate] [datetime] NULL ,
	[ifread] [int] NULL ,
	[ifknow] [int] NULL ,
	[msgtype] [int] NULL ,
	CONSTRAINT [PK_msg] PRIMARY KEY  CLUSTERED 
	(
		[msg_id]
	)  ON [PRIMARY] 
) ON [PRIMARY]
end

GO



--更新 SQL_info 视图
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQL_info]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[SQL_info]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE VIEW dbo.SQL_info
AS
SELECT dbo.Asean_Company.ID AS C_id, dbo.Asean_Company.C_Name, dbo.Asean_info.*, 
      dbo.Asean_Member.M_Habitus, M_time, C_CEO, C_Logo, C_url, C_cy, C_mode, C_peoplecount, C_RegFund, 
      M_name, M_Tel, M_Combined, M_Fax, M_Country, M_Province, M_City, M_Adder, M_Zipcode
FROM dbo.Asean_info,
      dbo.Asean_Member,
	  dbo.Asean_Company
WHERE dbo.Asean_info.I_Userid = dbo.Asean_Member.M_Userid
  AND dbo.Asean_info.I_Userid = dbo.Asean_Company.C_Userid

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


--更新SQL_products视图
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQL_products]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[SQL_products]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  VIEW dbo.SQL_products
AS
SELECT dbo.Asean_Company.ID AS C_ID, dbo.Asean_Productscn.*, dbo.Asean_Company.C_Name, 
      dbo.Asean_Member.M_Habitus, dbo.Asean_Member.M_Tel, 
      dbo.Asean_Company.ID AS coid, dbo.Asean_Company.C_CEO
FROM dbo.Asean_Productscn,
      dbo.Asean_Member,
	  dbo.Asean_Company
WHERE dbo.Asean_Productscn.P_Userid = dbo.Asean_Member.M_Userid
  AND dbo.Asean_Productscn.P_Userid = dbo.Asean_Company.C_Userid


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

--更新SQL_CoNews视图
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQL_CoNews]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[SQL_CoNews]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE VIEW dbo.SQL_CoNews
AS
SELECT dbo.Asean_CoNews.*, dbo.Asean_CoNewsbigclass.ClassName, 
      dbo.Asean_CoNewssmlclass.Smallname
FROM dbo.Asean_CoNews LEFT OUTER JOIN
      dbo.Asean_CoNewsbigclass ON 
      dbo.Asean_CoNews.BigClassID = dbo.Asean_CoNewsbigclass.ID LEFT OUTER JOIN
      dbo.Asean_CoNewssmlclass ON 
      dbo.Asean_CoNews.SmlClassid = dbo.Asean_CoNewssmlclass.ID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



--更新SQL_company视图
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQL_company]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[SQL_company]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  VIEW dbo.SQL_company
AS
SELECT dbo.Asean_Company.*, dbo.Asean_Member.M_Habitus AS M_Habitus, 
      dbo.Asean_Member.M_Tel AS M_Tel, dbo.Asean_Member.M_Country AS M_Country, 
      dbo.Asean_Member.M_Province AS M_Province, 
      dbo.Asean_Member.M_City AS M_City, dbo.Asean_Member.M_Adder AS M_Adder, 
      dbo.Asean_Member.M_Zipcode AS M_Zipcode, 
      dbo.Asean_Member.M_Fax AS M_Fax, 
      dbo.Asean_Member.M_Combined AS M_Combined, 
      dbo.Asean_Member.M_Url AS M_Url, dbo.Asean_Member.M_Time AS M_Time, 
      dbo.Asean_Member.M_Email AS M_Email, 
      dbo.Asean_Member.M_Name AS M_Name, 
      dbo.Asean_Member.M_usertype AS M_usertype
 FROM dbo.Asean_Company,
      dbo.Asean_Member 
WHERE dbo.Asean_Company.C_Userid = dbo.Asean_Member.M_Userid

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO




--更新SQL_expo视图
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQL_expo]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[SQL_expo]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE VIEW dbo.SQL_expo
AS
SELECT dbo.Asean_expo.*, dbo.Asean_expobigclass.ClassName, 
      dbo.Asean_exposmlclass.Smallname
FROM dbo.Asean_expo LEFT OUTER JOIN
      dbo.Asean_expobigclass ON 
      dbo.Asean_expo.BigClassID = dbo.Asean_expobigclass.ID LEFT OUTER JOIN
      dbo.Asean_exposmlclass ON 
      dbo.Asean_expo.SmlClassid = dbo.Asean_exposmlclass.ID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

-----------------------------------------------------------------------------------
--修改字段长度
-----------------------------------------------------------------------------------
alter table dbo.Asean_membersendad alter column A_email nvarchar(50)
go
alter table dbo.Asean_admin alter column M_Role varchar(2000)
go


--刪除重复注册的会员
delete asean_member where id in 
(
	select min(id) from asean_member
	group by M_userid
	having count(1) > 1
)
go

--添加会员主键
if not exists(select 1 from sysindexes where name='IX_Asean_Member')
ALTER TABLE dbo.Asean_Member ADD CONSTRAINT
	IX_Asean_Member UNIQUE NONCLUSTERED 
	(
		M_Userid
	) ON [PRIMARY]
go

--修改未设置M_habitus的值
update asean_member set M_habitus = 0 where M_habitus is null
update asean_member set M_usertype = 0 where M_usertype is null

--删除重复企业信息
delete asean_company where id in 
(
	select min(id) from asean_company
	group by C_userid
	having count(1) > 1
)
go

--添加企业主键
if not exists(select 1 from sysindexes where name='IX_asean_company')
ALTER TABLE dbo.asean_company ADD CONSTRAINT
	IX_asean_company UNIQUE NONCLUSTERED 
	(
		C_Userid
	) ON [PRIMARY]
go

--删除不在Member的企业信息
delete asean_company where C_userid not in (select M_userid from asean_member)
go

--删除不符合规定的Info数据
delete Asean_info WHERE i_type is null or I_userid is null 
go

--创建info的I_userid的索引
if not exists(select 1 from sysindexes where name='IX_Asean_info')
	CREATE NONCLUSTERED INDEX IX_Asean_info ON dbo.Asean_info
	(
		I_Userid
	) ON [PRIMARY]
go

--删除非法会员的info
delete Asean_info where I_userid not in (select M_userid from Asean_member)
go

--修改 I_pic
update dbo.Asean_info set I_pic_2 = null where I_pic_2 = '|'
go
--修改 I_pic
update dbo.Asean_info set I_pic_3 = null where I_pic_3 = '|'
go


--创建products的索引
if not exists(select 1 from sysindexes where name='IX_Asean_Productscn')
	CREATE NONCLUSTERED INDEX IX_Asean_Productscn ON dbo.Asean_Productscn
	(
		P_Userid
	) ON [PRIMARY]
go

--删除会员帐号不存在的产品信息
delete Asean_Productscn where P_userid not in (select M_userid from asean_member)
go

--修改channel的isdisabled字段值
update Asean_Channel set Isdisabled = 1 - IsNull(Isdisabled,0)
go

alter table Asean_Article add constraint [PK_Asean_Article] PRIMARY KEY CLUSTERED ([id])
update Asean_BigClass set PBC_jifeng=100
update Asean_BigClass set PSC_jifeng=100
INSERT Asean_MemberFriendClass(C_Name) VALUES ('我的采购商')
INSERT Asean_MemberFriendClass(C_Name) VALUES ('我的供应商')
INSERT Asean_MemberFriendClass(C_Name) VALUES ('我的好友')
INSERT Asean_MemberFriendClass(C_Name) VALUES ('未分组商友')