SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckMenuSetName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[CheckMenuSetName]
GO





CREATE PROCEDURE [dbo].[CheckMenuSetName]
@menuName varchar(100),
@menuSetId int
AS
	SET NOCOUNT ON
if (@menuSetId=0)
begin
	select pkId from menuset where [name]=@menuName
end
else
begin
	select pkId from menuset where [name]=@menuName and pkid<>@menuSetId
end





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[clockOneDay]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[clockOneDay]
GO






CREATE PROCEDURE [dbo].[clockOneDay]
AS
	/* SET NOCOUNT ON */
	update popwindow set popDay=popDay-1 where popDay>0
	update ad set dating=dating-1 where dating>0
	update board set showDay=showDay-1 where showDay>0
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteAdmin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteAdmin]
GO







CREATE PROCEDURE [dbo].[DeleteAdmin]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM Admin WHERE (pkId = @pkId)  






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteAdminAssignByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteAdminAssignByAdminId]
GO







CREATE PROCEDURE [dbo].[DeleteAdminAssignByAdminId]
	@pkId int
AS
	/* SET NOCOUNT ON */
	delete from AdminAssign
	where @pkId=adminId
	RETURN  
	 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteArtDesignTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteArtDesignTemplate]
GO







CREATE PROCEDURE [dbo].[DeleteArtDesignTemplate]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM artDesignTemplate WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteBackupInfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteBackupInfo]
GO







CREATE PROCEDURE [dbo].[DeleteBackupInfo]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM BackupInfo WHERE (pkId = @pkId) 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteBanner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteBanner]
GO







CREATE PROCEDURE [dbo].[DeleteBanner]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM banner WHERE (pkId = @pkId) 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteBigClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteBigClass]
GO

CREATE PROCEDURE  DeleteBigClass
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
BEGIN TRY 
BEGIN TRANSACTION

delete from BigClassAssociation where bigClassId = @pkId 
delete from contributeClass where bigClassId = @pkId
delete from contributeClass where contributeClassId in (select m.pkId from menu m where bigClassId=@pkId)

delete from siteContributeClass where bigClassId = @pkId

delete from moduleAssign where menuId in (select m.pkId from menu m where bigClassId=@pkId)
delete from groupAssign  where menuId in (select m.pkId from menu m where bigClassId=@pkId)   --add by shijian201701 for V7.0
delete from menu where bigClassId=@pkId

delete from bigclass where pkId=@pkId

delete from newsContent where newsid in (select pkid from news where bigclassId=@pkid ) --where bigclassId = @pkid
delete from news where bigclassId=@pkId

delete from messageBoard where bigClassId=@pkId  --留言本表
delete from people where bigClassId=@pkId  --人事物介绍表
delete from board where bigClassId=@pkId  --站点公告表
delete from link where bigClassId=@pkId  --链接表
delete from album where bigClassId=@pkId --相册表

COMMIT TRANSACTION   
END TRY 

BEGIN CATCH 
	ROLLBACK TRANSACTION ;
END CATCH


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteBigClassAssociateByMenuIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteBigClassAssociateByMenuIdAndBigClassId]
GO







CREATE PROCEDURE [dbo].[DeleteBigClassAssociateByMenuIdAndBigClassId]
(
	@menuId int,	-- menuId
	@bigClassId int
)
AS
	SET NOCOUNT OFF;
             
       DELETE FROM BigClassAssociation WHERE menuId = @menuId and bigClassId = @bigClassId  







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteBoard]
GO







CREATE PROCEDURE [dbo].[DeleteBoard]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM board WHERE (duplicatedPkId = @pkId) and (duplicateFrom='board')
DELETE FROM board WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteContributeClassByContributeClassIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteContributeClassByContributeClassIdAndBigClassId]
GO







CREATE PROCEDURE [dbo].[DeleteContributeClassByContributeClassIdAndBigClassId]
(
	@contributeClassId int,	-- menuId
	@bigClassId int
)
AS
	SET NOCOUNT OFF;
       
       DELETE FROM contribute 
       WHERE MenuId = @contributeClassId AND contributeId = @bigClassId
       DELETE FROM contributeClass WHERE contributeClassId = @contributeClassId and bigClassId = @bigClassId  







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteContributePostil]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteContributePostil]
GO







--region [dbo].[DeleteContributePostil]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[DeleteContributePostil]
-- Date Generated: 2007骞?2鏈?7鏃?
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[DeleteContributePostil]
	@pkId int
AS

SET NOCOUNT ON

DELETE FROM [dbo].[ContributePostil]
WHERE
	[pkId] = @pkId

--endregion







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteDepartment]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteDepartment]
GO



CREATE PROCEDURE DeleteDepartment
(
	@pkId int,
	@rtn varchar(100)output
)
AS
--news
DECLARE @count int
SELECT @count=Count(pkId) FROM news WHERE departmentId=@pkId
if( @count >0)
begin
set @rtn='news'
return
end
--users
SELECT @count=Count(pkId) FROM users WHERE departmentId=@pkId
if( @count >0)
begin
set @rtn='users'
return
end

DELETE FROM department WHERE (pkId = @pkId)
set @rtn='success'




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeletediscussProperty]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeletediscussProperty]
GO






-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DeletediscussProperty]
	-- Add the parameters for the stored procedure here
	@discussId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	delete from discussProperty where discussId = @discussId
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteDownloadFileBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteDownloadFileBypkId]
GO





CREATE PROCEDURE DeleteDownloadFileBypkId
	-- Add the parameters for the stored procedure here
	@pkId int
as
delete
    from 
    ResourceList
    where pkId = @pkId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteDuplicatedInfoByBoardIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteDuplicatedInfoByBoardIdAndBigClassId]
GO







CREATE PROCEDURE [dbo].[DeleteDuplicatedInfoByBoardIdAndBigClassId]
(
	@pkId int,
	@bigClassId int
)
AS
	SET NOCOUNT OFF
	
	BEGIN TRANSACTION
	
	DELETE FROM board WHERE (duplicatedPkId=@pkId) and duplicateFrom='board' and bigClassId = @bigClassId
	
	COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteDuplicatedInfoByNewsIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteDuplicatedInfoByNewsIdAndBigClassId]
GO

CREATE PROCEDURE DeleteDuplicatedInfoByNewsIdAndBigClassId
(
	@pkId int,
	@bigClassId int
)
AS
	SET NOCOUNT OFF
	
BEGIN TRY 	 
	BEGIN TRANSACTION;
	
	DECLARE @duplicatedId int

	-- 查询被抄送新闻并删除其对应评论
	SELECT @duplicatedId=pkId FROM news WHERE duplicatedPkId=@pkId and bigClassId = @bigClassId	
	DELETE FROM review WHERE (newsId=@duplicatedId)		
	DELETE FROM newsContent WHERE (newsId=@duplicatedId)
	-- 从新闻、公告中删除抄送新闻
	DELETE FROM news WHERE (duplicatedPkId=@pkId) and bigClassId = @bigClassId		
	DELETE FROM board WHERE (duplicatedPkId=@pkId) and duplicateFrom='news' and bigClassId = @bigClassId	
	
	COMMIT TRANSACTION;
END TRY 
BEGIN CATCH 
	ROLLBACK TRANSACTION ;
END CATCH

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteGrade]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteGrade]
GO



CREATE PROCEDURE DeleteGrade
(
	@pkId int,
    @rtn varchar(100) output 
)
AS
--news
DECLARE @count int
SELECT @count=Count(pkId) FROM news WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='news'
return
end

--menu
SELECT @count=Count(pkId) FROM menu WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='menu'
return
end

--users
SELECT @count=Count(pkId) FROM users WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='users'
return
end

--messageBoard
SELECT @count=Count(pkId) FROM messageBoard WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='messageBoard'
return
end

--people
SELECT @count=Count(pkId) FROM people WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='people'
return
end

--popwindow
SELECT @count=Count(pkId) FROM popwindow WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='popwindow'
return
end

--review
SELECT @count=Count(pkId) FROM review WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='review'
return
end

--ad
SELECT @count=Count(pkId) FROM ad WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='ad'
return
end

--board
SELECT @count=Count(pkId) FROM board WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='board'
return
end

--link
SELECT @count=Count(pkId) FROM link WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='link'
return
end

--linkGang
SELECT @count=Count(pkId) FROM linkGang WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='linkGang'
return
end

--linkGroup
SELECT @count=Count(pkId) FROM linkGroup WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='linkGroup'
return
end

--includePage
SELECT @count=Count(pkId) FROM includePage WHERE gradeId=@pkId
if( @count >0)
begin
set @rtn='includePage'
return
end

DELETE FROM grade WHERE (pkId = @pkId) and pkId!=1
set @rtn='success'




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteGroupAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteGroupAssign]
GO



create PROCEDURE DeleteGroupAssign
(
	@groupId int,
	@ModuleId int
)
AS

DELETE FROM groupAssign WHERE (groupId=@groupId and MenuId=@ModuleId)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteInfoSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteInfoSearch]
GO






-- =============================================
-- Author:		<Yinyf>
-- Create date: <2007-12-4>
-- Description:	<删除具体信息查询栏目对应的表信息>
-- =============================================
CREATE PROCEDURE DeleteInfoSearch
	@BigClassID int
AS
BEGIN	
	SET NOCOUNT ON;
	DELETE FROM InfoSearch WHERE bigClassId = @BigClassID
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteLayoutTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteLayoutTemplate]
GO







CREATE PROCEDURE [dbo].[DeleteLayoutTemplate]
	@pkId int
AS
	/* SET NOCOUNT ON */
	delete layoutTemplate where pkId=@pkId
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteLibrariesTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteLibrariesTemplate]
GO







CREATE PROCEDURE [dbo].[DeleteLibrariesTemplate]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM librariesTemplate WHERE (pkId = @pkId) 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteLinkGang]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteLinkGang]
GO







CREATE PROCEDURE [dbo].[DeleteLinkGang]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
	begin transaction
	DELETE FROM linkGang where (pkId = @pkId)
	commit transaction  
	






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteLockIP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteLockIP]
GO







CREATE PROCEDURE [dbo].[DeleteLockIP]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM LockIP WHERE (pkId = @pkId)






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteMenu]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteMenu]
GO

CREATE PROCEDURE DeleteMenu
(
@pkId int
)
as
 BEGIN TRANSACTION
	declare @mpkId int
	declare @count int
	declare @npkId int
	set @npkId=@pkId
	set @mpkId=-1
	select  @count=Count(*) from menu where  @pkId=parentId
	while(@count<>0 or @mpkId<>@pkId)
	begin
		SELECT top 1 @mpkId=pkId
		FROM menu
		WHERE @npkId=parentId
		select  @count=Count(*) from menu where  @pkId=parentId
		if(@count=0)
		begin
			break;
		end
		select @count=Count(*) from menu where  @mpkId=parentId
		set @npkId=@mpkId
		if(@count>0)
		begin
			continue
		end
		else
		begin
			delete from contributeClass where contributeClassId= @mpkId	-- edit contributeClass
			delete from moduleAssign where @mpkId=menuId
			delete from groupAssign where @mpkId=menuId --20141226 处理用户组授权bug
			delete from artDesign where @mpkId=menuId
			delete from menu where @mpkId=pkId  
			delete from bigClassDesign where menuId=@mpkId --edit bigclassDesign
			set @npkId=@pkId  
		end
	 END	 
	delete from BigClassAssociation where menuId = @pkId 
	delete from contributeClass where contributeClassId= @pkId	-- edit contributeClass
	delete from moduleAssign where @pkId=menuId
	delete from groupAssign where @pkId=menuId --20141226 处理用户组授权bug
	delete from artDesign where @pkId=menuId
	delete from menu where @pkId=pkId   
	delete from bigClassDesign where menuId=@pkId --edit bigclassDesign
 COMMIT TRANSACTION 
 return


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteMenuByParentIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteMenuByParentIdAndBigClassId]
GO

CREATE PROCEDURE DeleteMenuByParentIdAndBigClassId
(
	@parentId int,
	@BigClassId int
)
AS
	SET NOCOUNT OFF;
BEGIN TRANSACTION
declare @pkId int
select @pkId=pkId from menu where parentId=@parentId and bigClassId=@bigClassId
delete from moduleAssign where menuId=@pkId
delete from groupAssign where menuId=@pkId
delete from artDesign where menuId=@pkId
delete from bigclassDesign where menuId=@pkId
DELETE FROM menu WHERE parentId=@parentId and bigClassId=@bigClassId and modeId='4'
COMMIT TRANSACTION


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteMenuSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteMenuSet]
GO







CREATE PROCEDURE [dbo].[DeleteMenuSet]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM menuSet WHERE (pkId = @pkId and pkId<>1)








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteMessageBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteMessageBoard]
GO








CREATE PROCEDURE [dbo].[DeleteMessageBoard]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM messageBoard WHERE (pkId = @pkId)








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteModuleAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteModuleAssign]
GO







CREATE PROCEDURE [dbo].[DeleteModuleAssign]
(
	@usersId int,
	@ModuleId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM moduleAssign WHERE (usersId=@usersId and MenuId=@ModuleId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteNews]
GO

CREATE PROCEDURE DeleteNews
(
	@pkId int
)
AS
	SET NOCOUNT OFF;

	BEGIN TRY 	 
	BEGIN TRANSACTION;

	DECLARE @duplicatedId int

	-- 查询被抄送新闻并删除其对应评论
	DECLARE myCursor CURSOR FOR 
	SELECT pkId FROM news WHERE duplicatedPkId=@pkId
	OPEN myCursor

	FETCH NEXT FROM myCursor 
	INTO @duplicatedId
	WHILE @@FETCH_STATUS = 0
	BEGIN
		DELETE FROM review WHERE (newsId=@duplicatedId)
		FETCH NEXT FROM myCursor INTO @duplicatedId
	END
	CLOSE myCursor
	DEALLOCATE myCursor

-- 从新闻、公告中删除抄送新闻
	DELETE FROM newsContent WHERE newsId in(select pkid from news where duplicatedPkId=@pkId)   --删除抄送新闻content
	DELETE FROM news WHERE (duplicatedPkId=@pkId)--删除抄送新闻
	DELETE FROM board WHERE (duplicatedPkId=@pkId) and duplicateFrom='news'

	DELETE FROM review where (newsId=@pkId)
	DELETE FROM newsContent WHERE (newsId = @pkId)--删除源新闻content
	DELETE FROM news WHERE (pkId = @pkId)--删除源新闻
	--删除历史上的今天记录
	if exists(select pkId from todayInHistory where newsId = @pkId)
	delete from todayInHistory where newsId = @pkId

	COMMIT TRANSACTION;
	END TRY 
	BEGIN CATCH 
		ROLLBACK TRANSACTION ;
	END CATCH

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteOperationLogByType]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteOperationLogByType]
GO







  
CREATE PROCEDURE [dbo].[DeleteOperationLogByType]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
       
          DELETE FROM OperationLog WHERE type = @pkId   







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeletePeople]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeletePeople]
GO







CREATE PROCEDURE [dbo].[DeletePeople]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM people WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeletePopWindow]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeletePopWindow]
GO








CREATE PROCEDURE [dbo].[DeletePopWindow]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM popWindow WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeletePushNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeletePushNews]
GO

CREATE PROCEDURE DeletePushNews
(
	@pkid int
)
AS
	SET NOCOUNT OFF;
DELETE FROM PushNews WHERE (pkid = @pkid) 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteRegServiceList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteRegServiceList]
GO









CREATE PROCEDURE [dbo].[DeleteRegServiceList]
	@pkId int
AS
	Delete from ServiceClass where regServiceListId = @pkId
	 DELETE FROM RegServiceList WHERE pkId = @pkId   
	
	RETURN









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteServiceClassByRegServiceIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteServiceClassByRegServiceIdAndBigClassId]
GO








CREATE PROCEDURE [dbo].[DeleteServiceClassByRegServiceIdAndBigClassId]
(
	@regServiceId int,	
	@bigClassId int
)
AS
	SET NOCOUNT OFF;
       
    DELETE FROM ServiceClass WHERE RegServiceListId = @regServiceId and bigClassId = @bigClassId  







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteSignUpBuildFieldByName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteSignUpBuildFieldByName]
GO







CREATE PROCEDURE [dbo].[DeleteSignUpBuildFieldByName]
	@TableName varchar(100),
	@Name varchar(50)
AS

SET NOCOUNT ON

DELETE FROM [dbo].[SignUpBuildField]
WHERE [Name] = @Name and TableName=@TableName
	







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteSignUpBuildFieldByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteSignUpBuildFieldByPkId]
GO







CREATE PROCEDURE [dbo].[DeleteSignUpBuildFieldByPkId]
	@PkId int
AS

SET NOCOUNT ON

DELETE FROM [dbo].[SignUpBuildField]
WHERE PkId = @PkId








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteSignUpField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteSignUpField]
GO








CREATE PROCEDURE [dbo].[DeleteSignUpField]
	@PkId int
AS

SET NOCOUNT ON

DELETE FROM [dbo].[SignUpField]
WHERE [PkId] = @PkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteSiteContributeClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteSiteContributeClass]
GO








CREATE PROCEDURE [dbo].[DeleteSiteContributeClass]
(
	@bigClassId int
)
AS
	SET NOCOUNT OFF;
	 BEGIN TRANSACTION
DELETE FROM siteContributeClass WHERE (bigClassId = @bigClassId)
  COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteSpecial]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteSpecial]
GO








CREATE PROCEDURE [dbo].[DeleteSpecial]
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM special WHERE (pkId = @pkId) and pkId<>1







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteUsers]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteUsers]
GO

create PROCEDURE DeleteUsers
(
	@pkId int,
	@rtn varchar(100) output
)
AS
	SET NOCOUNT OFF;
--V522：删除用户时没判断，直接把news、board等9表数据删除。易导致删除一人就删除网站大部分数据的后果
--V6.0和201410之前的V6.1：软删除方式，设置users.isDel=1，同时不再删除news等9表数据。且只能手动恢复，
--坏处：导致1，发文统计仍有被删除的用户；导致2：无法再次注册该用户名。
--V6.1：判断和提示应删除哪些数据，方可删除用户；另外，本功能结合禁止登录功能使用，足以满足各种需求。
--好处：可删除真正应该删除的用户，如随便建的测试帐号、长时间不用且发文很少的用户。

DECLARE @count int
--news
SELECT @count=Count(pkId) FROM news WHERE usersid=@pkId
if( @count >0)
begin
set @rtn='news'
return
end
--messageBoard
SELECT @count=Count(pkId) FROM messageBoard WHERE userId=@pkId
if( @count >0)
begin
set @rtn='messageboard'
return
end
--link
SELECT @count=Count(pkId) FROM link WHERE userId=@pkId
if( @count >0)
begin
set @rtn='link'
return
end
--linkGroup
SELECT @count=Count(pkId) FROM linkGroup WHERE userId=@pkId
if( @count >0)
begin
set @rtn='linkgroup'
return
end
--linkGang
SELECT @count=Count(pkId) FROM linkGang WHERE userId=@pkId
if( @count >0)
begin
set @rtn='linkgang'
return
end
--ad
SELECT @count=Count(pkId) FROM ad WHERE userId=@pkId
if( @count >0)
begin
set @rtn='ad'
return
end
--people
SELECT @count=Count(pkId) FROM people WHERE userId=@pkId
if( @count >0)
begin
set @rtn='people'
return
end

--board
SELECT @count=Count(pkId) FROM board WHERE userId=@pkId
if( @count >0)
begin
set @rtn='board'
return
end
--popwindow
SELECT @count=Count(pkId) FROM popwindow WHERE userId=@pkId
if( @count >0)
begin
set @rtn='popwindow'
return
end

--bigclass  v7.0新增判断bigclass表中有无数据
SELECT @count=Count(pkId) FROM bigclass WHERE adminid=@pkId
if( @count >0)
begin
set @rtn='bigclass'
return
end

 BEGIN TRANSACTION
--以下注释V522版遗留问题:riches有些V6.x已删除
begin
IF  EXISTS (SELECT name from sysobjects where  type='U' and name='Riches')
DELETE FROM riches where (usersId=@pkId);   --riches为财富表，有FK与usersid关联,本表V6.1一般已删除
end
--DELETE FROM news WHERE (usersId = @pkId)
--DELETE FROM popwindow WHERE (userId = @pkId)
--DELETE FROM board where (userId=@pkId)
--DELETE FROM link WHERE (userId = @pkId)
--DELETE FROM people WHERE (userId = @pkId)
--DELETE FROM ad WHERE (userId = @pkId)
--DELETE FROM linkGang WHERE (userId=@pkId)
--DELETE FROM linkGroup WHERE (userId=@pkId)
--DELETE FROM messageBoard WHERE (userId=@pkId)
--update Users set isDel='1' WHERE (pkId = @pkId)
--6.0版本为软删除方式，6.1版本修改为真实删除。
DELETE FROM moduleAssign where(usersId=@pkId);
DELETE FROM Users WHERE (pkId = @pkId);
set @rtn='success'
 COMMIT TRANSACTION


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteUsersGroup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteUsersGroup]
GO



create PROCEDURE DeleteUsersGroup
(
	@pkId int
)
AS
	SET NOCOUNT OFF;
DELETE FROM UsersGroup WHERE pkId = @pkId
DELETE FROM groupAssign where groupId = @pkId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteWebResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteWebResear]
GO






CREATE PROCEDURE [dbo].[DeleteWebResear]
(@pkId int)
AS
	SET NOCOUNT OFF;
DELETE FROM question WHERE (pkId = @pkId)
DELETE from options where questionId =(@pkId)
Delete from answer where  questionId = (@pkId)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteWebResearAnswerBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteWebResearAnswerBypkId]
GO






CREATE PROCEDURE [dbo].[DeleteWebResearAnswerBypkId]
(@pkId int)
AS
update options
set options.counter = options.counter -1
from options,answer
where answer.pkId = (@pkId) and answer.optionsId = options.pkId
delete from answer 
where @pkId = answer.pkId





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteWebResearList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteWebResearList]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[DeleteWebResearList]
	-- Add the parameters for the stored procedure here
	@pkId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	delete from answer  
	where answer.questionId in (select pkId from question where questionnaireSpecialId = @pkId)
	delete from options
	where options.questionId in (select pkId from question where questionnaireSpecialId = @pkId)
	delete from question 
	where question.questionnaireSpecialId = @pkId
	delete from questionnaireSpecial
	where questionnaireSpecial.pkId = @pkId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DeleteWebResearOptions]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DeleteWebResearOptions]
GO







CREATE PROCEDURE [dbo].[DeleteWebResearOptions]
(@pkId int)
AS
	SET NOCOUNT OFF;
update question 
set optionsCount = optionsCount -1
from question,options
where options.questionId = question.pkId and (options.pkId=@pkId);
DELETE FROM options WHERE (pkId = @pkId);
DELETE FROM answer where (optionsId = @pkId)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableDelete]
GO






CREATE PROCEDURE dbo.DynamicTableDelete
	@PkId int
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		DELETE FROM DynamicTable WHERE pkId = @PkId
	COMMIT TRANSACTION
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableDeleteByTableName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableDeleteByTableName]
GO






CREATE PROCEDURE dbo.DynamicTableDeleteByTableName
	@TableName varchar(255)
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		DELETE FROM DynamicTable WHERE LTRIM(RTRIM(tableName)) = LTRIM(RTRIM(@TableName))
	COMMIT TRANSACTION
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableInsert]
GO






CREATE PROCEDURE dbo.DynamicTableInsert 
	@PkId int OUTPUT,
	@TableName varchar(255),
	@CreateTable varchar(6000)
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		INSERT INTO DynamicTable (tableName, createTable) 
		VALUES (@TableName, @CreateTable)
	COMMIT TRANSACTION
	SET @pkId = SCOPE_IDENTITY()
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableList]
GO






CREATE PROCEDURE [dbo].[DynamicTableList]
AS
BEGIN
	SET NOCOUNT OFF;
	SELECT * FROM DynamicTable
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableSearchByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableSearchByPkId]
GO






CREATE PROCEDURE dbo.DynamicTableSearchByPkId
	@PkId int
AS
BEGIN
	SET NOCOUNT OFF;
	SELECT TOP 1 * FROM DynamicTable WHERE pkId = @PkId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DynamicTableUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[DynamicTableUpdate]
GO






CREATE PROCEDURE dbo.DynamicTableUpdate
	@PkId int,
	@TableName varchar(255),
	@CreateTable varchar(6000)
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		 UPDATE DynamicTable SET tableName = @TableName, createTable = @CreateTable WHERE pkId = @PkId
	COMMIT TRANSACTION
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAccredit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAccredit]
GO


CREATE PROCEDURE [dbo].[GetAccredit]
AS
	select pkId,name,parentId,modeId
	from menu 
	where modeId<>4
	union
	SELECT m.pkId,m.name,m.parentId,m.modeId
   	FROM menu m,bigClass b,template t	
	 where t.code<>'newsTop'
		and t.code<>'gameTop' 
		and t.code<>'latestGame' 
		and t.code<>'todayInHistoryShort' 
		and t.code<>'todayInHistoryPic' 
		and t.code<>'searchTodayInHistory' 
		and t.code<>'fileDownTop'
		and t.code<>'special'
		and t.code<>'login'
		and t.code<>'nav' 
		and t.code<>'LoginHorizontal'
		and t.code<>'baidu'
		and t.code<>'shareNews'
		and t.code<>'LatestNews'
		and t.code<>'LatestImageNews'
		and t.code<>'ArticleStatistics'
		and t.code<>'VisitorsSessions'
		and t.code<>'OnlineStatistics'
		and t.code<>'AdminLogin' 
		and t.code<>'contributeStat'
		and t.code<>'LatestMessage' 
		and t.code<>'contributeTop'
		and t.code<>'latestContribute'
		and t.code<>'newsSummary'
		and t.code<>'messageboardSummary' 
		and t.code<>'releaseTop'
		and t.code<>'latestscrollFlashNews' 
		and t.code<>'newsCalendar'
		and m.bigClassId=b.pkId
		and b.templateId=t.pkId		
	RETURN



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAccreditByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAccreditByAdminId]
GO


CREATE PROCEDURE [dbo].[GetAccreditByAdminId]
	@adminId int
AS
	select m.pkId,name,parentId, modeId
	from menu m,adminAssign a
	where 
	a.menuId=0
	and @adminId=a.adminId
	and m.pkId=0
	union
		select m.pkId,name,parentId, m.modeId
	from menu m,adminAssign a
	where 
	parentId=0
	and a.adminId=@adminId
	and a.menuId=m.pkId
	
	union
	select pkId,name,parentId, modeId
	from menu 
	where 
	modeId<>4 
	and parentId<>0
	union
	SELECT m.pkId,m.name,m.parentId, m.modeId
   	FROM menu m,bigClass b,template t,adminAssign a	
	where t.code<>'newsTop'
		and t.code<>'gameTop' 
		and t.code<>'latestGame' 
		and t.code<>'todayInHistoryShort' 
		and t.code<>'todayInHistoryPic' 
		and t.code<>'searchTodayInHistory' 
		and t.code<>'fileDownTop'
		and t.code<>'special'
		and t.code<>'login'
		and t.code<>'nav'
		and t.code<>'search'
		and t.code<>'SearchHorizontal'
		and t.code<>'LoginHorizontal'
	    and t.code<>'baidu'
	   	and t.code<>'shareNews'
		and t.code<>'LatestNews'
		and t.code<>'LatestImageNews'
		and t.code<>'ArticleStatistics'
		and t.code<>'VisitorsSessions'
		and t.code<>'OnlineStatistics'
		and t.code<>'AdminLogin'
		and t.code<>'contributeStat'
		and t.code<>'LatestMessage'
		and t.code<>'contributeTop'
		and t.code<>'latestContribute'
		and t.code<>'newsSummary'
		and t.code<>'messageboardSummary'
		and t.code<>'releaseTop'
		and t.code<>'latestscrollFlashNews' 
		and t.code<>'newsCalendar'
		and m.bigClassId=b.pkId
		and b.templateId=t.pkId		
	  
	RETURN



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdByBigClassIdAndtype]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdByBigClassIdAndtype]
GO







CREATE PROCEDURE [dbo].[GetAdByBigClassIdAndtype]
@bigClassId int=null,
@gradeid int=1,
@type int,
@gateId int
AS
	 SET NOCOUNT ON
	 
	 select a.*,u.userName "userName",g.name "grade" from ad a,users u,grade g
       where  a.gate<>@gateId
          and a.bigClassId=@bigClassId 
          and a.gradeId<=@gradeId
          and a.gradeId=g.pkId
          and a.userid=u.pkid
          and a.passed=1
          and a.dating>0
                   
       order by istop desc,a.pkId desc
    return







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdmin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdmin]
GO









CREATE PROCEDURE [dbo].[GetAdmin]
AS
	SET NOCOUNT ON;
SELECT * FROM Admin









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdminAssignByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdminAssignByAdminId]
GO







CREATE PROCEDURE [dbo].[GetAdminAssignByAdminId]
@adminId int
AS
	/* SET NOCOUNT ON */
	select * from AdminAssign
	where @adminId=adminId
	RETURN 
	






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdminByAdminName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdminByAdminName]
GO








CREATE PROCEDURE [dbo].[GetAdminByAdminName]
@adminName varchar(100)
AS
	SET NOCOUNT ON;
SELECT * FROM Admin where  adminName=@adminName







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdminByAdminNameAndPassword]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdminByAdminNameAndPassword]
GO







CREATE PROCEDURE [dbo].[GetAdminByAdminNameAndPassword]
@adminName varchar(100),
@password  varchar(100)
AS
	SET NOCOUNT ON;
SELECT * FROM Admin 
where  adminName=@adminName
  and  password=@password








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAdminByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAdminByPkId]
GO







CREATE PROCEDURE [dbo].[GetAdminByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM Admin where  pkId=@pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAllSignUpField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAllSignUpField]
GO








CREATE PROCEDURE [dbo].[GetAllSignUpField]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	*
FROM
	[dbo].[SignUpField]






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAllSignUpTable]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAllSignUpTable]
GO







CREATE PROCEDURE [dbo].[GetAllSignUpTable]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@tableName varchar(255),
	@recordCount int output
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
	
		SET @str = '1=1'

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'PkId ASC'
	SET @orderTypeD = 'PkId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) from '+Convert(nvarchar(50),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(2000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' *  FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' * FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' * FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignByMenuId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignByMenuId]
GO







CREATE PROCEDURE [dbo].[GetArtDesignByMenuId]
@menuId int
AS
	SET NOCOUNT ON;
SELECT * FROM artDesign where menuId=@menuId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignByPkId]
GO








CREATE PROCEDURE [dbo].[GetArtDesignByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM artDesign where pkId=@pkId








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignByState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignByState]
GO



Create PROCEDURE GetArtDesignByState
AS
	SET NOCOUNT ON;
--TODO：是否更新被使用标志，待定
update artDesignTemplate set artstate ='0';
update artDesignTemplate set artstate ='1' where pkid in (SELECT distinct m.artDesignTemplateId FROM menu m 
inner join artDesignTemplate a 
on  m.artDesignTemplateId=a.pkId
where m.modeId=4 and m.artDesignTemplateId!=0)

--返回频道页的模板集合
SELECT pkId FROM  artDesignTemplate  
where artState='1'




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignTemplate]
GO








CREATE PROCEDURE [dbo].[GetArtDesignTemplate]
AS
	SET NOCOUNT ON;
SELECT * FROM artDesignTemplate order by pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignTemplateByName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignTemplateByName]
GO








CREATE PROCEDURE [dbo].[GetArtDesignTemplateByName]
@name varchar(100)
AS
	SET NOCOUNT ON;
SELECT * FROM artDesignTemplate where [name]=@name







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArtDesignTemplateByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArtDesignTemplateByPkId]
GO







CREATE PROCEDURE [dbo].[GetArtDesignTemplateByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM artDesignTemplate where pkId=@pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetArticleStatistics]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetArticleStatistics]
GO




-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetArticleStatistics]
(
	@bigClassId int ,
	@passed int,
    @gradeid int ,
    @gateid int
)	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @num int
	select @num=0 
	select @num= count(pkId) from BigClassAssociation where thisBigClassId = @bigClassId
	if (@num = 0)
		BEGIN			
			
			select count(*) from news n,users u,department d,bigClass b 
							where n.usersId=u.pkId 
							and n.departmentId=d.pkId 
							and n.bigClassId=b.pkId	
							and (n.validDays > 0 OR n.validDays < 0) 
							and n.passed = 1 and isDuplicated != 1
			
		END
	else
		BEGIN
			select count(*) from news n,users u,department d,bigClass b 
						 where n.usersId=u.pkId 
							and n.departmentId=d.pkId 
							and n.bigClassId=b.pkId	
							and (n.validDays > 0 OR n.validDays < 0) 
							and n.passed = 1 
							and isDuplicated != 1 
							--n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@bigClassId)
							and exists (select 1 from BigClassAssociation ba where ba.bigclassid = n.bigclassid and  ba.thisBigclassId=@bigClassId) 	
		End
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAssessorNewsStat]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAssessorNewsStat]
GO




CREATE PROCEDURE dbo.GetAssessorNewsStat
(
	          @PageIndex		int,
	          @PageSize		    int,
              @StartTime        datetime,
              @EndTime          datetime,
              @UserName           nvarchar(50),
              @Totalnews        int output
)
AS
	BEGIN
	
	
	SET @PageIndex = @PageIndex - 1
	IF (@PageIndex < 0)
		BEGIN
			SET @PageIndex = 1
		END


    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT    
    SET @PageLowerBound = @PageSize * @PageIndex
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
      
      DECLARE @sql nvarchar(4000)
      DECLARE @where nvarchar(4000)
      DECLARE @where1 nvarchar(100)
      
	  SET @where = ' WHERE ISNUMERIC(CopyFrom)=1 and DATEDIFF(day, ''' + CONVERT(nvarchar, @StartTime, 120) + ''', updatetime) >= 0 and 
						 DATEDIFF(day, ''' + CONVERT(nvarchar, @endtime, 120) + ''', updatetime) <= 0  '
      
      SET @where1 = 'where 1=1'
      
      IF(@UserName<>'')
      BEGIN
          SET @where1=@where1+' and UserName LIKE ''%' + @UserName + '%'''
      END
      
      SET @sql='
      
      IF object_id(''tempdb..#AllNews'') IS NOT NULL
		BEGIN
			DROP TABLE #AllNews
		END
      
      CREATE TABLE #AllNews
      (
        BigClassId   INT,
        intactTitle  NVARCHAR(100),
        updatetime   DATETIME,
        Author       NVARCHAR(50),
        pass         INT,
        bigClassName  NVARCHAR(50),
        UnitId      NVARCHAR(50),
        UserName    NVARCHAR(50),
        assessorId   INT
      )
      
      
      insert into #AllNews(BigClassId,intactTitle,updatetime,Author,pass,UnitId,assessorId)
      select bigClassId,intactTitle,updatetime,Author,passed,copyFrom,AuditUserId
      from news
      '+@where+' and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.typeId=2) order by updatetime desc
      
      update #AllNews
      set bigClassName=
      (
        select name from bigClass as a where a.pkId=#AllNews.BigClassId
      )
      
      update #AllNews
      set UserName=
      (
        select fullName from Users as a where a.pkId=#AllNews.assessorId 
      )
      
      
       IF object_id(''tempdb..#PageIndexs'') IS NOT NULL
		BEGIN
			DROP TABLE #PageIndexs
		END
      
      CREATE TABLE #PageIndexs
      (
        IndexId       int IDENTITY (0, 1) NOT NULL,
        bigClassName  NVARCHAR(50),
        intactTitle   NVARCHAR(100),
        updatetime    DATETIME,
        Author        NVARCHAR(50),
        UnitId        NVARCHAR(50), 
        pass          INT
      )
      
      insert into #PageIndexs(bigClassName,intactTitle,updatetime,Author,UnitId,pass)
      select bigClassName,intactTitle,updatetime,Author,UnitId,pass
      from #AllNews  '+@where1+'
      
      select bigClassName,intactTitle,convert(char(10),updatetime,120) as times,Author,UnitId,pass
      from #PageIndexs
      where IndexId >= '+str(@PageLowerBound)+' AND IndexId <= '+str(@PageUpperBound)+'
      
      
      select @InternalTotalnews=count(*) from #PageIndexs
      
      IF object_id(''tempdb..#AllNews'') IS NOT NULL
      BEGIN
        DROP TABLE #AllNews
      END
      
      IF object_id(''tempdb..#PageIndexs'') IS NOT NULL
      BEGIN
        DROP TABLE #PageIndexs
      END'
      
      
      
      EXEC sp_ExecuteSQL @sql,
      N'@InternalTotalnews int output', 
	  @Totalnews output
	
	RETURN
    END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingBoardByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingBoardByUserId]
GO



CREATE PROCEDURE [dbo].[GetAuditingBoardByUserId]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
select b.pkId bigclassId, m.name menuName,m.pkId sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test z, board n,users u,grade g, template t, bigClass b '

end
else
begin

	select b.pkId bigclassId, m.name menuName,m.pkId sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test1 z, board n,users u,grade g, template t, bigClass b '

end

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingCount]
GO




CREATE PROCEDURE [dbo].[GetAuditingCount]
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@newsCount int output, --news表待审条数
	@reviewsCount int output, --评论待审条数
	@messageBoardCount int output,--留言待审条数
	@linkCount int output,--友情链接待审条数
	@peopleCount int output,--人事物介绍栏目待审条数
	@boardCount int output,--公告待审条数
	@linkGangCount int output  --列表链接待审条数
	
AS
	
	--初始化查询条件(不带where)
	DECLARE @str1 nvarchar(1000)
	DECLARE @str2 nvarchar(1000)
	DECLARE @str3 nvarchar(4000)
	DECLARE @str4 nvarchar(4000)
	DECLARE @str5 nvarchar(4000)
	DECLARE @str6 nvarchar(4000)
	DECLARE @str7 nvarchar(4000)

	--设置查询表名称
	DECLARE @tableName nvarchar(100)

	--判断当前用户是否属于用户组
	Declare @groupid int 
	select @groupid = usersGroupId from users where pkid=@userId
--------------新闻开始--------------
	--新闻条件
	SET @str1 = ' 1=1 '
	SET @str1 = @str1 + ' and n.bigclassId = z.bigclassId'
	SET @str1 = @str1 + ' and z.bigclassId = b.pkid'
	SET @str1 = @str1 + ' and b.templateId = t.pkId'
	SET @str1 = @str1 + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str1 = @str1 + ' and n.gate<>'+STR(@gateId)
	SET @str1 = @str1 + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId '

if(@groupid=1)
begin
	--新闻 用户权限
	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
	--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	--根据栏目Id进行分组，以消除不用菜单位置的相同栏目产生的重复记录，20091010
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #testnews from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @tableName = '#testnews z, news n,users u,department d,grade g, template t, bigClass b '
end
else
begin
	--新闻 用户组权限
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #testnews1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name
	
	SET @tableName = '#testnews1 z, news n,users u,department d,grade g, template t, bigClass b '
end

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(4000)
	SET @vSQL = N'SELECT @count1 = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(1000),@str1)
	EXEC sp_ExecuteSQL @vSQL, N'@count1 int output', @tempNum output
	SELECT @newsCount = @tempNum
--------------评论开始---------------

	--评论条件
	SET @str2 = ' 1=1 '
	SET @str2 = @str2 + ' and n.bigclassId = z.bigclassId'
	SET @str2 = @str2 + ' and z.bigclassId = b.pkid'
	SET @str2 = @str2 + ' and b.templateId = t.pkId'
	SET @str2 = @str2 + ' and r.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str2 = @str2 + ' and n.gate<>'+STR(@gateId)
	SET @str2 = @str2 + ' and n.usersId=u.pkId and n.gradeId=g.pkId '
	SET @str2 = @str2 + ' and r.newsId=n.pkId '

if(@groupid=1)
begin
	--评论 用户权限
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #testreview from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @tableName = '#testreview z, news n,users u,grade g, template t, bigClass b, review r '
end
else
begin
	--评论 用户组权限
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #testreview1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name
	
	SET @tableName = '#testreview1 z, news n,users u,grade g, template t, bigClass b, review r '
end

	SET @vSQL = N'SELECT @count2 = Count(r.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(1000),@str2) 
	EXEC sp_ExecuteSQL @vSQL, N'@count2 int output', @tempNum output
	SELECT @reviewsCount = @tempNum

--------------留言本开始-----------------
	
	--留言本
	SET @str3 = ' 1=1 '
	SET @str3 = @str3 + ' and  m.pkid in (select m.pkId from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and a.usersId='+STR(@userId)+' and charindex(''canAuditNews=1'',privilegeSet)>0 )'
	SET @str3 = @str3 + ' and b.bigclassid=m.bigclassid  and checked=0 and b.gradeId<='+STR(@gradeId)
	SET @str3 = @str3 + ' and b.gate<>'+STR(@gateId)


----------------友情链接开始-----------------
	SET @str4 = ' 1=1 '
	SET @str4 = @str4 + ' and n.bigClassId in (select b.pkId from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and a.usersId='+STR(@userId)+' and charindex(''canAuditNews=1'',privilegeSet)>0 )'
	SET @str4 = @str4 + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str4 = @str4 + ' and n.gate<>'+STR(@gateId)
	SET @str4 = @str4 + ' and n.userId=u.pkId'
	--SET @str4 = @str4 + ' and ' + @manageAll 
	--由于link表中字段为userid 和 news表中的usersid不一致，导致生成的@manageAll条件不成立

---------------人物事物开始-------------------


	SET @str5 = ' 1=1 '
	SET @str5 = @str5 + ' and n.bigClassId in (select b.pkId from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and a.usersId='+STR(@userId)+' and charindex(''canAuditNews=1'',privilegeSet)>0 )'
	SET @str5 = @str5 + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str5 = @str5 + ' and n.gate<>'+STR(@gateId)
	--SET @str5 = @str5 + ' and n.userId=u.pkId'
	--SET @str5 = @str5 + ' and ' + @manageAll
	--由于people表中字段为userid 和 news表中的usersid不一致，导致生成的@manageAll条件不成立


--------------------公告-----------------------
	SET @str6 = ' 1=1 '
	SET @str6 = @str6 + ' and n.bigClassId in (select b.pkId from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and a.usersId='+STR(@userId)+' and charindex(''canAuditNews=1'',privilegeSet)>0 )'
	SET @str6 = @str6 + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str6 = @str6 + ' and n.gate<>'+STR(@gateId)
	SET @str6 = @str6 + ' and n.userId=u.pkId'
	--SET @str6 = @str6 + ' and ' + @manageAll

-------------------列表链接-------------------
	SET @str7 = ' 1=1 '
	SET @str7 = @str7 + ' and n.groupid=l.pkid and exists (select m.pkId from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and a.usersId='+STR(@userId)+' and charindex(''canAuditNews=1'',privilegeSet)>0 and n.menuid=m.pkid )'
	SET @str7 = @str7 + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str7 = @str7 + ' and n.gate<>'+STR(@gateId)
	SET @str7 = @str7 + ' and n.userId=u.pkId'
	--SET @str7 = @str7 + ' and ' + @manageAll



--清理零时表
if(@groupid=1)
begin
delete #testnews
delete #testreview
end
else
begin
delete #testnews1
delete #testreview1
end


	SET @vSQL = N'SELECT @count3 = Count(b.pkId) from  messageBoard b,menu m where ' + Convert(nvarchar(4000),@str3)
	EXEC sp_ExecuteSQL @vSQL, N'@count3 int output', @tempNum output
	SELECT @messageBoardCount = @tempNum
	
	
	SET @vSQL = N'SELECT @count4 = Count(n.pkId) from link n,users u where ' + Convert(nvarchar(4000),@str4)
	EXEC sp_ExecuteSQL @vSQL, N'@count4 int output', @tempNum output
	SELECT @linkCount = @tempNum	
	
	SET @vSQL = N'SELECT @count5 = Count(n.pkId) from people n where ' + Convert(nvarchar(4000),@str5)
	EXEC sp_ExecuteSQL @vSQL, N'@count5 int output', @tempNum output
	SELECT @peopleCount = @tempNum
	
	SET @vSQL = N'SELECT @count6 = Count(n.pkId) from board n,users u where ' + Convert(nvarchar(4000),@str6)
	EXEC sp_ExecuteSQL @vSQL, N'@count6 int output', @tempNum output
	SELECT @boardCount = @tempNum	
	
	SET @vSQL = N'SELECT @count7 = Count(n.pkId) from linkGang n, linkgroup l,users u where ' + Convert(nvarchar(4000),@str7)
	EXEC sp_ExecuteSQL @vSQL, N'@count7 int output', @tempNum output
	SELECT @linkGangCount = @tempNum

RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingLinkByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingLinkByUserId]
GO



CREATE PROCEDURE [dbo].[GetAuditingLinkByUserId]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test z, link n,users u,grade g, template t, bigClass b '

end
else
begin

select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test1 z, link n,users u,grade g, template t, bigClass b '

end
	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingLinkGangByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingLinkGangByUserId]
GO



CREATE PROCEDURE [dbo].[GetAuditingLinkGangByUserId]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and l.bigClassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId and n.groupId=l.pkId'

	SET @tableName = '#test z, linkGang n,users u,grade g, template t, bigClass b, linkGroup l '
end
else
begin

select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and l.bigClassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId and n.groupId=l.pkId'

	SET @tableName = '#test1 z, linkGang n,users u,grade g, template t, bigClass b, linkGroup l '

end
	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code, l.bigClassId FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = l.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code, l.bigClassId FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code, l.bigClassId FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code, l.bigClassId FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingMessageBoardByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingMessageBoardByUserId]
GO



CREATE PROCEDURE [dbo].[GetAuditingMessageBoardByUserId]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(1000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
--根据栏目Id进行分组，以消除不用菜单位置的相同栏目产生的重复记录，20091010
select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #MessageBoardTemp from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and m.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and m.checked=0 and m.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and m.gate<>'+STR(@gateId)
	SET @str = @str + ' and m.gradeId=g.pkId '

	SET @tableName = '#MessageBoardTemp z, messageBoard m, grade g , bigClass b '

end
else
begin
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #MessageBoardTemp1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and m.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and m.checked=0 and m.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and m.gate<>'+STR(@gateId)
	SET @str = @str + ' and m.gradeId=g.pkId '

	SET @tableName = '#MessageBoardTemp1 z, messageBoard m, grade g , bigClass b '
end
	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'isTop ASC, msgId ASC'
	SET @orderTypeD = 'isTop DESC, msgId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(m.pkId) from '+Convert(nvarchar(150),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(2000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' z.menuName,z.sourceMenuId, m.*,g.name "grade", m.pkId as msgId FROM '+Convert(nvarchar(150),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' z.menuName,z.sourceMenuId, m.*,g.name "grade", m.pkId as msgId  FROM '+Convert(nvarchar(150),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' z.menuName,z.sourceMenuId, m.*,g.name "grade", m.pkId as msgId FROM '+Convert(nvarchar(150),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' z.menuName,z.sourceMenuId,m.*,g.name "grade", m.pkId as msgId FROM '+Convert(nvarchar(150),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)

RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingNewsByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingNewsByUserId]
GO

CREATE PROCEDURE GetAuditingNewsByUserId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)

	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin

--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
--根据栏目Id进行分组，以消除不用菜单位置的相同栏目产生的重复记录，20091010
select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId ' --and n.MenuId=m.pkId 去掉了 fengyb 2008年-1-9日and n.MenuId=m.pkId 
	
	SET @tableName = '#test z, news n,users u,department d,grade g, template t, bigClass b '

end
else
begin
	
	SELECT b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId ' --and n.MenuId=m.pkId 去掉了 fengyb 2008年-1-9日and n.MenuId=m.pkId 

	SET @tableName = '#test1 z, news n,users u,department d,grade g, template t, bigClass b '
end
 
	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'ntime ASC, sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'ntime DESC, sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)
	
	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END
	
	--执行查询
	EXEC(@sqlStr)
	if(@groupid=1)
	begin
		DELETE #test
	end
	else
	begin
		DELETE #test1
	end
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingPeopleByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingPeopleByUserId]
GO



CREATE PROCEDURE [dbo].[GetAuditingPeopleByUserId]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test z, people n,users u,grade g, template t, bigClass b '

end
else
begin

select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=0 and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.userId=u.pkId and n.gradeId=g.pkId '

	SET @tableName = '#test1 z, people n,users u,grade g, template t, bigClass b '
end

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.*,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditingReviewsByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditingReviewsByUserId]
GO

CREATE PROCEDURE GetAuditingReviewsByUserId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	DECLARE @str nvarchar(1000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name
	
	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.pkid in (select newsid from review where passed=0) and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId '
	
	SET @tableName = '#test z, news n,users u,grade g, template t, bigClass b'

end
else
begin
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid and charindex('canAuditNews=1',privilegeSet)>0 group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.pkid in (select newsid from review where passed=0) and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId '
	
	SET @tableName = '#test1 z, news n,users u,grade g, template t, bigClass b'
end
	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'ntime ASC,sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'ntime DESC,sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
	if(@groupid=1)
	begin
		DELETE #test
	end
	else
	begin
		DELETE #test1
	end
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAuditUserName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetAuditUserName]
GO




CREATE PROCEDURE dbo.GetAuditUserName

AS
	
	select fullname from Users where pkId>0 and pkId in(select distinct AuditUserID from news where ISNUMERIC(CopyFrom)=1 and AuditUserID is not null)
	
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBackupInfoByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBackupInfoByPkId]
GO







CREATE PROCEDURE [dbo].[GetBackupInfoByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM BackupInfo
WHERE @pkId=pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBannerByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBannerByAdminId]
GO








CREATE PROCEDURE [dbo].[GetBannerByAdminId]
@adminId int
AS
	SET NOCOUNT ON;
	SELECT * FROM banner
where isSystem=1
union
SELECT * FROM banner
where adminId=@adminId
and isSystem=0
order by isSystem desc,pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBannerByGroupingId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBannerByGroupingId]
GO







CREATE PROCEDURE [dbo].[GetBannerByGroupingId]
@groupingId int
AS
	SET NOCOUNT ON;
SELECT * FROM banner 
where @groupingId=grouping







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBannerByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBannerByPkId]
GO







CREATE PROCEDURE [dbo].[GetBannerByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM banner where @pkId=pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBannerGroup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBannerGroup]
GO







CREATE PROCEDURE [dbo].[GetBannerGroup]
AS
	SET NOCOUNT ON;
SELECT grouping FROM banner 
 group by grouping







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBannerGroupByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBannerGroupByAdminId]
GO







CREATE PROCEDURE [dbo].[GetBannerGroupByAdminId]
@adminId int
AS
	SET NOCOUNT ON;
SELECT grouping FROM banner 
where isSystem=1
 group by grouping
union
select grouping from banner
where isSystem=0
and adminId=@adminId
 group by grouping







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBigClassByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBigClassByAdminId]
GO


CREATE PROCEDURE GetBigClassByAdminId
@adminId int
AS
	SET NOCOUNT ON;
	SELECT b.*, t.name 'template',t.code,t.previewTemplate,a.username as adminName, tp.type, tp.pkid as typeid 
       
FROM bigClass b,template t,users a, templateType tp
where 
   b.templateId=t.pkId
   and b.isSystemItem=1
   and b.adminId=a.pkId
   and t.typeId=tp.pkId
   
UNION
SELECT b.*, t.name 'template',t.code,t.previewTemplate,a.username as adminName, tp.type, tp.pkId
       
FROM bigClass b,template t,users a, templateType tp
where 
   b.templateId=t.pkId
   and b.isSystemItem=0
   and b.adminId=@adminId
   and b.adminId=a.pkId
   and t.typeId=tp.pkId
   
  order by b.isSystemItem desc,t.name, tp.pkId, b.pkId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBigClassForDuplicatedBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBigClassForDuplicatedBoard]
GO







CREATE PROCEDURE [dbo].[GetBigClassForDuplicatedBoard]
(
	@pkId int
)	
AS
	SET NOCOUNT ON
	select b.pkId, b.name from bigClass b, board n
	where b.pkId = n.bigClassId and n.duplicatedPkId = @pkId and n.duplicateFrom='board'
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBigClassForDuplicatedNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBigClassForDuplicatedNews]
GO







CREATE PROCEDURE [dbo].[GetBigClassForDuplicatedNews]
(
	@pkId int
)
AS
	SET NOCOUNT ON
	select b.pkId, b.name from bigClass b, news n
	where b.pkId = n.bigClassId and n.duplicatedPkId = @pkId and n.duplicateFrom='news' 
	union all 
	select b.pkId, b.name from bigClass b, board n
	where b.pkId = n.bigClassId and n.duplicatedPkId = @pkId and n.duplicateFrom='news'
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBoardByBigClassIdAndDuplicatedPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBoardByBigClassIdAndDuplicatedPkId]
GO







CREATE PROCEDURE [dbo].[GetBoardByBigClassIdAndDuplicatedPkId]
(
	@bigClassId int,
	@duplicatedPkId int,	
	@duplicatedFrom varchar(100)
)
AS
	SET NOCOUNT ON
	SELECT * FROM board WHERE bigClassId = @bigClassId AND duplicatedPkId = @duplicatedPkId AND duplicateFrom = @duplicatedFrom
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetBoardByBigClassIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetBoardByBigClassIdAndPassed]
GO


CREATE PROCEDURE GetBoardByBigClassIdAndPassed
@bigClassId int=null,
@gradeid int=1,
@gateId int
AS
	SET NOCOUNT ON
	
	select b.*,u.userName 'userName', g.name 'grade'
	 from board b,users u,grade g
	where  b.gradeId<=@gradeId
	 and  b.passed=1
	 
	 and gate<>@gateId
	 and @bigClassId=bigClassId
	 and b.userId=u.pkId
	  and @gradeId=g.pkId
	order by IsTop desc,b.pkId desc
	RETURN 



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributeByMenuIdAndBigClassIdAndUserIdForAudit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributeByMenuIdAndBigClassIdAndUserIdForAudit]
GO












CREATE PROCEDURE [dbo].[GetContributeByMenuIdAndBigClassIdAndUserIdForAudit]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@menuId int,
	@bigClassId int,
	@userId int,
	@recordCount int output	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(c.pkId) FROM contribute c, users u WHERE c.menuId=' + CONVERT(nvarchar,@menuId) 
				+ N' AND c.bigClassId=' + CONVERT(nvarchar,@bigClassId) + N' AND c.userId=' + CONVERT(nvarchar,@userId) 
				+ N' AND c.userId=u.pkId and (c.state=0 or c.state=2)'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
		
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' c.*,u.userName "userName",u.fullName "fullName"  FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=' + STR(@userId) 
					+ ' and c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1	
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' c.*,u.userName "userName",u.fullName "fullName" FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=' + STR(@userId) 
					+ ' and c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId ASC) TempTable ORDER BY pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' c.*,u.userName "userName",u.fullName "fullName"  FROM contribute c, users u '
					+ 'WHERE c.menuId<=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=' + STR(@userId) 
					+ ' and c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId ASC) TempTable ORDER BY pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' c.*,u.userName "userName",u.fullName "fullName" FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=' + STR(@userId) 
					+ ' AND c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)
RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributeByMenuIdAndBigClassIdForAudit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributeByMenuIdAndBigClassIdForAudit]
GO







CREATE PROCEDURE [dbo].[GetContributeByMenuIdAndBigClassIdForAudit]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@menuId int,
	@bigClassId int,
	@recordCount int output	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(c.pkId) FROM contribute c, users u WHERE c.menuId=' + CONVERT(nvarchar,@menuId) 
				+ N' AND c.bigClassId=' + CONVERT(nvarchar,@bigClassId) + N' AND c.userId=u.pkId and (c.state=0 or c.state=2)'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' c.*,u.userName "userName",u.fullName "fullName" FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' c.*,u.userName "userName",u.fullName "fullName" FROM contribute c, users u '
					+ 'WHERE c.menuId<=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId ASC) TempTable ORDER BY pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' c.*,u.userName "userName",u.fullName "fullName"  FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId ASC) TempTable ORDER BY pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' c.*,u.userName "userName",u.fullName "fullName"  FROM contribute c, users u '
					+ 'WHERE c.menuId=' + STR(@menuId) + ' AND c.bigClassId=' + STR(@bigClassId) + ' AND c.userId=u.pkId and (c.state=0 or c.state=2) ORDER BY c.pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributeByStatTime]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributeByStatTime]
GO





create PROCEDURE [dbo].[GetContributeByStatTime]
	@beginTime varchar(20),
	@endTime varchar(20),
	@pageIndex int,
	@pageSize int,
	@recordCount int output,
	@contributeSortType int
AS
BEGIN	
	SET NOCOUNT ON;
	
	DECLARE @isSql2005 int, @isSql2000 int, @tempTable varchar(20)		
	
	-- 判断数据库版本  修正支持sql2005 以上版本
	IF PATINDEX('%2005 - 9%', @@VERSION)<>0
	BEGIN
		SET @isSql2005 = 1
		SET @isSql2000 = 0
	END
	ELSE IF PATINDEX('%2000 - 8%', @@VERSION)<>0
	BEGIN
		SET @isSql2000 = 1
		SET @isSql2005 = 0
		SET @tempTable = 'TempA' + LTRIM(STR(YEAR(GetDate()))) + LTRIM(STR(MONTH(GetDate()))) + LTRIM(STR(DAY(GetDate()))) 
					+ LTRIM(STR(DATEPART(hh,GetDate()))) + LTRIM(STR(DATEPART(n,GetDate()))) + LTRIM(STR(DATEPART(ss,GetDate())))
	END
	else
	begin
		SET @isSql2005 = 1
		SET @isSql2000 = 0
	end
		
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000), @sortFiled varchar(50)
	if(@contributeSortType=0)
		SET @sortFiled = 'articleNums'
	Else
		SET @sortFiled = 'totalNums'
	
	SET @vSQL = N'SELECT @count = Count(*) FROM (SELECT u.pkId as userId, Count(n.pkId) as articleNums FROM Users u, news n, contribute c '
			+'WHERE u.pkId = c.userId AND c.newsId = n.pkId AND c.state = 1 AND c.updateTime >= ''' + @beginTime + ''' AND c.updateTime <''' 
			+ @endTime + ''' GROUP BY u.pkId) TempTable'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
	
	--转换成索引
	DECLARE @pageIndex_1 int
	SELECT @pageIndex_1 = @pageIndex-1	
	
	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	DECLARE @insert varchar(1000)
		
	IF (@isSql2005 = 1) AND (@isSql2000 = 0)	--sql2005执行
	BEGIN
		if(@contributeSortType = 0)
		BEGIN
		SET	@sqlStr = 'SELECT * FROM (SELECT  u.pkId AS userId, u.userName AS userName, u.fullName AS fullName, u.tel AS tel, u.email AS email, '
				+ 'Count(n.pkId) AS articleNums,(select Count(*) from contribute where userId=u.pkId and updateTime>=''' + @beginTime+ ''' and updateTime<''' + @endTime + ''' and state<>5) AS totalNums, rank() over(order by Count(n.pkId) DESC, u.fullName ASC) AS ID , row_number() over(order by Count(n.pkId) DESC, u.fullName ASC) AS pkId '
				+ 'FROM Users u, news n, contribute c WHERE u.pkId = c.userId AND c.newsId = n.pkId AND c.state = 1 AND c.updateTime >= '''
				+ @beginTime + ''' AND c.updateTime <''' + @endTime + ''' GROUP BY u.pkId, userName, fullName, tel, email) '
				+ 'TempTable WHERE pkId > ' + STR(@pageIndex_1*@pageSize) + ' AND pkId<=' + STR(@pageIndex*@pageSize) + 'ORDER BY ID asc'
		END
		ELSE
		BEGIN
			SET	@sqlStr = 'SELECT * FROM (SELECT  u.pkId AS userId, u.userName AS userName, u.fullName AS fullName, u.tel AS tel, u.email AS email, '
				+ 'Count(n.pkId) AS articleNums,(select Count(*) from contribute where userId=u.pkId and updateTime>=''' + @beginTime+ ''' and updateTime<''' + @endTime + ''' and state<>5) AS totalNums, rank() over(order by (select Count(*) from contribute where userId=u.pkId and updateTime>=''' + @beginTime+ ''' and updateTime<''' + @endTime + ''' and state<>5) DESC, u.fullName ASC) AS ID , row_number() over(order by (select Count(*) from contribute where userId=u.pkId and updateTime>=''' + @beginTime+ ''' and updateTime<''' + @endTime + ''' and state<>5) DESC, u.fullName ASC) AS pkId '
				+ 'FROM Users u, news n, contribute c WHERE u.pkId = c.userId AND c.newsId = n.pkId AND c.state = 1 AND c.updateTime >= '''
				+ @beginTime + ''' AND c.updateTime <''' + @endTime + ''' GROUP BY u.pkId, userName, fullName, tel, email) '
				+ 'TempTable WHERE pkId > ' + STR(@pageIndex_1*@pageSize) + ' AND pkId<=' + STR(@pageIndex*@pageSize) + 'ORDER BY ID asc'
		END
	END
	ELSE IF (@isSql2005 = 0) AND (@isSql2000 =1) --sql2000执行
	BEGIN	
		SET @insert = 'SELECT Count(n.pkId) AS articleNums,(select Count(*) from contribute where userId=u.pkId and updateTime>=''' + @beginTime+ ''' and updateTime<''' + @endTime + ''' and state<>5) AS totalNums, u.pkId AS userId, u.userName AS userName, u.fullName AS fullName, u.tel AS tel, u.email AS email'
				+ ' into ' + @tempTable + ' FROM Users u, news n, contribute c WHERE u.pkId = c.userId AND c.newsId = n.pkId AND '
				+ 'c.state = 1 AND c.updateTime >= ''' + @beginTime + ''' AND c.updateTime <''' + @endTime + ''' GROUP BY u.pkId, userName, fullName, tel, email '
				+ 'ORDER BY '+@sortFiled+' DESC,  u.fullName ASC'	
		EXEC(@insert)
		 
		DECLARE @tempTable1 varchar(1000)
		SET @tempTable1 = 'TempC' + LTRIM(STR(YEAR(GetDate()))) + LTRIM(STR(MONTH(GetDate()))) + LTRIM(STR(DAY(GetDate()))) 
					+ LTRIM(STR(DATEPART(hh,GetDate()))) + LTRIM(STR(DATEPART(n,GetDate()))) + LTRIM(STR(DATEPART(ss,GetDate())))

		SET @insert = 'SELECT identity(int, 1, 1) as pkId , 1 as ID, * into ' + @tempTable1 + ' FROM  '+@tempTable
		EXEC(@insert)
		SET @insert = 'DROP TABLE ' + @tempTable	
		EXEC(@insert)		
		SET @insert = 'declare @id int, @nums int declare myCursor CURSOR FOR SELECT MIN(pkId) as Id, '+@sortFiled+' as nums FROM ' + @tempTable1 + ' GROUP BY '+@sortFiled+' ORDER BY Id asc '
				+ 'OPEN myCursor FETCH NEXT FROM myCursor INTO @id, @nums WHILE @@FETCH_STATUS = 0 BEGIN UPDATE ' + @tempTable1 + ' SET ID=@id WHERE '+@sortFiled+'=@nums '
				+ 'FETCH NEXT FROM myCursor INTO @id, @nums END CLOSE myCursor DEALLOCATE myCursor'
		EXEC(@insert)
		IF @pageIndex_1 <= 0
		BEGIN
			SET	@sqlStr = 'SELECT TOP ' + STR(@pageSize) + '* FROM ' + @tempTable1 + ' ORDER BY ID ASC, fullName ASC'
		END
		ELSE IF @pageIndex_1 >= @pageCount -1
		BEGIN
			SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM ' + @tempTable1 + ' ORDER BY ID DESC, fullName DESC) TempTable ORDER BY ID ASC, fullName ASC'
		END
		ELSE IF @pageIndex_1 > @pageCount/2
		BEGIN
			SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + '* FROM (SELECT TOP ' + STR(@recordCount - @pageIndex * @pageSize) + ' * FROM ' + @tempTable1
					+ ' ORDER BY ID DESC, fullName DESC) TempTable ORDER BY ID ASC, fullName ASC'
		END
		ELSE BEGIN
			SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + '* FROM (SELECT TOP ' + STR(@pageIndex * @pageSize) + ' * FROM ' + @tempTable1
					+ ' ORDER BY ID ASC, fullName ASC) TempTable ORDER BY ID DESC, fullName DESC) TempTable1 ORDER BY ID ASC, fullName ASC'
		END		
		SET @insert = 'DROP TABLE ' + @tempTable1
	END
	--执行查询
	EXEC(@sqlStr)
	
	EXEC(@insert)
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributeClassById]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributeClassById]
GO







CREATE PROCEDURE [dbo].[GetContributeClassById]
@pkId int
AS
	/* SET NOCOUNT ON */
	SELECT * FROM contributeClass where pkId=@pkId
	
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributePostilByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributePostilByPkId]
GO







--region [dbo].[GetContributePostilByPkId]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[GetContributePostilByPkId]
-- Date Generated: 2007骞?2鏈?7鏃?
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[GetContributePostilByPkId]
	@pkId int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	*
FROM
	[dbo].[ContributePostil]
WHERE
	[pkId] = @pkId

--endregion







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributesByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributesByPage]
GO






CREATE PROCEDURE [dbo].[GetContributesByPage]
	@pageIndex int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@recordCount int output,	-- 
	@contributeSortType int
AS
--转换成索引
	SELECT @pageIndex = @pageIndex-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000),@sortFiled varchar(50)

	if(@contributeSortType=0)
		SET @sortFiled = 'articleNums'
	Else
		SET @sortFiled = 'totalNums'

	SET @vSQL = N'SELECT @count = Count(*) FROM( SELECT Count(n.pkId) as nums, u.pkId as userId FROM news n,users u,department d,'
			+ 'contribute c where c.userId=u.pkId and u.departmentId=d.pkId and c.state=1 and c.newsId = n.pkId '
			+ 'group by u.pkId) TempTable'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum	

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int, @tempTable varchar(1000)

	SET @tempTable = 'TempB' + LTRIM(STR(YEAR(GetDate()))) + LTRIM(STR(MONTH(GetDate()))) + LTRIM(STR(DAY(GetDate()))) 
					+ LTRIM(STR(DATEPART(hh,GetDate()))) + LTRIM(STR(DATEPART(n,GetDate()))) + LTRIM(STR(DATEPART(ss,GetDate())))

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	DECLARE @insert varchar(1000)
	
	SET @insert = 'SELECT u.pkId as userId, u.userName as userName, '
					+ 'u.fullName as fullName, u.email as email, u.tel as tel, d.name as department, '
					+ 'Count(n.pkId) as articleNums,(select Count(*) from contribute where userId=u.pkId and state<>5) AS totalNums into ' + @tempTable + ' from news n,users u,department d, '
					+ 'contribute c where c.userId=u.pkId and u.departmentId=d.pkId and c.state=1 and '
					+ 'c.newsId = n.pkId Group by u.pkId, u.userName, u.fullName, u.tel, u.email, d.name '
					+ ' ORDER BY '+@sortFiled+' desc,  u.fullName ASC '
	EXEC(@insert)					
	--查询第一页
	IF (@pageIndex = 0) OR (@pageIndex <0)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM ' + @tempTable + ' ORDER by '+@sortFiled+' desc, fullName ASC'	
	--查询最后一页
	ELSE IF ((@pageIndex = @pageCount -1) OR (@pageIndex > @pageCount-1))			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * from ' + @tempTable + ' ORDER BY '+@sortFiled+' asc, fullName DESC) TempTable ORDER BY '+@sortFiled+' desc, fullName ASC'	
	--查询中间页后面的页
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' * from ' + @tempTable + ' ORDER BY '+@sortFiled+' asc, fullName DESC) TempTable ORDER BY '+@sortFiled+' desc, fullName ASC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' * from ' + @tempTable + ' ORDER BY '+@sortFiled+' desc, fullName ASC) TempTable1 '
					+ ' ORDER BY '+@sortFiled+' asc, fullName DESC) TempTable2 ORDER BY '+@sortFiled+' desc, fullName ASC'
	END

	--执行查询
	EXEC(@sqlStr)
	SET @insert = 'DROP TABLE ' + @tempTable
	EXEC(@insert)
RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributesByPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributesByPassed]
GO







CREATE PROCEDURE [dbo].[GetContributesByPassed]
	@gradeId int=1,
	@gateId int,
	@pageIndex int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@recordCount int output,	-- 
	@passed int	-- 
AS	
	--转换成索引
	SELECT @pageIndex = @pageIndex-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count=Count(*) from news n,users u,department d,contribute c where  n.gate<>'
			+Convert(nvarchar,@gateId)+' and c.userId=u.pkId and u.departmentId=d.pkId '
			+'and n.gradeId<='+convert(nvarchar,@gradeId)+' and n.pkId=c.newsId and c.state=1'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum	

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@pageIndex = 0) OR (@pageIndex <0)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + '  n.*,u.userName, u.fullName ,d.name "department", '
					+ 'b.name "BigClassName" from news n,users u,department d,bigClass b, contribute c '
					+ ' where  n.gate<>'+Convert(varchar,@gateId)+' and  c.userId=u.pkId and u.departmentId=d.pkId '
					+ 'and n.bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.pkId=c.newsId and c.state=1 '
					+ 'ORDER BY n.isTop DESC, n.pkId DESC'	
	--查询最后一页
	ELSE IF ((@pageIndex = @pageCount -1) OR (@pageIndex > @pageCount-1))			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName, u.fullName ,d.name "department", '
					+ 'b.name "BigClassName" from news n,users u,department d,bigClass b, contribute c '
					+ ' where  n.gate<>'+Convert(varchar,@gateId)+' and   c.userId=u.pkId and u.departmentId=d.pkId '
					+ 'and n.bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.pkId=c.newsId and c.state=1 '
					+ 'ORDER BY n.isTop DESC, n.pkId ASC) TempTable ORDER BY isTop DESC, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' n.*,u.userName, u.fullName ,d.name "department", '
					+ 'b.name "BigClassName" from news n,users u,department d,bigClass b, contribute c '
					+ ' where  n.gate<>'+Convert(varchar,@gateId)+' and   c.userId=u.pkId and u.departmentId=d.pkId '
					+ 'and n.bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.pkId=c.newsId and c.state=1 '
					+ 'ORDER BY n.isTop DESC, n.pkId ASC) TempTable ORDER BY isTop DESC,pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + '  n.*,u.userName, u.fullName ,d.name "department", '
					+ 'b.name "BigClassName" from news n,users u,department d,bigClass b, contribute c '
					+ ' where  n.gate<>'+Convert(varchar,@gateId)+' and   c.userId=u.pkId and u.departmentId=d.pkId '
					+ 'and n.bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.pkId=c.newsId and c.state=1 '
					+ 'ORDER BY n.isTop DESC, n.pkId DESC) TempTable1 '
					+ ' ORDER BY isTop ASC, pkId ASC) TempTable2 ORDER BY isTop DESC, pkId DESC'
	END
	print @sqlStr

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetContributesByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetContributesByUserId]
GO







CREATE PROCEDURE [dbo].[GetContributesByUserId]
	@pageIndex int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeid int=1,
	@gateId int,
	@userId int,
	@recordCount int output	-- 
AS	
	--转换成索引
	SELECT @pageIndex = @pageIndex-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(n.pkId) FROM news n, users u, grade g, contribute c, department d WHERE n.gradeId<=' + CONVERT(nvarchar,@gradeId) 
				+ N' AND c.userId=' + CONVERT(nvarchar,@userId) + N' AND n.gate<>' + CONVERT(nvarchar,@gateId) 
				+ N' AND n.gradeId=g.pkId and c.userId=u.pkId AND n.pkId = c.newsId AND u.departmentId = d.pkId'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--PRINT @vSQL
	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.*,u.userName "userName",g.name "grade", d.name "department"  , d.name "department" FROM news n, users u, grade g, contribute c, department d '
					+ 'WHERE n.gradeId<=' + STR(@gradeId) + ' AND c.userId=' + STR(@userId) + ' AND n.gate<>' + STR(@gateId) 
					+ 'AND n.gradeId=g.pkId and c.userId=u.pkId AND n.pkId = c.newsId AND u.departmentId = d.pkId ORDER BY n.isTop DESC, n.pkId DESC'	
	--查询最后一页
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.*,u.userName "userName",g.name "grade", d.name "department" FROM news n, users u, grade g, contribute c, department d '
					+ 'WHERE n.gradeId<=' + STR(@gradeId) + ' AND c.userId=' + STR(@userId) + ' AND n.gate<>' + STR(@gateId) 
					+ 'AND n.gradeId=g.pkId and c.userId=u.pkId AND n.pkId = c.newsId AND u.departmentId = d.pkId ORDER BY n.isTop ASC, n.pkId ASC) TempTable ORDER BY isTop DESC, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' n.*,u.userName "userName",g.name "grade", d.name "department" FROM news n, users u, grade g, contribute c, department d '
					+ 'WHERE n.gradeId<=' + STR(@gradeId) + ' AND c.userId=' + STR(@userId) + ' AND n.gate<>' + STR(@gateId) 
					+ 'AND n.gradeId=g.pkId and c.userId=u.pkId AND n.pkId = c.newsId AND u.departmentId = d.pkId ORDER BY n.isTop ASC, n.pkId ASC) TempTable ORDER BY isTop DESC, pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' n.*,u.userName "userName",g.name "grade", d.name "department" FROM news n, users u, grade g, contribute c, department d '
					+ 'WHERE n.gradeId<=' + STR(@gradeId) + ' AND c.userId=' + STR(@userId) + ' AND n.gate<>' + STR(@gateId) 
					+ 'AND n.gradeId=g.pkId and c.userId=u.pkId AND n.pkId = c.newsId AND u.departmentId = d.pkId ORDER BY n.isTop DESC, n.pkId DESC) TempTable1 '
					+ ' ORDER BY isTop ASC, pkId ASC) TempTable2 ORDER BY isTop DESC, pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetCount]
GO


CREATE procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> -1 and bigclassId=6 and gradeId<=1 and passed=1 and (validDays > 0 OR validDays < 0) return

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDepartmentByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetDepartmentByPkId]
GO








CREATE PROCEDURE [dbo].[GetDepartmentByPkId]
@pkId int
AS
	 SET NOCOUNT ON
	 select *
	 from department
	 where pkId=@pkId
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetdiscussPropertyByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetdiscussPropertyByPkId]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetdiscussPropertyByPkId] 
	@pkId int =null
AS 
	select	*
	from	discussProperty dP
	where	dP.discussId=@pkId





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDownloadFileBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetDownloadFileBypkId]
GO





CREATE PROCEDURE GetDownloadFileBypkId
	-- Add the parameters for the stored procedure here
	@pkId int
as
SELECT *
    from ResourceList
    where pkId = @pkId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDownloadFileListBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetDownloadFileListBypkId]
GO





CREATE PROCEDURE GetDownloadFileListBypkId
	@searchStr varchar(500),
	@pageIndex int,
	@pageSize int,
	@pageCount int output,
	@recordCount int output
AS 
   SELECT @pageIndex = @pageIndex - 1
	
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(ResourceList.pkId) FROM ResourceList,ResourceClick WHERE ' + @searchStr
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	SET @pageCount = (@recordCount + @pageSize-1)/@pageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' ResourceList.pkId,ResourceList.FileName,ResourceList.FilePath,ResourceList.FileSize,ResourceList.Grade,ResourceList.Subject,ResourceList.AutoType,ResourceClick.clicks FROM ResourceList,ResourceClick WHERE ' + @searchStr + ' ORDER BY ResourceClick.clicks DESC,ResourceList.pkId asc'	
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' ResourceList.pkId,ResourceList.FileName,ResourceList.FilePath,ResourceList.FileSize,ResourceList.Grade,ResourceList.Subject,ResourceList.AutoType,ResourceClick.clicks FROM ResourceList,ResourceClick WHERE ' + @searchStr + ' ORDER BY Resourceclick.clicks ASC,ResourceList.pkId DESC) TempTable ORDER BY clicks DESC,pkId asc'	
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' ResourceList.pkId,ResourceList.FileName,ResourceList.FilePath,ResourceList.FileSize,ResourceList.Grade,ResourceList.Subject,ResourceList.AutoType,ResourceClick.clicks FROM ResourceList,ResourceClick WHERE ' + @searchStr + ' ORDER BY Resourceclick.clicks ASC,ResourceList.pkId DESC) TempTable ORDER BY clicks DESC,pkId asc'
	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' ResourceList.pkId,ResourceList.FileName,ResourceList.FilePath,ResourceList.FileSize,ResourceList.Grade,ResourceList.Subject,ResourceList.AutoType,ResourceClick.clicks FROM ResourceList,ResourceClick WHERE ' + @searchStr + ' ORDER BY Resourceclick.clicks DESC,ResourceList.pkId ASC) TempTable1 '
					+ ' ORDER BY clicks ASC,pkId DESC) TempTable2 ORDER BY clicks DESC,pkId asc'
	END
	EXEC(@sqlStr)
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDuplicateBigClassByUserIdAndTemplateId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetDuplicateBigClassByUserIdAndTemplateId]
GO

create PROCEDURE GetDuplicateBigClassByUserIdAndTemplateId
(
	@userId int,
	@templateId int
)
AS

	--判断当前用户是否属于用户组
	Declare @groupid int 
	select @groupid = usersGroupId from users where pkid=@userId
	if(@groupid=1)
	begin
	--用户权限
	select distinct b.pkId, b.name, b.templateId as templateId, t.name as templateName from bigClass b, moduleAssign m, users u, menu e, template t
	where m.usersid = u.pkId and m.menuId=e.pkId and e.bigClassId=b.pkId and b.templateId = t.pkId 
		and b.templateId=@templateId and u.pkId = @userId
	order by b.pkId

	end
	else
	begin
	--用户组权限
	select distinct b.pkId, b.name, b.templateId as templateId, t.name as templateName from bigClass b, groupAssign m, users u, menu e, template t
	where m.groupid = u.usersGroupId and m.menuId=e.pkId and e.bigClassId=b.pkId and b.templateId = t.pkId 
		and b.templateId=@templateId and u.pkId = @userId
	order by b.pkId
	end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetGradeByAdmin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetGradeByAdmin]
GO







CREATE PROCEDURE [dbo].[GetGradeByAdmin]
AS
	SET NOCOUNT ON
	select top 1 * from grade order by pkId desc

	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetGradeByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetGradeByPkId]
GO







CREATE PROCEDURE [dbo].[GetGradeByPkId]
@pkId int=null
AS
	 sET NOCOUNT ON 
	 select * from grade where pkId=@pkId
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetGroupAssignByGroupId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetGroupAssignByGroupId]
GO



create PROCEDURE GetGroupAssignByGroupId
@groupId int=null
AS

select * from groupAssign where groupId=@groupId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetImageNewsByRecommand]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetImageNewsByRecommand]
GO



CREATE PROCEDURE GetImageNewsByRecommand
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
	 SET NOCOUNT ON 
	
select top 1 pkId
	,specialId
	,gradeId
	,gate
	,departmentId
	,listTitle
	,intactTitle
	,subheading
	,isRecommand
	,isTop
	,useListTitle
	,showReviewLink
	,Author
	,AuthorMail
	,CopyFrom
	,copyFromLink
	,about
	,pageType
	,autoPageCharNum
	,readPoint
	,itemType
	,descript
	,usersId
	,editor
	,click
	,updateTime
	,passed
	,isReview
	,isImageNews
	,imageName
	,width
	,height
	,MenuId
	,bigClassId
	,updateIp
	,useSubHeading
	,isDuplicated
	,duplicateFrom
	,duplicatedPkId
	,validDays
	,mediaAutostart
	,AuditUserID from news 
where validDays <> 0 and passed=1 
and isRecommand=1  
and IsImageNews=1 
and gate<>@gateId
and gradeId<=@gradeId
order by istop desc,pkId desc

RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetIncludePageByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetIncludePageByBigClassId]
GO







CREATE PROCEDURE [dbo].[GetIncludePageByBigClassId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int

AS
	 SET NOCOUNT ON	 
	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' m.*,u.userName "userName",g.name "grade" from IncludePage m,users u,grade g where  m.gate<>'+Convert(varchar,@gateId)+' and  m.bigClassId='+Convert(varchar,@bigClassId)+' and m.gradeId<='+convert(varchar,@gradeId)+' and m.gradeId=g.pkId and m.userId=u.pkId  and m.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from IncludePage  where bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' order by istop desc,pkId desc)  order by istop desc,m.pkId desc'
Execute (@vSQL)
	
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetIncludePageByBigClassIdAndUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetIncludePageByBigClassIdAndUserId]
GO







CREATE PROCEDURE [dbo].[GetIncludePageByBigClassIdAndUserId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@userId int,
@gateId int
AS
	 SET NOCOUNT ON	 
	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' m.*,u.userName "userName",g.name "grade" from IncludePage m,users u,grade g where m.gate<>'+Convert(varchar,@gateId)+' and   m.bigClassId='+Convert(varchar,@bigClassId)+' and m.gradeId<='+convert(varchar,@gradeId)+' and m.userId='+convert(varchar,@userId)+' and m.gradeId=g.pkId and m.userId=u.pkId  and m.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from IncludePage  where bigClassId='+Convert(varchar,@bigClassId)+' and userId='+convert(varchar,@userId)+' and gradeId<='+convert(varchar,@gradeId)+' order by istop desc,pkId desc)  order by istop desc,m.pkId desc'
Execute (@vSQL)
	
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetInfoList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetInfoList]
GO






-- =============================================
-- Author:		<Yinyf>
-- Create date: <2007-11-23>
-- Description:	<获取具体信息查询栏目对应的表数据>
-- =============================================
CREATE PROCEDURE [dbo].[GetInfoList]
	@TableName varchar(255),
	@PageSize int = 20,
	@PageIndex int = 1,
	@PageCount int output,
	@Recordcount int output
AS	
	SELECT @PageIndex = @PageIndex - 1
	
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) FROM ' + @TableName
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @Recordcount = @tempNum

	SET @PageCount = (@Recordcount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @Recordcount%@PageSize	
	IF(@size=0)
		SET @size = @PageSize	

	IF (@PageIndex = 0) OR (@PageIndex > @PageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM ' + @TableName + ' ORDER BY pkId ASC'	
	ELSE IF @PageIndex = @PageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM ' + @TableName + ' ORDER BY pkId DESC) TempTable ORDER BY pkId ASC'	
	ELSE IF @PageIndex > @PageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@Recordcount - @PageSize * @PageIndex ) + ' * FROM ' + @TableName + ' ORDER BY pkId DESC) TempTable ORDER BY pkId ASC'
	ELSE
	BEGIN							
		SET @PageIndex=@PageIndex+1
		SET @sqlStr = '	SELECT * FROM (	SELECT TOP ' + STR(@PageSize) + ' * 
										FROM (	SELECT TOP ' + STR(@PageSize * @PageIndex ) + ' * 
												FROM ' + @TableName + ' 
												ORDER BY pkId ASC
											 ) TempTable1 ' + ' 
										ORDER BY pkId DESC
									  ) TempTable2 
						ORDER BY pkId ASC'
	END
	EXEC(@sqlStr)
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetInfoSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetInfoSearch]
GO






-- =============================================
-- Author:		<Yinyf>
-- Create date: <2007-11-23>
-- Description:	<获取具体信息查询栏目对应的表信息>
-- =============================================
CREATE PROCEDURE GetInfoSearch 
	@BigClassID int 
AS
BEGIN
	SET NOCOUNT ON;
	SELECT TOP 1 * FROM InfoSearch WHERE BigClassID = @BigClassID
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetInfoSearchByNewsId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetInfoSearchByNewsId]
GO




-- =============================================
-- Author:		<Hezl>
-- Create date: <2015-09-22>
-- Description:	<获取具体信息查询栏目对应的表信息>
-- =============================================
create PROCEDURE GetInfoSearchByNewsId 
	@NewsID int 
AS
BEGIN
	SET NOCOUNT ON;
	SELECT TOP 1 * FROM InfoSearch WHERE newsId = @NewsID
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetIsFlippy]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetIsFlippy]
GO






CREATE PROCEDURE [dbo].[GetIsFlippy]
(@bigClassId int)
AS
	SET NOCOUNT ON;
SELECT * FROM bigClass
where pkId = @bigClassId and templateId = 25





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLatestGameByPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLatestGameByPassed]
GO







CREATE PROCEDURE [dbo].[GetLatestGameByPassed]
	@pageIndex int,
	@pageSize int=5,
	@gradeid int=1,
	@passed int=1,
	@gateId int,	
	@recordCount int ,
	@pageCount int 
AS	
	--转换成索引
	SELECT @pageIndex=@pageIndex-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(*) FROM News WHERE bigclassId in (select bigclassId from bigClass where templateId in (49,50,53)) and gate<>'+Convert(varchar,@gateId)+' and gradeId<='+convert(varchar,@gradeId)+' and passed='+convert(varchar,@passed)+' and (isDuplicated=0 or isDuplicated is null) and  isImageNews=0'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + '  n.*,u.userName,d.name "department",b.name "BigClassName" from news n,users u,department d,bigClass b where b.templateId in (49,53) and n.gate<>'+Convert(varchar,@gateId)+' and (n.isDuplicated=0 or n.isDuplicated is null) and n.usersId=u.pkId and n.departmentId=d.pkId and bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  ORDER BY n.updateTime DESC, n.pkId DESC'	
	--查询最后一页
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' +STR(@size)  + '  n.*,u.userName,d.name "department",b.name "BigClassName" from news n,users u,department d,bigClass b where b.templateId in (49,53) and  n.gate<>'+Convert(varchar,@gateId)+' and (n.isDuplicated=0 or n.isDuplicated is null) and n.usersId=u.pkId and n.departmentId=d.pkId and bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  ORDER BY n.updateTime ASC, n.pkId ASC) TempTable ORDER BY updateTime DESC, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP '  + STR(@recordCount - @pageSize * @pageIndex ) + '  n.*,u.userName,d.name "department",b.name "BigClassName" from news n,users u,department d,bigClass b where b.templateId in (49,53) and n.gate<>'+Convert(varchar,@gateId)+' and (n.isDuplicated=0 or n.isDuplicated is null) and n.usersId=u.pkId and n.departmentId=d.pkId and bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  ORDER BY n.updateTime ASC, n.pkId ASC) TempTable ORDER BY updateTime DESC, pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' + STR(@pageSize * @pageIndex ) + '  n.*,u.userName,d.name "department",b.name "BigClassName" from news n,users u,department d,bigClass b where b.templateId in (49,53) and n.gate<>'+Convert(varchar,@gateId)+' and (n.isDuplicated=0 or n.isDuplicated is null) and n.usersId=u.pkId and n.departmentId=d.pkId and bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  ORDER BY n.updateTime DESC, n.pkId DESC) TempTable1 ORDER BY updateTime ASC, pkId ASC) TempTable2 ORDER BY updateTime DESC, pkId DESC'
	END

	--查询具有图片的两个最新游戏
		set @sqlStr = @sqlStr + ' SELECT TOP 2  n.*,u.userName,d.name "department",b.name "BigClassName" from news n,users u,department d,bigClass b where b.templateId = 50 and n.gate<>'+Convert(varchar,@gateId)+' and (n.isDuplicated=0 or n.isDuplicated is null) and n.usersId=u.pkId and n.departmentId=d.pkId and bigClassId=b.pkId and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  ORDER BY n.updateTime DESC, n.pkId DESC'
	
	--执行查询
	EXEC(@sqlStr)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLatestMessageBoardByPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLatestMessageBoardByPassed]
GO







CREATE PROCEDURE [dbo].[GetLatestMessageBoardByPassed]
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@passed int=1,
@gateId int
AS
	 SET NOCOUNT ON 
	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' m.*, b.name "BigClassName"  from messageBoard m, bigClass b where m.bigClassId=b.pkId and m.gate<>'+Convert(varchar,@gateId)+' and  m.gradeId<='+convert(varchar,@gradeId)+' and m.checked='+convert(varchar,@passed)+' and m.isSecrecy =0  and m.pkId < all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from messageBoard where  gate<>'+Convert(varchar,@gateId)+' and   gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy=0 order by istop desc,pkId desc)  order by m.istop desc,m.pkId desc'
Execute (@vSQL)
	
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLatestNewsByPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLatestNewsByPassed]
GO

CREATE PROCEDURE GetLatestNewsByPassed
	@pageIndex int,
	@pageSize int=5,
	@gradeid int=1,
	@passed int=1,
	@gateId int,
	@menuId int,
	@sortType int
AS

BEGIN

	SET @pageIndex = @pageIndex - 1
	IF (@pageIndex < 0)
		BEGIN
			SET @pageIndex = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @pageIndex
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
	
    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

	DECLARE @num int
	select @num=0
	select @num = count(pkId) from BigClassAssociation where thisBigClassId=@menuId
	if(@num = 0)
	begin

	if(@pageIndex=0) --优化首页访问速度
	begin
		--增加排序方式判断
		if(@sortType=1)
		begin
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT  TOP (@pageSize)  n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					n.gradeId <=		@gradeId and 
					n.passed =			@passed and 
					isDuplicated != 1 and
					(n.validDays > 0 OR n.validDays < 0) and
					(n.albumid=0 OR n.albumid is null)  
			ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
		end
		else
		begin
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT  TOP (@pageSize)  n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					n.gradeId <=		@gradeId and 
					n.passed =			@passed and 
					isDuplicated != 1 and
					(n.validDays > 0 OR n.validDays < 0)  and
					(n.albumid=0 OR n.albumid is null) 
			ORDER BY n.istop desc,n.updateTime desc,n.Priority desc,n.pkid desc
		end
	end
	else
	begin
		if(@sortType=1)
		begin
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT	n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					n.gradeId <=		@gradeId and 
					n.passed =			@passed and 
					isDuplicated != 1 and
					(n.validDays > 0 OR n.validDays < 0) and
					(n.albumid=0 OR n.albumid is null)  
			ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
		end
		else
		begin
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT	n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					n.gradeId <=		@gradeId and 
					n.passed =			@passed and 
					isDuplicated != 1 and
					(n.validDays > 0 OR n.validDays < 0)  and
					(n.albumid=0 OR n.albumid is null) 
			ORDER BY n.istop desc,n.updateTime desc,n.Priority desc,n.pkid desc
		end
	end	
	end
	else
	begin
		if(@pageIndex=0) --优化首页访问速度
		begin
			if(@sortType=1)
			begin
				-- Insert into our temp table
				INSERT	INTO #PageIndexs (ID)
				SELECT TOP (@pageSize) n.pkId
				FROM	[News] AS n 
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
						n.gradeId <=		@gradeId and 
						n.passed =			@passed and 
						isDuplicated = 0 and
						(n.validDays > 0 OR n.validDays < 0) and	
						exists (select 1 from BigClassAssociation ba where ba.bigclassid = n.bigclassid and  ba.thisBigclassId=@menuId) 
						--n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) 
						  
				ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
			end
			else
			begin
				-- Insert into our temp table
				INSERT	INTO #PageIndexs (ID)
				SELECT TOP (@pageSize) n.pkId
				FROM	[News] AS n 
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
						n.gradeId <=		@gradeId and 
						n.passed =			@passed and 
						isDuplicated = 0 and
						(n.validDays > 0 OR n.validDays < 0)  and
						exists (select 1 from BigClassAssociation ba where ba.bigclassid = n.bigclassid and  ba.thisBigclassId=@menuId) 
						--n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId)	
						 
				ORDER BY n.istop desc,n.updateTime desc,n.Priority desc,n.pkid desc
			end
		end
		else
		begin

			if(@sortType=1)
			begin
				-- Insert into our temp table
				INSERT	INTO #PageIndexs (ID)

				SELECT	n.pkId
				FROM	[News] AS n 
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
						n.gradeId <=		@gradeId and 
						n.passed =			@passed and 
						isDuplicated = 0 and
						(n.validDays > 0 OR n.validDays < 0) and	
						exists (select 1 from BigClassAssociation ba where ba.bigclassid = n.bigclassid and  ba.thisBigclassId=@menuId) 
						--n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) 
						 
				ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
			end
			else
			begin
				-- Insert into our temp table
				INSERT	INTO #PageIndexs (ID)

				SELECT	n.pkId
				FROM	[News] AS n 
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
						n.gradeId <=		@gradeId and 
						n.passed =			@passed and 
						isDuplicated = 0 and
						(n.validDays > 0 OR n.validDays < 0)  and
						exists (select 1 from BigClassAssociation ba where ba.bigclassid = n.bigclassid and  ba.thisBigclassId=@menuId) 
						--n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) 	
						
				ORDER BY n.istop desc,n.updateTime desc,n.Priority desc,n.pkid desc
			end
		end
	end

	if(@sortType=1)
	begin

		SELECT  n.pkId ,n.specialId, n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
					,n.isRecommand	,n.isTop, n.useListTitle ,n.showReviewLink, n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
					,n.about ,n.pageType ,n.autoPageCharNum	,n.readPoint ,n.itemType ,n.descript ,n.usersId	,n.editor
					,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews	,n.imageName ,n.width ,n.height
					,n.MenuId ,n.bigClassId	,n.updateIp	,n.useSubHeading,n.isDuplicated	,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
					,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department ,b.name as BigClassName
		FROM	[News] AS n
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				INNER JOIN [BigClass] AS b ON n.bigClassId=b.pkId
				INNER JOIN [Template] AS t ON t.pkId = b.templateId
				INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
		WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
		ORDER BY n.updateTime desc,n.istop desc ,n.Priority desc,n.pkid desc
	end
	else
	begin
		SELECT  n.pkId ,n.specialId, n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand	,n.isTop, n.useListTitle ,n.showReviewLink, n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum	,n.readPoint ,n.itemType ,n.descript ,n.usersId	,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews	,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId	,n.updateIp	,n.useSubHeading,n.isDuplicated	,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department ,b.name as BigClassName
		FROM	[News] AS n
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				INNER JOIN [BigClass] AS b ON n.bigClassId=b.pkId
				INNER JOIN [Template] AS t ON t.pkId = b.templateId
				INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
		WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
		ORDER BY n.istop desc ,n.updateTime desc,n.Priority desc,n.pkid desc
	end

	DELETE #PageIndexs
	RETURN 
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLatestScrollFlashNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLatestScrollFlashNews]
GO

CREATE PROCEDURE GetLatestScrollFlashNews
	@pageIndex int,
	@pageSize int=5,
	@gradeid int=1,
	@passed int=1,
	@gateId int,
	@menuId int
AS

BEGIN

	SET @pageIndex = @pageIndex - 1
	IF (@pageIndex < 0)
		BEGIN
			SET @pageIndex = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @pageIndex
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
	
    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

	DECLARE @num int
	select @num=0
	select @num = count(pkId) from BigClassAssociation where thisBigClassId=@menuId
	if(@num = 0)
	begin

	if(@pageIndex=0) --优化首页访问速度
	begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)
		SELECT  TOP (@pageSize)  n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				isDuplicated != 1 and
				(n.validDays > 0 OR n.validDays < 0) and
					(n.albumid=0 OR n.albumid is null) and
n.imageName!='' and CHARINDEX('/Images/uploadfiles/nopic.gif',n.imageName)=0
		ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
	end
	else
	begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)
		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				isDuplicated != 1 and
				(n.validDays > 0 OR n.validDays < 0)   and
					(n.albumid=0 OR n.albumid is null) and
				n.imageName!='' and CHARINDEX('/Images/uploadfiles/nopic.gif',n.imageName)=0
		ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
	end	
	end
	else
	begin
		if(@pageIndex=0) --优化首页访问速度
		begin
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT TOP (@pageSize) n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					n.gradeId <=		@gradeId and 
					n.passed =			@passed and 
					n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) and	
					isDuplicated != 1 and
					(n.validDays > 0 OR n.validDays < 0)  and
					n.imageName!='' and CHARINDEX('/Images/uploadfiles/nopic.gif',n.imageName)=0 
			ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
		end
		else
		begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)

		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) and	
				isDuplicated != 1 and
				(n.validDays > 0 OR n.validDays < 0) and
				n.imageName!='' and CHARINDEX('/Images/uploadfiles/nopic.gif',n.imageName)=0  
		ORDER BY n.updateTime desc,n.istop desc,n.Priority desc,n.pkid desc
		end
	end

	SELECT  n.pkId ,n.specialId, n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand	,n.isTop, n.useListTitle ,n.showReviewLink, n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum	,n.readPoint ,n.itemType ,n.descript ,n.usersId	,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews	,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId	,n.updateIp	,n.useSubHeading,n.isDuplicated	,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department ,b.name as BigClassName
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [BigClass] AS b ON n.bigClassId=b.pkId
			INNER JOIN [Template] AS t ON t.pkId = b.templateId
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.updateTime desc,n.istop desc ,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLayoutTemplateByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLayoutTemplateByAdminId]
GO







CREATE PROCEDURE [dbo].[GetLayoutTemplateByAdminId]
@adminId int
AS
	SET NOCOUNT ON;
SELECT pkId,name,isSystem,layoutTemplateType,adminId FROM layoutTemplate
WHERE isSystem=1
UNION
SELECT pkId,name,isSystem,layoutTemplateType,adminId FROM layoutTemplate 
WHERE isSystem=0
and   @adminId=adminId
order by isSystem desc,pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLayoutTemplateByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLayoutTemplateByBigClassId]
GO







CREATE PROCEDURE [dbo].[GetLayoutTemplateByBigClassId]
@bigClassId int
AS
	/* SET NOCOUNT ON */
	select l.*
	      from layoutTemplate l,menu m
	where m.bigClassId=@bigClassId
	  and m.layoutTemplateId=l.pkId
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLayoutTemplateByMenuId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLayoutTemplateByMenuId]
GO







CREATE PROCEDURE [dbo].[GetLayoutTemplateByMenuId]
@menuId int
AS
	/* SET NOCOUNT ON */
	select l.* from layoutTemplate l,menu m where m.pkId=@menuId and m.layoutTemplateId=l.pkId
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLayoutTemplateByName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLayoutTemplateByName]
GO








CREATE PROCEDURE [dbo].[GetLayoutTemplateByName]
	@name varchar(100),
	@type int
AS
	SET NOCOUNT ON;
SELECT * FROM layoutTemplate where [name] = @name and [layoutTemplateType]=@type







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLayoutTemplateByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLayoutTemplateByPkId]
GO







CREATE PROCEDURE [dbo].[GetLayoutTemplateByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM layoutTemplate where @pkId=pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLibrariesTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLibrariesTemplate]
GO



CREATE PROCEDURE GetLibrariesTemplate
AS
	SET NOCOUNT ON;
SELECT * FROM librariesTemplate
order by isSystem desc,pkId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLibrariesTemplateByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLibrariesTemplateByAdminId]
GO



CREATE PROCEDURE GetLibrariesTemplateByAdminId
@adminId int
AS
	SET NOCOUNT ON;
select pkId,name,isSystem,adminId from librariesTemplate
where isSystem=1
union
SELECT pkId,name,isSystem,adminId FROM librariesTemplate
where isSystem=0
and @adminId=adminId
order by isSystem desc,pkId



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLibrariesTemplateByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLibrariesTemplateByPkId]
GO







CREATE PROCEDURE [dbo].[GetLibrariesTemplateByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM librariesTemplate
where @pkId=pkId
order by isSystem desc,pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLinkByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLinkByBigClassId]
GO






CREATE PROCEDURE [dbo].[GetLinkByBigClassId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
	SET NOCOUNT ON 
	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @pageSize * @currentPage
    SET @PageUpperBound = @pageSize - 1 + @PageLowerBound
	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)

	SELECT	l.pkId
	FROM	[link] AS l 
			INNER JOIN [Users] AS u ON l.userId = u.pkId
			INNER JOIN [grade] AS g ON l.gradeId=g.pkId
	WHERE	(l.gate > @gateId OR l.gate < @gateId) and   
			l.gradeId <=	@gradeId and 
			l.bigClassId = @bigClassId 

	ORDER BY l.istop desc,l.Priority desc,l.pkid desc

	SELECT  l.*,u.fullname as userName,g.name as grade 
	FROM	[link] AS l 
			INNER JOIN [Users] AS u ON l.userId = u.PKID
			INNER JOIN [grade] AS g ON l.gradeId=g.pkId
			INNER JOIN [#PageIndexs] AS p ON l.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY l.istop desc ,l.Priority desc,l.pkid desc

	DELETE #PageIndexs


RETURN 









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLinkGangsByGroupId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLinkGangsByGroupId]
GO



CREATE PROCEDURE GetLinkGangsByGroupId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeid int=1,
	@gateId int,
	@groupId int,
	@recordCount int output	-- 修改方法，返回记录数 王敬红20060404
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(l.pkId) FROM linkGang l, users u, grade g WHERE l.gradeId<=' + CONVERT(nvarchar,@gradeId) 
				+ N' AND l.groupId=' + CONVERT(nvarchar,@groupId) + N' AND l.gate<>' + CONVERT(nvarchar,@gateId) 
				+ ' AND l.gradeId=g.pkId and l.userId=u.pkId'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.gate<>' + STR(@gateId) 
					+ 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop DESC,l.priority desc, l.pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.gate<>' + STR(@gateId) 
					+ 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop ASC,l.priority asc, l.pkId ASC) TempTable ORDER BY isTop DESC,priority desc, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.gate<>' + STR(@gateId) 
					+ 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop ASC,l.priority asc, l.pkId ASC) TempTable ORDER BY isTop DESC,priority desc, pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.gate<>' + STR(@gateId) 
					+ 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop DESC,l.priority DESC, l.pkId DESC) TempTable1 '
					+ ' ORDER BY isTop ASC,priority asc, pkId ASC) TempTable2 ORDER BY isTop DESC,priority desc, pkId DESC'
	END
	--执行查询
	EXEC(@sqlStr)
RETURN



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLinkGangsByGroupIdAndUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLinkGangsByGroupIdAndUserId]
GO



create PROCEDURE GetLinkGangsByGroupIdAndUserId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeid int=1,
	@gateId int,
	@groupId int,
	@userId int,
	@recordCount int output	-- 修改方法，返回记录数 王敬红20060404
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(l.pkId) FROM linkGang l, users u, grade g WHERE l.gradeId<=' + CONVERT(nvarchar,@gradeId) 
				+ N' AND l.groupId=' + CONVERT(nvarchar,@groupId) + N' AND l.userId=' + CONVERT(nvarchar,@userId) 
				+ N' AND l.gate<>' + CONVERT(nvarchar,@gateId) + ' AND l.gradeId=g.pkId and l.userId=u.pkId'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
		
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.userId=' + STR(@userId) 
					+ ' AND l.gate<>' + STR(@gateId) + 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop DESC,priority desc, l.pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1	
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.userId=' + STR(@userId) 
					+ ' AND l.gate<>' + STR(@gateId) + 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop ASC,l.priority asc, l.pkId ASC) TempTable ORDER BY isTop DESC,priority desc, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.userId=' + STR(@userId) 
					+ ' AND l.gate<>' + STR(@gateId) + 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop ASC,l.priority asc, l.pkId ASC) TempTable ORDER BY isTop DESC,priority desc, pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' l.*,u.userName,u.fullName "fullName",g.name "grade"  FROM linkGang l, users u, grade g '
					+ 'WHERE l.gradeId<=' + STR(@gradeId) + ' AND l.groupId=' + STR(@groupId) + ' AND l.userId=' + STR(@userId) 
					+ ' AND l.gate<>' + STR(@gateId) + 'AND l.gradeId=g.pkId and l.userId=u.pkId ORDER BY l.isTop DESC,l.priority desc, l.pkId DESC) TempTable1 '
					+ ' ORDER BY isTop ASC,priority asc, pkId ASC) TempTable2 ORDER BY isTop DESC,priority desc, pkId DESC'
	END
	--执行查询
	EXEC(@sqlStr)
RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLinkGroupByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLinkGroupByBigClassId]
GO










CREATE PROCEDURE [dbo].[GetLinkGroupByBigClassId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=7,
@gradeid int=1,
@gateId int

AS
	SET NOCOUNT ON	
	DECLARE @vSQL varchar(1000)
	SELECT @vSQL = 'select top ' + convert(varchar, @pageSize) + 
   		' l.*,u.userName "userName",g.name "grade" from linkGroup l,users u,grade g where  l.gate<>'+Convert(varchar,@gateId) + 
		--' and l.isShow = 1' + 
		' and l.bigClassId=' + Convert(varchar,@bigClassId) +
		' and l.gradeId<=' + convert(varchar,@gradeId) +
		' and l.gradeId=g.pkId and l.userId=u.pkId  and l.pkId <> all (select top ' + Convert(varchar,((@currentPage-1)*@pageSize)) + ' pkId from linkGroup  where bigClassId=' + Convert(varchar,@bigClassId) + 
		' and gradeId<=' + convert(varchar,@gradeId) + ' order by ordering desc,pkId desc)  order by ordering desc,l.priority desc,l.pkId desc'
	
	Execute (@vSQL)	
	RETURN 









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLockIpByIp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLockIpByIp]
GO







CREATE PROCEDURE [dbo].[GetLockIpByIp]
@ip float=null
AS
	SET NOCOUNT ON
	select * from lockIp where @ip<=ip2 and @ip>=ip1
RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetLockIpByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetLockIpByPkId]
GO







CREATE PROCEDURE [dbo].[GetLockIpByPkId]
@pkId int

AS
	SET NOCOUNT ON
	select * 
	  from lockIp
	  where pkId=@pkId
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuAndGate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuAndGate]
GO

CREATE PROCEDURE GetMenuAndGate
@gradeId int=1,
@gate int
AS
	/* SET NOCOUNT ON */
	SELECT m.*,
	       g.name grade,
	       o.name mode
	FROM menu m,grade g,mode o
	where m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and m.bigClassId<>-1
	  and m.modeid=4

	order by ordering desc
	RETURN 



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuAssign]
GO



create PROCEDURE GetMenuAssign
@gradeId int=1,
@userId int

AS

Declare @groupid int 

select @groupid = usersGroupId from users where pkid=@userId

if(@groupid=1)
begin
	SELECT m.*, g.name grade, o.name mode
	FROM menu m,grade g,mode o,moduleAssign a
	where m.gradeId<=@gradeId
	  and m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and a.usersId=@userId
	  and m.pkId=a.menuId
	order by ordering desc
end
else
begin
	SELECT m.*, g.name grade, o.name mode
	FROM menu m,grade g,mode o,groupAssign a
	where m.gradeId<=@gradeId
	  and m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and a.groupId=@groupid
	  and m.pkId=a.menuId
	order by ordering desc
end




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByBigClassId]
GO







CREATE PROCEDURE [dbo].[GetMenuByBigClassId]
	@bigClassId int
AS
	SELECT	m.*, t.code 
	FROM	menu m, bigClass b, template t 
	where	m.bigClassId=b.pkId and 
			b.templateId=t.pkId and 
			m.bigClassId=@bigClassId
	
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByBigClassIdA]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByBigClassIdA]
GO






create PROCEDURE [dbo].[GetMenuByBigClassIdA]
@bigClassId int
AS
BEGIN
	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int,
		NAVID int
    )

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID,NAVID) 
	select parentid, navigate from menu where bigclassid = @bigClassId


	SELECT pkid,parentid,name,address,P.NAVID as navigate,layouttemplateid 
	FROM menu AS m INNER JOIN [#PageIndexs] AS p ON m.PKID = p.ID 
	
	DELETE #PageIndexs
	RETURN

END 





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByBigClassidAndLayouttemplateid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByBigClassidAndLayouttemplateid]
GO






CREATE PROCEDURE [dbo].[GetMenuByBigClassidAndLayouttemplateid]
(
@bigClassId int,
@layouttemplateid int
)
as

select * from menu where bigclassid=@bigClassId and layouttemplateid=@layouttemplateid




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByBigClassidAndNavigate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByBigClassidAndNavigate]
GO







create PROCEDURE [dbo].[GetMenuByBigClassidAndNavigate]
(
@bigClassId int,
@navigate int
)
as

select * from menu where bigclassid=@bigClassId and navigate=@navigate 




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByParentId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByParentId]
GO




CREATE PROCEDURE [dbo].[GetMenuByParentId]
@parentId int
AS
	/* SET NOCOUNT ON */
	SELECT m.*, g.name grade, o.name mode FROM menu m,grade g,mode o where m.gradeId=g.pkId and m.modeId=o.pkId and m.parentId=@parentId and m.bigClassId<>-1 order by ordering DESC ,m.pkId 
	RETURN 





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByParentIdAndAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByParentIdAndAdminId]
GO



CREATE PROCEDURE GetMenuByParentIdAndAdminId
@parentId int,
@adminId int

AS
	/* SET NOCOUNT ON */
	SELECT m.*,g.name grade, o.name mode FROM menu m,grade g,mode o,adminAssign a where a.menuId=m.pkId
	  and m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and a.adminId=@adminId
	  and m.parentId=@parentId
	  and m.bigClassId<>-1 order by ordering desc ,m.pkId
	RETURN 




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByParentIdAndGradeIdAndGate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByParentIdAndGradeIdAndGate]
GO







CREATE PROCEDURE [dbo].[GetMenuByParentIdAndGradeIdAndGate]
@parentId int,
@gradeId int=1,
@gate int
AS
	/* SET NOCOUNT ON */
	SELECT m.*,
	       g.name grade,
	       o.name mode
	FROM menu m,grade g,mode o
	where m.gradeId<=@gradeId
	  and m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and m.parentId=@parentId
	  and m.bigClassId<>-1
	  and m.gate<>@gate
	order by ordering  desc
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByPkIdAndGradeId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByPkIdAndGradeId]
GO





CREATE PROCEDURE [dbo].[GetMenuByPkIdAndGradeId]
@gradeId int=1,
@pkId int
AS
	/* SET NOCOUNT ON */
	SELECT m.*,g.name grade,o.name mode FROM menu m,grade g,mode o
	where m.gradeId<=@gradeId
	  and m.gradeId=g.pkId
	  and m.modeId=o.pkId
	  and m.pkId=@pkId

RETURN 





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuByRegServiceId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuByRegServiceId]
GO








create PROCEDURE [dbo].[GetMenuByRegServiceId]
@regServiceId int
AS
	/* SET NOCOUNT ON */
	SELECT b.*,
	       b.pkId "bigClassId"
	FROM bigClass b,ServiceClass c
	where b.pkId=c.bigClassId
	  and c.RegServiceListId=@regServiceId
	 
	order by c.pkId  desc
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuIDByArtDesignTemplateID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuIDByArtDesignTemplateID]
GO




-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE GetMenuIDByArtDesignTemplateID

	@ArtDesignTemplateID	int

AS
BEGIN
	
	SET NOCOUNT ON;

	SELECT PKID FROM Menu WHERE ArtDesignTemplateID = @ArtDesignTemplateID
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuSet]
GO







CREATE PROCEDURE [dbo].[GetMenuSet]
AS
	SET NOCOUNT ON;
SELECT * FROM menuSet order by isSystem desc,pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuSetByAdminId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuSetByAdminId]
GO








CREATE PROCEDURE [dbo].[GetMenuSetByAdminId]
@adminId int
AS
	SET NOCOUNT ON;
SELECT * FROM menuSet where isSystem=1 order by isSystem desc,pkId desc








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuSetByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuSetByPkId]
GO







CREATE PROCEDURE [dbo].[GetMenuSetByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM menuSet
where pkId=@pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMenuTreeByParentId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMenuTreeByParentId]
GO



CREATE PROCEDURE GetMenuTreeByParentId
@parentId int
AS

	SELECT m.*, g.name grade, o.name mode FROM menu m,grade g,mode o where m.gradeId=g.pkId and m.modeId=o.pkId and parentId=@parentId and m.bigClassId<>-1 and modeId<>4 order by ordering  desc
	RETURN 




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByAfterPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByAfterPkId]
GO







CREATE PROCEDURE [dbo].[GetMessageBoardByAfterPkId]
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 		
	SELECT TOP 1 * FROM messageBoard WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId < @pkId 
	AND bigClassId = (SELECT bigClassId FROM messageBoard where pkId = @pkId) ORDER BY pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByAfterPkIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByAfterPkIdAndPassed]
GO







CREATE PROCEDURE [dbo].[GetMessageBoardByAfterPkIdAndPassed]
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 		
	SELECT TOP 1 * FROM messageBoard WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId < @pkId 
	and checked=1 and isSecrecy=0  AND bigClassId = (SELECT bigClassId FROM messageBoard where pkId = @pkId) ORDER BY pkId desc







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByBeforePkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByBeforePkId]
GO







CREATE PROCEDURE [dbo].[GetMessageBoardByBeforePkId]
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 		
	SELECT TOP 1 * FROM messageBoard WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId > @pkId 
	AND bigClassId = (SELECT bigClassId FROM messageBoard where pkId = @pkId) ORDER BY pkId ASC







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByBeforePkIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByBeforePkIdAndPassed]
GO








CREATE PROCEDURE [dbo].[GetMessageBoardByBeforePkIdAndPassed]
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 		
	SELECT TOP 1 * FROM messageBoard WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId > @pkId 
	and checked=1 and isSecrecy=0 AND bigClassId = (SELECT bigClassId FROM messageBoard where pkId = @pkId) ORDER BY pkId ASC







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByBigClassId]
GO



CREATE PROCEDURE [dbo].[GetMessageBoardByBigClassId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int,
@searchStr varchar(100)='',
@recordCount int output

AS
	SET NOCOUNT ON

	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)
	
	SET @vSQL = N'SELECT @count = Count(m.pkId) from 
			messageBoard AS m 
			INNER JOIN [grade] as g on m.gradeId=g.pkId
			WHERE (m.gate > ' + STR(@gateId) + ' OR m.gate < ' + STR(@gateId) + ') and  
			m.bigClassId = ' + STR(@bigClassId) +' and 
			m.gradeId <= ' + STR(@gradeId) +' ' +
			@searchStr 

	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
	
	DECLARE @sql nvarchar(4000)
	SET @sql = '
	CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    );
    
    INSERT	INTO #PageIndexs (ID)
	SELECT	m.pkId
	FROM	messageBoard AS m 
			INNER JOIN [grade] as g on m.gradeId=g.pkId
	WHERE	(m.gate > ' + STR(@gateId) + ' OR m.gate < ' + STR(@gateId) + ') and   
			m.bigClassId = ' + STR(@bigClassId) +' and 	
			m.gradeId <= ' + STR(@gradeId) + ' ' +
			@searchStr + ' ' + '
	ORDER BY m.istop desc,m.updateTime desc,m.pkid desc;
	
	select m.pkId,m.title,m.checked,m.author,m.tel,m.isSecrecy,m.email,m.sex,m.userId
	,m.updatetime ,m.updateIp, m.click,m.isTop, m.MenuId,m.gradeId,m.bigclassId,m.gate,m.username,m.password
	,m.retime,m.redept,m.reAudit ,g.name as grade
	FROM	messageBoard AS m
			INNER JOIN [grade] as g on m.gradeId=g.pkId
			INNER JOIN [#PageIndexs] AS p ON m.pkId = p.ID
	WHERE	p.IndexId >= ' + STR(@PageLowerBound) + ' AND p.IndexId <= ' + STR(@PageUpperBound) + ' 
	ORDER BY m.istop desc,m.updateTime desc,m.pkid desc;

	DELETE #PageIndexs'
	EXEC(@sql)
	
	RETURN 

--------------------------------------------------------------
--	declare @vSQL varchar(1000)
--	select @vSQL = '
--
--	select top ' + convert(varchar, @pageSize) + ' m.*,g.name "grade" from 
--		messageBoard m, grade g 
--		where m.gate<>'+Convert(varchar,@gateId)+' and
-- m.bigClassId='+Convert(varchar,@bigClassId)+' and
-- m.gradeId<='+convert(varchar,@gradeId)+' and
-- m.gradeId=g.pkId and
-- m.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from
-- messageBoard where gate<>'+Convert(varchar,@gateId)+' and
-- bigClassId='+Convert(varchar,@bigClassId)+' and
-- gradeId<='+convert(varchar,@gradeId)+' order by istop desc,reaudit asc,m.updatetime desc,pkId desc)
-- '+@searchStr+' order by istop desc,reaudit asc,m.updatetime desc,m.pkId desc'
--	Execute (@vSQL)	
--	RETURN



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByBigClassIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByBigClassIdAndPassed]
GO







CREATE PROCEDURE [dbo].[GetMessageBoardByBigClassIdAndPassed]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@bigClassId int=null,
	@gradeid int=1,
	@gateId int,
	@passed int=1,
	@recordCount int output	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) from messageBoard where  gate<>'+Convert(varchar,@gateId)+' and  bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' *  FROM messageBoard where gate<>'+Convert(varchar,@gateId)+' and  bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0 ORDER BY isTop DESC, pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' *  FROM messageBoard  where gate<>'+Convert(varchar,@gateId)+' and  bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0 ORDER BY isTop ASC, pkId ASC) TempTable ORDER BY isTop DESC, pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' * FROM messageBoard where gate<>'+Convert(varchar,@gateId)+' and  bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0 ORDER BY isTop ASC, pkId ASC) TempTable ORDER BY isTop DESC, pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' * FROM messageBoard where gate<>'+Convert(varchar,@gateId)+' and  bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0 ORDER BY isTop DESC, pkId DESC) TempTable1 '
					+ ' ORDER BY isTop ASC, pkId ASC) TempTable2 ORDER BY isTop DESC, pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardByNameAndPwd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardByNameAndPwd]
GO







CREATE PROCEDURE [dbo].[GetMessageBoardByNameAndPwd]
	@username varchar(100),
	@password varchar(50)
AS
	SET NOCOUNT ON 		
	SELECT * FROM messageBoard WHERE username=@username and password=@password








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardBySearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardBySearch]
GO








CREATE PROCEDURE [dbo].[GetMessageBoardBySearch]
@bigClassId int,
@isManage int=0,
@gradeId int=1,
@gateId int

AS
	DECLARE @sqlStr varchar(1000)
	IF (@isManage = 0)
	 	SET @sqlStr = 'select *	 from messageBoard where checked=1 and IsSecrecy=0 and gradeId<='+Convert(varchar,@gradeId)+' and gate<>'+Convert(varchar,@gateId)+' and bigClassId='+Convert(varchar,@bigClassId)+' order by isTop desc,  pkId desc'

	 ELSE 
		SET @sqlStr = 'select *	 from messageBoard where gradeId<='+Convert(varchar,@gradeId)+' and gate<>'+Convert(varchar,@gateId)+' and bigClassId='+Convert(varchar,@bigClassId)+' order by isTop desc,  pkId desc'
	EXEC(@sqlStr)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageBoardSummaryByBigClassIdAndpassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageBoardSummaryByBigClassIdAndpassed]
GO




CREATE PROCEDURE [dbo].[GetMessageBoardSummaryByBigClassIdAndpassed]
	@currentPage int=1,
	@pageSize int=7,
	@bigClassId int=null,
	@gradeid int=1,
	@gateId int,
	@passed int=1,
	@recordCount int output	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) from messageBoard where gate<>'+Convert(varchar,@gateId)+' and bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId='+Convert(varchar,@bigClassId)+') and gradeId<='+convert(varchar,@gradeId)+' and checked='+convert(varchar,@passed)+' and isSecrecy =0'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' messageBoard.*,bigClass.name as bigClassName FROM messageBoard,bigclass WHERE bigclass.pkId = messageBoard.bigClassId and messageBoard.checked=1 and messageBoard.isSecrecy =0 and messageBoard.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId='+Convert(varchar,@bigClassId)+') and messageBoard.gate<>'+Convert(varchar,@gateId)+' and messageBoard.gradeId<='+convert(varchar,@gradeId)+' order by messageBoard.updatetime desc'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' messageBoard.*,bigClass.name as bigClassName FROM messageBoard,bigclass where bigclass.pkId = messageBoard.bigClassId and messageBoard.checked=1 and messageBoard.isSecrecy =0 and messageBoard.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId='+Convert(varchar,@bigClassId)+') and messageBoard.gate<>'+Convert(varchar,@gateId)+' and messageBoard.gradeId<='+convert(varchar,@gradeId)+' order by messageBoard.updatetime desc ) TempTable ORDER BY messageBoard.isTop DESC '	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' messageBoard.*,bigClass.name as bigClassName FROM messageBoard,bigclass where bigclass.pkId = messageBoard.bigClassId and messageBoard.checked=1 and messageBoard.isSecrecy =0 and messageBoard.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId='+Convert(varchar,@bigClassId)+') and messageBoard.gate<>'+Convert(varchar,@gateId)+' and messageBoard.gradeId<='+convert(varchar,@gradeId)+' ORDER BY messageBoard.updatetime desc) TempTable ORDER BY messageBoard.isTop DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' messageBoard.*,bigClass.name as bigClassName FROM messageBoard,bigclass where bigclass.pkId = messageBoard.bigClassId and messageBoard.checked=1 and messageBoard.isSecrecy =0 and messageBoard.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId='+Convert(varchar,@bigClassId)+') and messageBoard.gate<>'+Convert(varchar,@gateId)+' and messageBoard.gradeId<='+convert(varchar,@gradeId)+' ORDER BY messageBoard.updatetime DESC) TempTable1 '
					+ ' ORDER BY messageBoard.isTop ASC ) TempTable2 ORDER BY messageBoard.isTop DESC, messageBoard.pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)

RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageForwardByUserName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageForwardByUserName]
GO





CREATE PROCEDURE [dbo].[GetMessageForwardByUserName]
	-- Add the parameters for the stored procedure here
	@ForwardToUserName nvarchar(250),
	@Status int,
	@recordCount int output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	DECLARE @tableName nvarchar(50)
	DECLARE @str nvarchar(1000)
	SET @tableName = 'messageForward'
	SET @str = ' ForwardToUserName = ''' + @ForwardToUserName + ''''
	
	SET @vSQL = N'SELECT @count = Count(*) from '+Convert(nvarchar(50),@tableName)+' where ' + @str
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
	
	
	
	if @Status = 0
	   begin
	   SET @vSQL = N'SELECT messageBoard.MenuId as menuId,messageBoard.bigClassId as bigClassId,bigClass.name,bigClass.templateId,count(messageBoard.MenuId) as count from '+Convert(nvarchar(50),@tableName) + ',bigClass,messageBoard' +' where ' + @str + ' and messageForward.messageId = messageBoard.pkId and messageBoard.bigClassId = bigClass.pkId and messageboard.reContent is null group by messageBoard.bigClassId,bigClass.name,bigClass.templateId,messageBoard.MenuId'
	   end
	else
	   begin
	   SET @vSQL = N'SELECT messageBoard.MenuId as menuId,messageBoard.bigClassId as bigClassId,bigClass.name,bigClass.templateId,count(messageBoard.MenuId) as count from '+Convert(nvarchar(50),@tableName) + ',bigClass,messageBoard' +' where ' + @str + ' and messageForward.messageId = messageBoard.pkId and messageBoard.bigClassId = bigClass.pkId and messageBoard.reContent is not null group by messageBoard.bigClassId,bigClass.name,bigClass.templateId,messageBoard.MenuId'
	   end
	
	
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMessageForwardUserCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMessageForwardUserCount]
GO





CREATE PROCEDURE [dbo].[GetMessageForwardUserCount]
@bigClassId int=null,
@gradeId int=1,
@gateId int=-1,
@count int output,
@isForeGround int,
@searchStr varchar(100) = '',
@userName nvarchar(250)
AS
	SET NOCOUNT ON
	declare @num int
	declare @vSQL varchar(1000)
	IF (@isForeGround=0)
	BEGIN
		select @vSQL=
		     'ALTER  procedure GetCount @count int output as set nocount on select @count=Count(*) from messageBoard,messageForward where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and  gradeId<='+convert(varchar,@gradeId)+' and messageBoard.pkId = messageForward.messageId and ForwardToUserName = ''' + @userName + ''' return'
	END 
	ELSE IF (@isForeGround=1)
	BEGIN
		select @vSQL=
			'ALTER  procedure GetCount @count int output as set nocount on select @count=Count(*) from messageBoard,messageForward where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and  gradeId<='+convert(varchar,@gradeId)+' and messageBoard.reContent is not null and messageBoard.pkId = messageForward.messageId and ForwardToUserName = ''' + @userName + ''' return'
	END	
	Execute (@vSQL)
	execute GetCount @count=@num output
	select @count=@num
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetModuleAssignByusersId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetModuleAssignByusersId]
GO







CREATE PROCEDURE [dbo].[GetModuleAssignByusersId]
@usersId int=null
AS
	SET NOCOUNT ON
	select *
	from moduleAssign
	where usersId=@usersId
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetModuleAssignByusersIdAndBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetModuleAssignByusersIdAndBigClassId]
GO



CREATE PROCEDURE GetModuleAssignByusersIdAndBigClassId
@usersId int=null,
@bigClassId int
AS
	SET NOCOUNT ON

--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@usersId
if(@groupid=1)
begin
	select a.* from moduleAssign a, menu m
	where m.pkId=a.menuId
	and a.usersId=@usersId
	and m.bigClassId=@bigClassId
end
else
begin
	select a.* 
	from groupAssign a, menu m
	where m.pkId=a.menuId
	and a.groupId=@groupid
	and m.bigClassId=@bigClassId
end
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetModuleAssignByusersIdAndMenuId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetModuleAssignByusersIdAndMenuId]
GO



CREATE PROCEDURE GetModuleAssignByusersIdAndMenuId
@usersId int=null,
@menuId int
AS
	SET NOCOUNT ON

	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@usersId
if(@groupid=1)
begin
	select * from moduleAssign where usersId=@usersId and menuId=@menuId
end
else
begin
	select * from groupAssign where groupId=@groupid and menuId=@menuId
end

RETURN 




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMyDraftByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetMyDraftByUserId]
GO

CREATE PROCEDURE GetMyDraftByUserId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@recordCount int output
	
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)

	--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin

	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
	--这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	--根据栏目Id进行分组，以消除不用菜单位置的相同栏目产生的重复记录，20091010
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=3'
	SET @str = @str + ' and n.usersId='+STR(@userId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId ' --and n.MenuId=m.pkId 去掉了 fengyb 2008年-1-9日and n.MenuId=m.pkId 
	
	SET @tableName = '#test z, news n,users u,department d,grade g, template t, bigClass b '

end
else
begin
	
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid group by b.pkId,m.name

	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and n.passed=3'
	SET @str = @str + ' and n.usersId='+STR(@userId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId ' --and n.MenuId=m.pkId 去掉了 fengyb 2008年-1-9日and n.MenuId=m.pkId 

	SET @tableName = '#test1 z, news n,users u,department d,grade g, template t, bigClass b '
end

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'ntime ASC, sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'ntime DESC, sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)
	
	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.updateTime as ntime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END
	
	--执行查询
	EXEC(@sqlStr)
	if(@groupid=1)
	begin
		DELETE #test
	end
	else
	begin
		DELETE #test1
	end
RETURN



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByAbout]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByAbout]
GO







CREATE PROCEDURE [dbo].[GetNewsByAbout]
@about varchar(1000)=null
AS
	 SET NOCOUNT ON 
Execute (@about)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByAfterPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByAfterPkId]
GO

CREATE PROCEDURE GetNewsByAfterPkId
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 	
	SELECT TOP 1 pkId,intactTitle,isStatic,staticUrl FROM news WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId < @pkId AND passed=1
	AND bigClassId = (SELECT bigClassId FROM news where pkId = @pkId) ORDER BY pkId DESC


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByAlbumIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByAlbumIdAndPassed]
GO

CREATE PROCEDURE GetNewsByAlbumIdAndPassed
     (    
       @gradeid int=1,
@gateId int,
@albumid int=null,
@currentPage int=1,
@pageSize int=5,
@passed int=1
     )    
     AS    
 BEGIN


	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    if(@currentPage=0) --优化第一页访问速度
	begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)
		SELECT	TOP (@pageSize) n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				albumid =		@albumid and 
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				(n.validDays > 0 OR n.validDays < 0)
		ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc
	end
	else
	begin
    	-- Insert into our temp table
    	INSERT	INTO #PageIndexs (ID)
		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				albumid =		@albumid and 
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				(n.validDays > 0 OR n.validDays < 0)
		ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc
	end

	SELECT n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,n.albumId, u.UserName, d.name as Department
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByAll]
GO




CREATE PROCEDURE GetNewsByAll
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@searchStr varchar(100),
	@gateId int,
	@bigClassId int, --查询时选择的栏目范围 0为选择的全部栏目
    @NewSearchbigClassId int, --查询栏目ID
	@sdate varchar(20),
	@edate varchar(20)
	
AS	
	
	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
	
	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )
	
--查询是否有指定的查询栏目 
DECLARE @sCount int
set @sCount = (SELECT Count(pkId) from searchOptions where bigClassId = @NewSearchbigClassId)
if(@sCount>0)
begin
	
	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
	--已规定查询栏目范围
	--选择查询所有
    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@searchStr+'%' or n.author like '%'+@searchStr+'%' or n.CopyFrom like '%'+@searchStr+'%' or n.intactTitle like '%'+@searchStr+'%' or n.subheading like '%'+@searchStr+'%' )
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc
	
	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@searchStr+'%' or n.author like '%'+@searchStr+'%' or n.CopyFrom like '%'+@searchStr+'%' or n.intactTitle like '%'+@searchStr+'%' or n.subheading like '%'+@searchStr+'%' )
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc
	
	end 
end
else
begin
	
	--没有规定查询栏目范围
	--选择查询所有
	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n ,[NewsContent] AS nc
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(n.isDuplicated>1 OR n.isDuplicated<1 or n.isDuplicated is null) and
			n.pkid=nc.newsid and (nc.content like '%'+@searchStr+'%' or n.author like '%'+@searchStr+'%' or n.CopyFrom like '%'+@searchStr+'%' or n.intactTitle like '%'+@searchStr+'%' or n.subheading like '%'+@searchStr+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc
	
	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n ,[NewsContent] AS nc
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@searchStr+'%' or n.author like '%'+@searchStr+'%' or n.CopyFrom like '%'+@searchStr+'%' or n.intactTitle like '%'+@searchStr+'%' or n.subheading like '%'+@searchStr+'%' ) 
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc
	end
end

	SELECT  n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc
	
DELETE #PageIndexs
RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByAuthor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByAuthor]
GO









CREATE PROCEDURE [dbo].[GetNewsByAuthor]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@author varchar(100),
	@gateId int,
	@bigClassId int,
	@NewSearchbigClassId int,
	@sdate varchar(20),
	@edate varchar(20)
AS	


	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )


--查询是否有指定的查询栏目 

DECLARE @sCount int
set @sCount = (SELECT Count(pkId) from searchOptions where bigClassId = @NewSearchbigClassId)
if(@sCount>0)
begin

	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
	--已规定查询栏目范围
	--选择查询所有

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.author like '%'+@author+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN
	end
	else
	begin
	

	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.author like '%'+@author+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN



	end 

end
else
begin
	
	--没有规定查询栏目范围
	--选择查询所有
	--查询的栏目ID
	if (@bigClassId = 0)
	begin

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(n.isDuplicated>1 OR n.isDuplicated<1 or n.isDuplicated is null) and	
			(n.author like '%'+@author+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 

	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			(n.author like '%'+@author+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
	end
end




set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBeforePkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBeforePkId]
GO

CREATE PROCEDURE GetNewsByBeforePkId
	@pkId int =null,
	@gradeId int=1,
	@gateId int 
AS
	SET NOCOUNT ON 	
	SELECT TOP 1 pkId,intactTitle,isStatic,staticUrl FROM news WHERE gradeId <= @gradeId AND gate <> @gateId AND pkId > @pkId AND passed=1
	AND bigClassId = (SELECT bigClassId FROM news where pkId = @pkId) ORDER BY pkId ASC



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBigClassIdAndisImageNewsAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBigClassIdAndisImageNewsAndPassed]
GO

CREATE PROCEDURE GetNewsByBigClassIdAndisImageNewsAndPassed
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int,
@isRandom int
AS
BEGIN

	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

	CREATE TABLE #PageIndexs1
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

	IF (@isRandom = 0)
		BEGIN
			--不随机
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT	n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
					INNER JOIN [grade] AS g ON n.gradeId = g.pkId
					INNER JOIN [special] AS s ON n.specialId = s.pkId
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					bigClassId =		@bigClassId and 
					n.gradeId <=		@gradeId and 
					n.passed =	1 and 
					(isImageNews=1 OR isImageNews=2) and 
					(n.validDays > 0 OR n.validDays < 0)
			ORDER BY n.istop DESC,n.priority desc,n.updateTime desc, n.pkId DESC

			SELECT  n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department,g.name as grade,s.name as special
			FROM	[News] AS n
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
					INNER JOIN [grade] AS g ON n.gradeId = g.pkId
					INNER JOIN [special] AS s ON n.specialId = s.pkId
					INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
			WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
			ORDER BY n.istop DESC,n.priority desc,n.updateTime desc, n.pkId DESC

		END
	ELSE IF(@isRandom = -1)
		BEGIN
			
			--全表随机
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT	n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
					INNER JOIN [grade] AS g ON n.gradeId = g.pkId
					INNER JOIN [special] AS s ON n.specialId = s.pkId
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					bigClassId =		@bigClassId and 
					n.gradeId <=		@gradeId and 
					n.passed =	1 and 
					(isImageNews=1 OR isImageNews=2) and 
					(n.validDays > 0 OR n.validDays < 0)
			ORDER BY newid()
			

			SELECT  n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department,g.name as grade,s.name as special
				FROM	[News] AS n
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
						INNER JOIN [grade] AS g ON n.gradeId = g.pkId
						INNER JOIN [special] AS s ON n.specialId = s.pkId
						INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
				WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
				ORDER BY newid()
		END
	ELSE
		BEGIN

			--范围随机
			-- Insert into our temp table
			INSERT	INTO #PageIndexs (ID)
			SELECT	TOP (@isRandom)  n.pkId
			FROM	[News] AS n 
					INNER JOIN [Users] AS u ON n.UsersID = u.PKID
					INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
					INNER JOIN [grade] AS g ON n.gradeId = g.pkId
					INNER JOIN [special] AS s ON n.specialId = s.pkId
			WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
					bigClassId =		@bigClassId and 
					n.gradeId <=		@gradeId and 
					n.passed =	1 and 
					(isImageNews=1 OR isImageNews=2) and 
					(n.validDays > 0 OR n.validDays < 0)
			ORDER BY n.istop DESC,n.priority desc,n.updateTime desc, n.pkId DESC

			INSERT	INTO #PageIndexs1 (ID)
			SELECT	ID 
			FROM [#PageIndexs] ORDER BY newid()
			
			SELECT n.pkId ,n.specialId, n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand	,n.isTop, n.useListTitle ,n.showReviewLink, n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum	,n.readPoint ,n.itemType ,n.descript ,n.usersId	,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews	,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId	,n.updateIp	,n.useSubHeading,n.isDuplicated	,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department,g.name as grade,s.name as special
				FROM	[News] AS n
						INNER JOIN [Users] AS u ON n.UsersID = u.PKID
						INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
						INNER JOIN [grade] AS g ON n.gradeId = g.pkId
						INNER JOIN [special] AS s ON n.specialId = s.pkId
						INNER JOIN [#PageIndexs1] AS p ON n.PKID = p.ID
				WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
				--ORDER BY newid()
			
		END
	DELETE #PageIndexs
	DELETE #PageIndexs1
	RETURN 
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBigClassIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBigClassIdAndPassed]
GO

CREATE PROCEDURE GetNewsByBigClassIdAndPassed
@gradeid int=1,
@gateId int,
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@passed int=1
AS
BEGIN


	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    if(@currentPage=0) --优化第一页访问速度
	begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)
		SELECT	TOP (@pageSize) n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				bigClassId =		@bigClassId and 
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				(n.validDays > 0 OR n.validDays < 0)
		ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc
	end
	else
	begin
    	-- Insert into our temp table
    	INSERT	INTO #PageIndexs (ID)
		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
				bigClassId =		@bigClassId and 
				n.gradeId <=		@gradeId and 
				n.passed =			@passed and 
				(n.validDays > 0 OR n.validDays < 0)
		ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc
	end

	SELECT n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.updateTime desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBigClassIdAndUsersIdCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBigClassIdAndUsersIdCount]
GO



CREATE PROCEDURE [dbo].[GetNewsByBigClassIdAndUsersIdCount]
@bigClassId int=null,
@gradeId int=1,
@gateId int=-1,
@usersId int,
@table varchar(100),
@searchStr varchar(100) = '',
@count int output
AS
	 SET NOCOUNT ON	
     declare @num int
     declare @vSQL varchar(1000)
     select @vSQL=
     case @table
        when 'popwindow' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<>'+convert(varchar,@gateId)+' and userId='+convert(varchar,@usersId)+'and gradeId<='+convert(varchar,@gradeId)+' return'
        --when 'newss' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<>'+convert(varchar,@gateId)+' and (specialId1='+convert(varchar,@bigclassId)+' or specialId2='+convert(varchar,@bigclassId)+' ) and usersId='+convert(varchar,@usersId)+' and gradeId<='+convert(varchar,@gradeId)+' return'
        when 'picNews'then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<>'+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and usersId='+convert(varchar,@usersId)+' and isImageNews=1 and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
        when 'news' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<>'+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and usersId='+convert(varchar,@usersId)+' and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+ @searchStr + ' return'     
        ELSE 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<>'+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and userId='+convert(varchar,@usersId)+' and gradeId<='+convert(varchar,@gradeId)+' return'
     end
Execute (@vSQL)
execute GetCount @count=@num output
select @count=@num

RETURN


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBigClassIdCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBigClassIdCount]
GO



CREATE PROCEDURE [dbo].[GetNewsByBigClassIdCount]
@bigClassId int=null,
@gradeId int=1,
@gateId int=-1,
@table varchar(100)=null,
@count int output,
@isForeGround int,
@searchStr varchar(100) = ''
AS
	SET NOCOUNT ON
	declare @num int
	declare @vSQL varchar(1000)
	IF (@isForeGround=0)
	BEGIN
		--user后台调用
		select @vSQL=
			case @table
				--when 'LatestNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigClassId in (select bigClassId from BigClassAssociation where thisBigclassId= '+convert(varchar,@bigclassId)+') and gradeId<='+convert(varchar,@gradeId)+' return'
				when 'users' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from users where isDel<>1 return'
				when 'popwindow' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and gradeId<='+convert(varchar,@gradeId)+' return'
				--when 'newss' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and (specialId1='+convert(varchar,@bigclassId)+' or specialId2='+convert(varchar,@bigclassId)+' ) and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
				when 'news' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId) + @searchStr + ' return'
				--when 'gameTitleList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' return'
				--when 'gamePicList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' return'
				--when 'gameList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' return'
				--when 'todayInHistory' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' return'
				--when 'picNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
				--when 'picNews1' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
				--when 'picNews2' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
				--when 'mediaNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=2 and (passed = 0 or passed = 1 or passed = 2) and gradeId<='+convert(varchar,@gradeId)+' return'
				--when 'special' then ''ALTER  procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and  gradeId<='+convert(varchar,@gradeId) + @searchStr + ' return''
				ELSE 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and gradeId<='+convert(varchar,@gradeId)+@searchStr+' return'
			end
	END 
	ELSE IF (@isForeGround=1)
	BEGIN
		--前台调用
		select @vSQL=
			case @table
				when 'LatestNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigClassId in (select bigClassId from BigClassAssociation where thisBigclassId= '+convert(varchar,@bigclassId)+') and gradeId<='+convert(varchar,@gradeId)+' and isDuplicated = 0 and (validDays > 0 OR validDays < 0) and passed=1 return'
				when 'users' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from users where isDel<>1 return'
				when 'popwindow' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				--when 'newss' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and (specialId1='+convert(varchar,@bigclassId)+' or specialId2='+convert(varchar,@bigclassId)+' ) and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'news' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'gameTitleList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'gamePicList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'gameList' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'todayInHistory' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId= '+convert(varchar,@bigclassId)+' and gradeId <= '+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'picNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'picNews1' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'picNews2' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=1 and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'mediaNews' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and isImageNews=2 and gradeId<='+convert(varchar,@gradeId)+' and passed=1 and (validDays > 0 OR validDays < 0) return'
				when 'special' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news where gate<> '+convert(varchar,@gateId)+' and specialId='+convert(varchar,@bigclassId)+' and gradeId<='+convert(varchar,@gradeId) + @searchStr + ' and passed=1 return'	
				when 'messageBoard' then 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from messageBoard where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and gradeId<='+convert(varchar,@gradeId)+' and checked=1 and isSecrecy=0 return'	--留言本专用		
				ELSE 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from '+convert(varchar,@table)+' where gate<> '+convert(varchar,@gateId)+' and bigclassId='+convert(varchar,@bigclassId)+' and gradeId<='+convert(varchar,@gradeId)+' and passed=1 return'
			end
	END	
	Execute (@vSQL)
	execute GetCount @count=@num output
	select @count=@num
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByBigClassIds]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByBigClassIds]
GO







CREATE PROCEDURE [dbo].[GetNewsByBigClassIds]
(
	@bigClassIds varchar(100),
	@pageSize int
)
AS
	SET NOCOUNT ON
	DECLARE @str nvarchar(1000)
	SET @str = 'SELECT top ' + Convert(nvarchar(50),@pageSize) + ' * FROM news WHERE passed=1 and patindex(''%|''+Convert(Varchar(50),news.bigClassId)+''|%'','''+Convert(varchar(100),@bigClassIds)+''')>0 order by updatetime desc'
	exec(@str)
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByClick]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByClick]
GO

CREATE PROCEDURE GetNewsByClick
@pageSize int=5,
@gradeid int=1,
@gateId int,
@menuId int
AS

BEGIN
	
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @pageSize * 0
    SET @PageUpperBound = @pageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    DECLARE @num int
	select @num=0
	select @num = count(pkId) from BigClassAssociation where thisBigClassId=@menuId
	if(@num = 0)
	begin

	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	TOP (@pageSize) n.pkId
	FROM	[News] AS n 
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
	WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
			n.gradeId <= @gradeId and 
			n.passed =	1 and 
			isDuplicated != 1 and
			(n.validDays > 0 OR n.validDays < 0)  
	ORDER BY n.click desc,n.istop desc,n.Priority desc,n.pkid desc

	end
	else
	begin

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	TOP (@pageSize) n.pkId
	FROM	[News] AS n 
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
	WHERE	(n.gate > @gateId OR n.gate < @gateId) and   
			n.gradeId <= @gradeId and 
			n.passed =	1 and 
			n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@menuId) and	
			isDuplicated != 1 and
			(n.validDays > 0 OR n.validDays < 0)  
	ORDER BY n.click desc,n.istop desc,n.Priority desc,n.pkid desc

	end


	SELECT  n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime, u.UserName, d.name as Department ,b.name as BigClassName
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [BigClass] AS b ON n.bigClassId=b.pkId
			INNER JOIN [Template] AS t ON t.pkId = b.templateId
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.click desc,n.istop desc ,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByContent]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByContent]
GO

CREATE  PROCEDURE  GetNewsByContent
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@content varchar(100),
	@gateId int,
	@bigClassId int,
	@NewSearchbigClassId int,
	@sdate varchar(20),
	@edate varchar(20)
AS	
	--转换成索引
	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )


--查询是否有指定的查询栏目 

DECLARE @sCount int
set @sCount = (SELECT Count(pkId) from searchOptions where bigClassId = @NewSearchbigClassId)
if(@sCount>0)
begin

	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
	--已规定查询栏目范围
	--选择查询所有

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@content+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN
	end
	else
	begin
	

	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@content+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN



	end 

end
else
begin
	
	--没有规定查询栏目范围
	--选择查询所有
	--查询的栏目ID
	if (@bigClassId = 0)
	begin

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(n.isDuplicated>1 OR n.isDuplicated<1 or n.isDuplicated is null) and 
			n.pkid=nc.newsid and (nc.content like '%'+@content+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 

	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n,[NewsContent] AS nc
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			n.pkid=nc.newsid and (nc.content like '%'+@content+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
	end
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByCopyFrom]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByCopyFrom]
GO








CREATE PROCEDURE [dbo].[GetNewsByCopyFrom]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@copyFrom varchar(100),
	@gateId int,
	@bigClassId int,
	@NewSearchbigClassId int,
	@sdate varchar(20),
	@edate varchar(20)
AS	
	--转换成索引
	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )


--查询是否有指定的查询栏目 

DECLARE @sCount int
set @sCount = (SELECT Count(pkId) from searchOptions where bigClassId = @NewSearchbigClassId)
if(@sCount>0)
begin

	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
	--已规定查询栏目范围
	--选择查询所有

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.CopyFrom like '%'+@copyFrom+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN
	end
	else
	begin
	

	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.CopyFrom like '%'+@copyFrom+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN



	end 

end
else
begin
	
	--没有规定查询栏目范围
	--选择查询所有
	--查询的栏目ID
	if (@bigClassId = 0)
	begin

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(n.isDuplicated>1 OR n.isDuplicated<1 or n.isDuplicated is null) and
			(n.CopyFrom like '%'+@copyFrom+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 

	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			n.bigClassId=@bigClassId and
			(n.CopyFrom like '%'+@copyFrom+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0


	SELECT  n.*
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
	end
end






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByDGSQMY]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByDGSQMY]
GO




CREATE PROCEDURE GetNewsByDGSQMY
(
	@currentPage int,
	@PageSize	int,
	@BigClassID	int,
	@NewsRecords int output
)
AS
BEGIN

	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
	
	select @NewsRecords = COUNT(*) from news where bigClassId = @bigClassId and passed = 1 
	
    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    if(@currentPage=0) --优化第一页访问速度
	begin
		-- Insert into our temp table
		INSERT	INTO #PageIndexs (ID)
		SELECT	TOP (@pageSize) n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	bigClassId =		@bigClassId and 
				n.passed =			1 
		ORDER BY n.istop desc,n.Priority desc,n.validDays desc,n.updateTime desc,n.pkid desc
	end
	else
	begin
    	-- Insert into our temp table
    	INSERT	INTO #PageIndexs (ID)
		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
		WHERE	bigClassId =		@bigClassId and 
				n.passed =			1 
				
		ORDER BY n.istop desc,n.Priority desc,n.validDays desc,n.updateTime desc,n.pkid desc
	end

	SELECT n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID, u.UserName, d.name as Department
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.validDays desc,n.updateTime desc,n.pkid desc

	DELETE #PageIndexs

	RETURN 
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByPage]
GO



CREATE PROCEDURE GetNewsByPage
	@searchStr varchar(500),
	@pageIndex int,
	@pageSize int,
	@pageCount int output,
	@recordCount int output
AS
BEGIN
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(n.pkId) FROM news AS n  
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				INNER JOIN [bigclass] As b ON n.bigclassId=b.pkId 
				WHERE' + @searchStr
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
	SET @pageCount = (@recordCount + @pageSize-1)/@pageSize
	
	SET @pageIndex = @pageIndex - 1
	IF (@pageIndex < 0)
		BEGIN
			SET @pageIndex = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @pageSize * @pageIndex
    SET @PageUpperBound = @pageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )
	declare @s Nvarchar(1000)

	if(@pageIndex=0) --优化第一页访问速度
	begin
		-- Insert into our temp table
	    SET @s='INSERT INTO #PageIndexs (ID)
		SELECT	TOP '+STR(@pageSize)+' n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				INNER JOIN [bigclass] As b ON n.bigclassId=b.pkId
		WHERE ' +@searchStr+'
		ORDER BY n.istop desc,n.Priority desc,n.pkid desc'
		EXEC(@s)
	end
	else
	begin
    	-- Insert into our temp table
    	SET @s='INSERT	INTO #PageIndexs (ID)
		SELECT	n.pkId
		FROM	[News] AS n 
				INNER JOIN [Users] AS u ON n.UsersID = u.PKID
				INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
				INNER JOIN [bigclass] As b ON n.bigclassId=b.pkId
		WHERE '+@searchStr+'
		ORDER BY n.istop desc,n.Priority desc,n.pkid desc'
		EXEC(@s)
	end
	SELECT n.pkId,n.intactTitle,n.updateTime,u.fullName, n.isduplicated,
case n.auditUserID when 0 then '' else (select fullname from users where pkid=n.auditUserID) end auditUser,
d.name as deptname,b.name as bigclassname
	FROM	[News] AS n
			INNER JOIN [Users] AS u ON n.UsersID = u.PKID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
			INNER JOIN [bigclass] As b ON n.bigclassId=b.pkId
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	DELETE #PageIndexs
	RETURN 
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByPkIdAndClick]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByPkIdAndClick]
GO

CREATE PROCEDURE GetNewsByPkIdAndClick
@pkId int =null,
@gradeId int=1,
@gateId int 
AS
	 SET NOCOUNT ON 
BEGIN TRANSACTION


	--更新新闻点击量，并同步更新抄送新闻点击量
	update news set click = click + 1 where pkId=@pkId or duplicatedPkId=@pkId


select N.[pkId],N.[specialId],N.[gradeId],N.[gate],N.[departmentId],N.[listTitle],N.[intactTitle],N.[subheading],N.[isRecommand],N.[isTop],N.[useListTitle],N.[showReviewLink],N.[Author],N.[AuthorMail],N.[CopyFrom],N.[copyFromLink],N.[about],N.[pageType],N.[autoPageCharNum],N.[readPoint],N.[itemType],N.[descript],N.[usersId],N.[editor],N.[click],N.[updateTime],N.[passed],N.[isReview],N.[isImageNews],N.[imageName],N.[MenuId],N.[bigClassId],N.[width],N.[height],N.[updateIp],N.[useSubHeading],N.[isDuplicated],N.[duplicateFrom],N.[duplicatedPkId],N.[validDays],N.[mediaAutostart],N.[AuditUserID],N.[Priority],N.[isStatic],N.[staticUrl],N.[staticTime],N.[albumId],C.* from news N,newsContent C where N.pkid=C.newsid and N.pkId=@pkId and N.gradeId<=@gradeId and N.gate<>@gateId
-- 改为通过程序去判断and passed = 1
	
 COMMIT TRANSACTION
 RETURN 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByRecommand]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByRecommand]
GO

CREATE PROCEDURE GetNewsByRecommand
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
SET NOCOUNT ON 


select top (@pageSize)  n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,d.name as Department from news AS n INNER JOIN [Department] AS d ON n.departmentId = d.PKID where gate<>@gateId and IsImageNews=0 and isDuplicated<>1 and gradeId<=@gradeId and passed=1 and isRecommand=1 and validDays<>0 order by n.istop desc,n.pkId desc



RETURN


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsBySpecialAndpassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsBySpecialAndpassed]
GO

CREATE PROCEDURE GetNewsBySpecialAndpassed
@special int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@passed int=1,
@gateId int
AS
	SET NOCOUNT ON 
	
	declare @vSQL varchar(2000)
	select @vSQL = 'select top ' + convert(varchar, @pageSize) 
	+ ' n.pkId ,n.specialId ,n.gradeId ,n.gate ,n.departmentId ,n.listTitle ,n.intactTitle ,n.subheading
				,n.isRecommand ,n.isTop ,n.useListTitle ,n.showReviewLink ,n.Author ,n.AuthorMail ,n.CopyFrom ,n.copyFromLink
				,n.about ,n.pageType ,n.autoPageCharNum ,n.readPoint ,n.itemType ,n.descript ,n.usersId ,n.editor
				,n.click ,n.updateTime ,n.passed ,n.isReview ,n.isImageNews ,n.imageName ,n.width ,n.height
				,n.MenuId ,n.bigClassId ,n.updateIp ,n.useSubHeading ,n.isDuplicated ,n.duplicateFrom ,n.duplicatedPkId ,n.validDays
				,n.mediaAutostart ,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,d.name "department" from news n,users u,department d where n.gate<>'
	+Convert(varchar,@gateId)+' and (specialId='+Convert(varchar,@special)
	+') and n.usersId=u.pkId and (n.isDuplicated=0 or n.isDuplicated is null) and n.departmentId=d.pkId and n.gradeId<='
	+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)
	+' and validDays <> 0 and n.pkId < all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))
	+' pkId from news where gate<>'+Convert(varchar,@gateId)+' and (specialId='
	+Convert(varchar,@special)+') and gradeId<='+convert(varchar,@gradeId)+' and isDuplicated<>1 and passed='
	+convert(varchar,@passed)+' and validDays <> 0 order by istop desc,pkId desc) order by istop desc,n.pkId desc'
	Execute (@vSQL)
	

	RETURN 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsByTitle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsByTitle]
GO




CREATE PROCEDURE [dbo].[GetNewsByTitle]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@title varchar(100),
	@gateId int,
	@bigClassId int,
	@NewSearchbigClassId int,
	@sdate varchar(20),
	@edate varchar(20)
AS	
    


	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

	-- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )


--查询是否有指定的查询栏目 

DECLARE @sCount int
set @sCount = (SELECT Count(pkId) from searchOptions where bigClassId = @NewSearchbigClassId)
if(@sCount>0)
begin

	--查询的栏目ID
	if (@bigClassId = 0)
	begin
	
	--已规定查询栏目范围
	--选择查询所有

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.intactTitle like '%'+@title+'%' or n.subheading like '%'+@title+'%' )
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	end
	else
	begin
	

	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
    SELECT	n.pkId
	FROM	[News] AS n,[searchOptions] AS so
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and 
			patindex('%|'+Convert(Varchar(50),n.bigClassId)+'|%',so.OptionsValue)>0 and so.bigClassId = @NewSearchbigClassId and
			(n.intactTitle like '%'+@title+'%' or n.subheading like '%'+@title+'%') and
			n.bigClassId=@bigClassId 
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	end 

end
else
begin
	
	--没有规定查询栏目范围
	--选择查询所有
	--查询的栏目ID
	if (@bigClassId = 0)
	begin

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(n.isDuplicated>1 OR n.isDuplicated<1 or n.isDuplicated is null) and	
			(n.intactTitle like '%'+@title+'%' or n.subheading like '%'+@title+'%')
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	end
	else
	begin
	
	-- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	n.pkId
	FROM	[News] AS n 
	WHERE	n.gradeId <= @gradeId and 
			n.passed = 1 and 
			(n.validDays > 0 OR n.validDays < 0) and
			(isDuplicated>1 OR isDuplicated<1 or isDuplicated is null) and
			(n.intactTitle like '%'+@title+'%' or n.subheading like '%'+@title+'%') and 
			n.bigClassId=@bigClassId 
			and datediff(d,n.updateTime,(case when @sdate ='' then n.updateTime else cast(@sdate as datetime) end))<=0 
		    and  datediff(d,n.updateTime,(case when @edate ='' then n.updateTime else cast(@edate as datetime) end))>=0

	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

	
	end
end


SELECT  n.*,d.name as Department 
	FROM	[News] AS n
			INNER JOIN [#PageIndexs] AS p ON n.PKID = p.ID
			INNER JOIN [Department] AS d ON n.DepartmentID = d.PKID
	WHERE	p.IndexId >= @PageLowerBound AND p.IndexId <= @PageUpperBound
	ORDER BY n.istop desc,n.Priority desc,n.pkid desc

DELETE #PageIndexs
RETURN




set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsCalendarByDay]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsCalendarByDay]
GO






CREATE PROCEDURE [dbo].[GetNewsCalendarByDay]
(
	@NewSearchbigClassId int,
	@bigClassId int,
	@gradeId int,
	@gateId int,
	@searchTimeBegin DateTime,
	@searchTimeEnd DateTime
)
AS
	--设置查询表名称
	DECLARE @tableName nvarchar(50)
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
    DECLARE @sCount int,@sNum int
    DECLARE @sSql nvarchar(200)
    DECLARE @searchStrBegin nvarchar(100)
    DECLARE @searchStrEnd nvarchar(100)
    SET @sSQL = N'SELECT @count=Count(searchOptions.pkId) from searchOptions where bigClassId = ' + STR(@NewSearchbigClassId)
    EXEC sp_ExecuteSQL @sSQL,N'@count int output',@sNum output
    SELECT @sCount = @sNum

	SET @str = ' validDays <> 0 '
	SET @searchStrBegin = Convert(varchar,@searchTimeBegin)
	SET @searchStrEnd = Convert(varchar,@searchTimeEnd)
	IF (@bigClassId <> 0)
    BEGIN
        SET @tableName = 'news,bigClass'
		SET @str = @str + ' and news.bigClassId='+STR(@bigClassId)
    END
    ELSE IF(@sCount > 0)
    BEGIN
        SET @tableName = 'news,searchOptions,bigClass'
        SET @str = @str + ' and patindex(''%|''+' + 'Convert(Varchar(50),news.bigClassId)'+ '+''|%'',searchOptions.OptionsValue)>0 and searchOptions.bigClassId = ' +STR(@NewSearchbigClassId)
	END
    ELSE IF(@sCount = 0)
    BEGIN
        SET @tableName = 'news,bigClass'
    END


	SET @str = @str + ' and news.updateTime >''' + @searchStrBegin + ''' and news.updateTime <''' + @searchStrEnd + '''' + ' and news.bigClassId = bigClass.pkId '

	SET @str = @str + ' and news.passed=1 and (news.isDuplicated<>1 or news.isDuplicated is null) and news.gradeId<='+Convert(varchar,@gradeId) + 'group by CONVERT(varchar(10),news.updatetime,120)'
	--SET @str = @str + ' and gate<>'+Convert(varchar,@gateId)

	
	
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT CONVERT(varchar(10),news.updatetime,120) ''日期'',count(*) ''新闻数'' from '+Convert(nvarchar(50),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsCalendarSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsCalendarSearch]
GO






CREATE PROCEDURE [dbo].[GetNewsCalendarSearch]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@searchTimeBegin DateTime,
	@searchTimeEnd DateTime,
	@gateId int,
	@bigClassId int,
	@NewSearchbigClassId int
AS	
    
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	--设置查询表名称
	DECLARE @tableName nvarchar(50)
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
    DECLARE @sCount int,@sNum int
    DECLARE @sSql nvarchar(200)
    DECLARE @searchStrBegin nvarchar(100)
    DECLARE @searchStrEnd nvarchar(100)
    SET @sSQL = N'SELECT @count=Count(searchOptions.pkId) from searchOptions where bigClassId = ' + STR(@NewSearchbigClassId)
    EXEC sp_ExecuteSQL @sSQL,N'@count int output',@sNum output
    SELECT @sCount = @sNum
    
    SET @str = ' validDays <> 0 '
    SET @searchStrBegin = Convert(varchar,@searchTimeBegin)
	SET @searchStrEnd = Convert(varchar,@searchTimeEnd)
	IF (@bigClassId <> 0)
    BEGIN    
        SET @tableName = 'news,bigClass'
		SET @str = @str + ' and news.bigClassId='+STR(@bigClassId)
    END
    ELSE IF(@sCount > 0)
    BEGIN
        SET @tableName = 'news,searchOptions,bigClass'
        SET @str = @str + ' and patindex(''%|''+' + 'Convert(Varchar(50),news.bigClassId)'+ '+''|%'',searchOptions.OptionsValue)>0 and searchOptions.bigClassId = ' +STR(@NewSearchbigClassId)
	END
	ELSE IF(@sCount = 0)
    BEGIN
        SET @tableName = 'news,bigClass'
    END
	SET @str = @str + ' and news.updateTime >''' + @searchStrBegin + ''' and news.updateTime <''' + @searchStrEnd + '''' + ' and news.bigClassId = bigClass.pkId '
	SET @str = @str + ' and news.passed=1 and (news.isDuplicated<>1 or news.isDuplicated is null) and news.gradeId<='+STR(@gradeId)
	--SET @str = @str + ' and gate<>'+STR(@gateId)

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
    DECLARE @orderTypeA_out nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
    DECLARE @orderTypeD_out nvarchar(100)
	SET @orderTypeA = 'isTop ASC, news.pkId ASC'
    SET @orderTypeA_out = 'isTop ASC, pkId ASC'
	SET @orderTypeD = 'isTop DESC, news.pkId DESC'
	SET @orderTypeD_out = 'isTop DESC, pkId DESC'
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int, @recordCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(news.pkId) from '+Convert(nvarchar(50),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(2000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' news.*,bigClass.name FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' news.*,bigClass.name  FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD_out)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' news.*,bigClass.name FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD_out)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' news.*,bigClass.name FROM '+Convert(nvarchar(50),@tableName)+' where '+Convert(nvarchar(1000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA_out)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD_out)
	END

	--执行查询
	EXEC(@sqlStr)

RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsCalendarSearchCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsCalendarSearchCount]
GO






CREATE PROCEDURE [dbo].[GetNewsCalendarSearchCount]
(
	@NewSearchbigClassId int,
	@bigClassId int,
	@gradeId int,
	@gateId int,
	@searchTimeBegin DateTime,
	@searchTimeEnd DateTime,
	@recordCount int output
)
AS
	--设置查询表名称
	DECLARE @tableName nvarchar(50)
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
    DECLARE @sCount int,@sNum int
    DECLARE @sSql nvarchar(200)
    DECLARE @searchStrBegin nvarchar(100)
    DECLARE @searchStrEnd nvarchar(100)
    SET @sSQL = N'SELECT @count=Count(searchOptions.pkId) from searchOptions where bigClassId = ' + STR(@NewSearchbigClassId)
    EXEC sp_ExecuteSQL @sSQL,N'@count int output',@sNum output
    SELECT @sCount = @sNum

	SET @str = ' validDays <> 0 '
	SET @searchStrBegin = Convert(varchar,@searchTimeBegin)
	SET @searchStrEnd = Convert(varchar,@searchTimeEnd)
	IF (@bigClassId <> 0)
    BEGIN
        SET @tableName = 'news,bigClass'
		SET @str = @str + ' and news.bigClassId='+STR(@bigClassId)
    END
    ELSE IF(@sCount > 0)
    BEGIN
        SET @tableName = 'news,searchOptions,bigClass'
        SET @str = @str + ' and patindex(''%|''+' + 'Convert(Varchar(50),news.bigClassId)'+ '+''|%'',searchOptions.OptionsValue)>0 and searchOptions.bigClassId = ' +STR(@NewSearchbigClassId)
	END
    ELSE IF(@sCount = 0)
    BEGIN
        SET @tableName = 'news,bigClass'
    END


	SET @str = @str + ' and news.updateTime >''' + @searchStrBegin + ''' and news.updateTime <''' + @searchStrEnd + '''' + ' and news.bigClassId = bigClass.pkId '

	SET @str = @str + ' and news.passed=1 and (news.isDuplicated<>1 or news.isDuplicated is null) and news.gradeId<='+Convert(varchar,@gradeId)
	--SET @str = @str + ' and gate<>'+Convert(varchar,@gateId)

	
	
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(news.pkId) from '+Convert(nvarchar(50),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsCount]
GO





CREATE PROCEDURE [dbo].[GetNewsCount]
 (
	-- Add the parameters for the stored procedure here
	@gradeid int=0,
	@begintime varchar(20), 
	@endtime varchar(20),
	@bigclassyn int=0,
	@bigclassid int=0,
	@passed int=1
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT OFF;
	declare @select varchar(500);  
	declare @from varchar(500);
	declare @group varchar(500) ;
	declare @where varchar(500);
    -- Insert statements for procedure here
    set @select='Select (select name from department where pkid=N.departmentId) deptname, ';
	set	@select=@select+' case U.isDel when 1 then U.username'+'+'+'''<已删除>'''+' else U.username end username,U.fullname,';
	set	@select=@select+ ' COUNT(N.pkId) fbcount,SUM(case passed when 1 then 1 else 0 end) passedcount,SUM(N.click) clicksum ';
	set @from = ' From news N, bigclass B, users U ';
	set @where =' Where N.bigclassId=B.pkId and N.usersId = U.pkId ';
	set @group = ' Group by N.departmentId,U.fullname,U.username,U.isDel ';
	
	if(@gradeid <>0)
		set @where=@where+' and N.gradeId='+STR(@gradeId);
		
	if(@begintime !='')
		set @where = @where + ' and N.updatetime >='''+ @begintime+'''';
	if(@endtime  !='')
		set @where = @where + ' and N.updatetime <=''' + @endtime+'''';			
	
	if (@bigclassYn =1 )  
		begin
			if (@bigclassId <> 0 )
			begin
			    set @where = @where + ' and N.bigclassId ='+STR(@bigclassid);
			end		
			
			set @select = @select+',B.name'	--如果选择了按栏目查，就自动按栏目分组
			set @group = @group+',B.name order by fbcount  desc ';
		end
	else
		begin
			set @select= @select+',''所有栏目''as name';
			set @group = @group+' order by fbcount desc ';
		end
	if(@passed <> 2)
	set @where=@where+' and N.passed='+STR(@passed);
	--执行查询
	EXEC(@select + @from + @where + @group );

END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsCountByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsCountByBigClassId]
GO





create PROCEDURE [dbo].[GetNewsCountByBigClassId]
(
	@bigClassId int
)
AS

select count(*) from news where bigclassid=@bigClassId





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsLayoutTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsLayoutTemplate]
GO








CREATE PROCEDURE [dbo].[GetNewsLayoutTemplate]
AS
	SET NOCOUNT ON;
SELECT * FROM layoutTemplate WHERE layoutTemplateType = 0 ORDER BY pkId 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsReleaseTopByDepartment]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsReleaseTopByDepartment]
GO



CREATE PROCEDURE GetNewsReleaseTopByDepartment
	@pageIndex int, --当前页
	@pageSize int=5, --分页数
	@bigClassPkId int, --栏目ID
	@selectType int, --查询类型
	@aparamTime datetime --当前时间
AS

BEGIN

	DECLARE @num int
	select @num=0
	select @num = count(pkId) from BigClassAssociation where thisBigClassId=@bigClassPkId
	if(@num = 0) --未选择的情况下查询所有栏目
	begin
	
			if(@selectType=21) --累计所有
			begin
				select TOP (@pageSize) N.departmentId as UserID, D.name as UserName,count(N.pkId) as NewsCount 
				from news N left join department D on N.departmentId=D.pkId 
				where passed=1 and --统计审核成功的信息
				(n.validDays > 0 OR n.validDays < 0) and 
				isDuplicated != 1 --不统计抄送信息
				group by N.departmentId, D.name 
				order by NewsCount desc
				
			end	
			else --按照时间条件查询
			begin
				
				select TOP (@pageSize) N.departmentId as UserID, D.name as UserName,count(N.pkId) as NewsCount 
				from news N left join department D on N.departmentId=D.pkId 
				where passed=1 and --统计审核成功的信息
				(n.validDays > 0 OR n.validDays < 0) and 
				isDuplicated != 1 and --不统计抄送信息
				updateTime >= @aparamTime  
				group by N.departmentId, D.name 
				order by NewsCount desc
			end
		
	end
	else
	begin
		
			if(@selectType=21) --累计所有
			begin
				select TOP (@pageSize) N.departmentId as UserID, D.name as UserName,count(N.pkId) as NewsCount 
				from news N left join department D on N.departmentId=D.pkId 
				where passed=1 and 
				(n.validDays > 0 OR n.validDays < 0) and 
				n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@bigClassPkId) and 
				isDuplicated != 1 
				group by N.departmentId, D.name 
				order by NewsCount desc
				
			end
			else --按照时间条件查询
			begin
				
				select TOP (@pageSize) N.departmentId as UserID, D.name as UserName,count(N.pkId) as NewsCount 
				from news N left join department D on N.departmentId=D.pkId 
				where passed=1 and 
				(n.validDays > 0 OR n.validDays < 0) and 
				n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@bigClassPkId) and 
				isDuplicated != 1 and
				updateTime >= @aparamTime 
				group by N.departmentId, D.name 
				order by NewsCount desc
			end	
	end

	RETURN 
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsReleaseTopByUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsReleaseTopByUser]
GO



CREATE PROCEDURE GetNewsReleaseTopByUser
	@pageIndex int, --当前页
	@pageSize int=5, --分页数
	@bigClassPkId int, --栏目ID
	@selectType int, --查询类型
	@aparamTime datetime --时间
AS

BEGIN

	DECLARE @num int
	select @num=0
	select @num = count(pkId) from BigClassAssociation where thisBigClassId=@bigClassPkId
	if(@num = 0) --未选择的情况下查询所有栏目
	begin
	
			if(@selectType=11) --累计所有
			begin
				select TOP (@pageSize) N.usersid as UserID, U.fullName as UserName,count(N.pkId) as NewsCount 
				from news N left join users U on N.usersid=U.pkId 
				where passed=1 and --统计审核成功的信息
				(n.validDays > 0 OR n.validDays < 0) and 
				isDuplicated != 1 --不统计抄送信息
				group by N.usersid, U.fullName 
				order by NewsCount desc
				
			end	
			else --按照时间条件查询
			begin
					
				select TOP (@pageSize) N.usersid as UserID, U.fullName as UserName,count(N.pkId) as NewsCount 
				from news N left join users U on N.usersid=U.pkId 
				where passed=1 and --统计审核成功的信息
				(n.validDays > 0 OR n.validDays < 0) and 
				isDuplicated != 1 and --不统计抄送信息
				updateTime >= @aparamTime 
				group by N.usersid, U.fullName 
				order by NewsCount desc
			end
			
	end
	else
	begin	
			if(@selectType=11) --累计所有
			begin
				select TOP (@pageSize) N.usersid as UserID, U.fullName as UserName,count(N.pkId) as NewsCount 
				from news N left join users U on N.usersid=U.pkId 
				where passed=1 and 
				(n.validDays > 0 OR n.validDays < 0) and 
				n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@bigClassPkId) and 
				isDuplicated != 1 
				group by N.usersid, U.fullName 
				order by NewsCount desc
				
			end	
			else --按照时间查询
			begin
				
				select TOP (@pageSize) N.usersid as UserID, U.fullName as UserName,count(N.pkId) as NewsCount 
				from news N left join users U on N.usersid=U.pkId 
				where passed=1 and 
				(n.validDays > 0 OR n.validDays < 0) and 
				n.bigClassId in (select bigClassId from BigClassAssociation where thisBigClassId=@bigClassPkId) and 
				isDuplicated != 1 and
				updateTime >= @aparamTime 
				group by N.usersid, U.fullName 
				order by NewsCount desc
			end
	end

	RETURN 
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsSearchCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsSearchCount]
GO






CREATE PROCEDURE [dbo].[GetNewsSearchCount]
    @NewSearchbigClassId int,
    @searchStr varchar(100),
	@bigClassId int,
	@type int,
	@gradeId int,
	@gateId int,
	@recordCount int output,
	@date varchar(100)
AS	
	--设置查询表名称
	DECLARE @tableName nvarchar(50)
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(1000)
    DECLARE @sCount int,@sNum int
    DECLARE @sSql nvarchar(200)
    SET @sSQL = N'SELECT @count=Count(searchOptions.pkId) from searchOptions where bigClassId = ' + STR(@NewSearchbigClassId)
    EXEC sp_ExecuteSQL @sSQL,N'@count int output',@sNum output
    SELECT @sCount = @sNum

	SET @str = ' validDays <> 0 '
	SET @searchStr = '%'+Convert(varchar,@searchStr)+'%'
	IF (@bigClassId <> 0)
    BEGIN
        SET @tableName = 'news'
		SET @str = @str + ' and news.bigClassId='+STR(@bigClassId)
    END
    ELSE IF(@sCount > 0)
    BEGIN
        SET @tableName = 'news,searchOptions'
        SET @str = @str + ' and patindex(''%|''+' + 'Convert(Varchar(50),news.bigClassId)'+ '+''|%'',searchOptions.OptionsValue)>0 and searchOptions.bigClassId = ' +STR(@NewSearchbigClassId)
	END
    ELSE IF(@sCount = 0)
    BEGIN
        SET @tableName = 'news'
    END

    IF (@type=0)
		BEGIN
			SET @str = @str + ' and (listTitle like ''' + @searchStr + ''''
			SET @str = @str + ' or intactTitle like ''' + @searchStr + ''''
			SET @str = @str + ' or subheading like ''' + @searchStr + ''''
			SET @str = @str + ' or content like ''' + @searchStr + ''''
			SET @str = @str + ' or author like ''' + @searchStr + ''')' 
		END
	IF(@type=1)
		BEGIN
			SET @str = @str + ' and (listTitle like ''' + @searchStr + ''''
			SET @str = @str + ' or intactTitle like ''' + @searchStr + ''''
			SET @str = @str + ' or subheading like ''' + @searchStr + ''')'
		END
	IF(@type=2)
		SET @str = @str + ' and author like ''' + @searchStr + ''''
	IF(@type=3)
		SET @str = @str + ' and content like ''' + @searchStr + ''''
	IF(@type=5)
		SET @str = @str + ' and copyFrom like ''' + @searchStr + ''''

	SET @str = @str + ' and passed=1 and (isDuplicated<>1 or isDuplicated is null) and gradeId<='+Convert(varchar,@gradeId)+@date
	--SET @str = @str + ' and gate<>'+Convert(varchar,@gateId)

	
	
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(news.pkId) from '+Convert(nvarchar(50),@tableName)+' where ' + Convert(nvarchar(1000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum
RETURN




set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetNewsUnitStat]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetNewsUnitStat]
GO




CREATE PROCEDURE [dbo].[GetNewsUnitStat] 
(
              @StartTime          datetime,
              @EndTime           datetime,
              @UnitIds               nvarchar(2000),
              @TotalUnits        int output,
              @Totalnews        int output,
              @Totalmedias     int output,
              @Totalnums       int output
)
AS
BEGIN

          DECLARE @sql    nvarchar(4000)
          DECLARE @where   nvarchar(4000)

	SET @where = ' WHERE ISNUMERIC(CopyFrom)=1 and DATEDIFF(day, ''' + CONVERT(nvarchar, @StartTime, 120) + ''', updatetime) >= 0 and 
						 DATEDIFF(day, ''' + CONVERT(nvarchar, @endtime, 120) + ''', updatetime) <= 0  '

            IF(@UnitIds <> '0')
              BEGIN SET @where=@where+' and CopyFrom in('+@UnitIds+')'
                END
          SET @sql='
          
          if object_id(''tempdb..#AllUsers'') is NOT NULL
            BEGIN
            drop table #AllUsers	
            END

		    CREATE TABLE #AllUsers
			(    
				UserID	int
			)
           insert	into #AllUsers (UserID)
           select usersID 
           from news group by usersID
           
          if object_id(''tempdb..#AllNews'') is NOT NULL
          BEGIN
          drop table #AllNews
          END
          
          CREATE TABLE #AllNews
          (
              UnitId nvarchar(50),
              articlenum int,
              medianum  int
          )          
          insert into #AllNews(UnitId)
          select CopyFrom from news '+@where+' group by CopyFrom
              
          update #AllNews
          set articlenum=
          ( select count(1) 
          from news '+@where+' and CopyFrom=#AllNews.UnitId and bigClassId in(SELECT b.pkId FROM bigClass b,template t 
          where b.templateId=t.pkId and t.code<>''mediaNews'' and t.typeId=2))
          
         
          update #AllNews
          set medianum=(
          select count(1) from news '+@where+' and CopyFrom=#AllNews.UnitId and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.code=''mediaNews'' and t.typeId=2))
         
         select UnitId,articlenum,medianum,articlenum+medianum as num,articlenum+medianum*5 as score
         from #Allnews order by score desc
          
            select @InternalTotalUnits=count(1) from #AllNews
           select @InternalTotalnews=count(pkId) from news '+@where+' and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.code<>''mediaNews'' and t.typeId=2)
           select @InternalTotalmedias=count(pkId) from news  '+@where+' and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.code=''mediaNews'')
         select @InternalTotalnums=count(pkId) from news '+@where+' and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.typeId=2)
   
            if object_id(''tempdb..#AllUsers'') is NOT NULL
            BEGIN
            drop table #AllUsers	
            END
   
          if object_id(''tempdb..#Allnews'') is NOT NULL
          BEGIN
          drop table #AllNews
          END
           '       
	EXEC sp_ExecuteSQL @sql, 
	N'@InternalTotalUnits int output, @InternalTotalnews int output, @InternalTotalmedias int output,@InternalTotalnums int output', 
	@TotalUnits output ,@Totalnews	output, @Totalmedias output,@Totalnums output
	return
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOperationLogByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetOperationLogByPage]
GO








--region [dbo].[GetOperationLogByPage]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[GetOperationLogByPage]
-- Date Generated: 2008
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[GetOperationLogByPage]
	@searchStr varchar(500),
	@pageIndex int,
	@pageSize int,
	@recordCount int output,
	@pageCount int output
AS
	SELECT @pageIndex = @pageIndex - 1
	
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) FROM OperationLog WHERE ' + @searchStr
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	SET @pageCount = (@recordCount + @pageSize-1)/@pageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM OperationLog WHERE ' + @searchStr + ' ORDER BY pkId DESC'	
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM OperationLog WHERE ' + @searchStr + ' ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'	
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' * FROM OperationLog WHERE ' + @searchStr + ' ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'
	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' * FROM OperationLog WHERE ' + @searchStr + ' ORDER BY pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END
	EXEC(@sqlStr)
	RETURN
--endregion








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOperationLogByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetOperationLogByPkId]
GO










create PROCEDURE [dbo].[GetOperationLogByPkId]
	@pkId int
AS
	
	SELECT *
	FROM OperationLog	
	where pkId = @pkId
	
	RETURN










GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPeopleByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPeopleByBigClassId]
GO






CREATE PROCEDURE [dbo].[GetPeopleByBigClassId]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
BEGIN

	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	p.pkId
	FROM	[people] AS p 
			INNER JOIN [Users] AS u ON p.userId=u.pkId
			INNER JOIN [grade] AS g ON p.gradeId=g.pkId
	WHERE	(p.gate > @gateId OR p.gate < @gateId) and   
			bigClassId =		@bigClassId and 
			p.gradeId <=		@gradeId 

	ORDER BY p.istop DESC, p.pkId DESC

	SELECT  p.*,u.userName as userName,u.fullName,g.name as grade
	FROM	[people] AS p 
			INNER JOIN [Users] AS u ON p.userId=u.pkId
			INNER JOIN [grade] AS g ON p.gradeId=g.pkId
			INNER JOIN [#PageIndexs] AS pp ON p.PKID = pp.ID
	WHERE	pp.IndexId >= @PageLowerBound AND pp.IndexId <= @PageUpperBound
	ORDER BY p.istop DESC, p.pkId DESC
	
	DELETE #PageIndexs
	
	RETURN 
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPeopleByBigClassIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPeopleByBigClassIdAndPassed]
GO



CREATE PROCEDURE GetPeopleByBigClassIdAndPassed
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int,
@passed int=1
AS
BEGIN

	SET @currentPage = @currentPage - 1
	IF (@currentPage < 0)
		BEGIN
			SET @currentPage = 1
		END
	-- Set the page bounds
    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT
    DECLARE @TotalRecords   INT
    SET @PageLowerBound = @PageSize * @currentPage
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    -- Create a temp table TO store the select results
    CREATE TABLE #PageIndexs
    (
        IndexId int IDENTITY (0, 1) NOT NULL,
        ID int
    )

    -- Insert into our temp table
    INSERT	INTO #PageIndexs (ID)
	SELECT	p.pkId
	FROM	[people] AS p 
			INNER JOIN [Users] AS u ON p.userId=u.pkId
			INNER JOIN [grade] AS g ON p.gradeId=g.pkId
	WHERE	(p.gate > @gateId OR p.gate < @gateId) and   
			bigClassId = @bigClassId and 
			p.gradeId <= @gradeId and 
			passed = @passed
	ORDER BY p.istop DESC, p.pkId DESC

	SELECT  p.*,u.userName as userName,u.fullName,g.name as grade
	FROM	[people] AS p 
			INNER JOIN [Users] AS u ON p.userId=u.pkId
			INNER JOIN [grade] AS g ON p.gradeId=g.pkId
			INNER JOIN [#PageIndexs] AS pp ON p.PKID = pp.ID
	WHERE pp.IndexId >= @PageLowerBound AND pp.IndexId <= @PageUpperBound
	ORDER BY p.istop DESC, p.pkId DESC
	
	DELETE #PageIndexs
	
	RETURN 
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPopWindow]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPopWindow]
GO







CREATE PROCEDURE [dbo].[GetPopWindow]
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
	SET NOCOUNT ON;
  
  	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' p.*,u.userName "userName",u.fullName,g.name "grade" from popwindow p,users u,grade g where p.gate<>'+Convert(varchar,@gateId)+' and p.gradeId<='+convert(varchar,@gradeId)+' and p.gradeId=g.pkId and p.userId=u.pkId and p.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from popwindow where p.gate<>'+Convert(varchar,@gateId)+' and p.gradeId<='+convert(varchar,@gradeId)+' order by istop desc,pkId desc) order by istop desc,p.pkId desc'
Execute (@vSQL)	
return


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPopWindowBypassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPopWindowBypassed]
GO







CREATE PROCEDURE [dbo].[GetPopWindowBypassed]
@gradeId int=1,
@gateId int
AS
	SET NOCOUNT ON
	select *
	from popWindow
	where passed=1
	  and gradeId<=@gradeId
	  and popDay>0
	  and gate<>@gateId
	order by isTop asc,ordering asc,pkId asc
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPopWindowByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPopWindowByPkId]
GO







CREATE PROCEDURE [dbo].[GetPopWindowByPkId]
@pkId int,
@gradeId int,
@gateId int
AS
	SET NOCOUNT ON;
SELECT p.*,g.name 'grade' FROM popWindow p,grade g
 where p.pkId=@pkId
   and gradeId<=@gradeId
   and p.gradeId=g.pkId
   and p.gate<>@gateId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPopWindowByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPopWindowByUserId]
GO







CREATE PROCEDURE [dbo].[GetPopWindowByUserId]
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@userId int,
@gateId int
AS
	SET NOCOUNT ON;
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' p.*,u.userName "userName",u.fullName,g.name "grade" from popWindow p,users u,grade g where p.gate<>'+Convert(varchar,@gateId)+' and  p.gradeId<='+convert(varchar,@gradeId)+' and userId='+convert(varchar,@userId)+' and p.userId=u.pkId and p.gradeId=g.pkId and p.pkId < all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from popWindow where p.gate<>'+Convert(varchar,@gateId)+' and p.gradeId<='+convert(varchar,@gradeId)+' and userId='+convert(varchar,@userId)+' order by istop desc,pkId desc) order by istop desc,p.pkId desc'
Execute (@vSQL)	



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPushNewsByNewsId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPushNewsByNewsId]
GO

CREATE PROCEDURE GetPushNewsByNewsId    
    (    
		@type varchar(10),   
		@newsid int
     )    
     AS    
 SELECT * FROM pushNews WHERE (type=@type and newsid=@newsid and state='未推送')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPushNewsByType]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetPushNewsByType]
GO

CREATE PROCEDURE GetPushNewsByType  
    (    
		@type varchar(10)
     )    
     AS    
 SELECT * FROM pushNews WHERE (type=@type and state='未推送')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetRegServiceList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetRegServiceList]
GO








create PROCEDURE [dbo].[GetRegServiceList]

AS
	
	SELECT *
	FROM RegServiceList	
	  
	order by pkId  desc
	RETURN








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetRegServiceListByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetRegServiceListByPkId]
GO








create PROCEDURE [dbo].[GetRegServiceListByPkId]
	@pkId int
AS
	
	SELECT *
	FROM RegServiceList	
	where pkId = @pkId
	
	RETURN








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetRegServiceListByRegServiceCodeAndServiceId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetRegServiceListByRegServiceCodeAndServiceId]
GO









create PROCEDURE [dbo].[GetRegServiceListByRegServiceCodeAndServiceId]
	@regServiceCode varchar(250),
	@serviceId int
AS
	
	SELECT *
	FROM RegServiceList	
	where regServiceCode = @regServiceCode and serviceId= @serviceId
	
	RETURN









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetReviewByNewsIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetReviewByNewsIdAndPassed]
GO







CREATE PROCEDURE [dbo].[GetReviewByNewsIdAndPassed]
@newsId int=null,
@gradeId int=1
AS
	SET NOCOUNT ON 
	select * from review where newsId=@newsId and passed=1 order by pkId desc
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSearchAllNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSearchAllNews]
GO

CREATE PROCEDURE GetSearchAllNews
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@searchStr varchar(200) = '',	 
	@recordCount int output
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
    --这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId group by b.pkId,m.name
	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	--SET @str = @str + ' and (n.passed=0 or n.passed=1 or n.passed=2)'
	--SET @str = @str + ' and n.passed=1'
	SET @str = @str + ' and (n.passed=1 or ((n.passed=0 or n.passed=2) and n.usersId='+STR(@userId) +'))'
	SET @str = @str + ' and n.gradeId<='+STR(@gradeId) 
	--SET @str = @str + ' and (n.usersId='+STR(@userId)
	--SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId '
	--SET @str = @str + ' and (n.duplicatedPkId=0 or n.duplicatedPkId is null)'
	IF(@searchStr <> '')
		SET @str = @str + @searchStr
	
	SET @tableName = '#test z, news n,users u,department d,grade g, template t, bigClass b '

end
else
begin

	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
    --这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid group by b.pkId,m.name
	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	--SET @str = @str + ' and (n.passed=0 or n.passed=1 or n.passed=2)' 
	--SET @str = @str + ' and n.passed=1'
	SET @str = @str + ' and (n.passed=1 or ((n.passed=0 or n.passed=2) and n.usersId='+STR(@userId) +'))'
	SET @str = @str + ' and n.gradeId<='+STR(@gradeId)
	--SET @str = @str + ' and n.usersId='+STR(@userId) 
	--SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId '
	--SET @str = @str + ' and (n.duplicatedPkId=0 or n.duplicatedPkId is null)'
	IF(@searchStr <> '')
		SET @str = @str + @searchStr
	
	SET @tableName = '#test1 z, news n,users u,department d,grade g, template t, bigClass b '

end

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'ntime ASC, sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'ntime DESC, sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.pkId
	,n.specialId,n.gradeId,n.gate,n.departmentId,n.listTitle,n.intactTitle,n.subheading,n.isRecommand,n.isTop,n.useListTitle,n.showReviewLink
	,n.Author,n.AuthorMail,n.CopyFrom,n.copyFromLink,n.about,n.pageType,n.autoPageCharNum,n.readPoint,n.itemType
	,n.descript,n.usersId,n.editor,n.click,n.updateTime,n.updateTime as ntime ,n.passed,n.isReview,n.isImageNews,n.imageName,n.width,n.height,n.MenuId,n.bigClassId,n.updateIp,n.useSubHeading,n.isDuplicated
	,n.duplicateFrom,n.duplicatedPkId,n.validDays,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department",g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSearchAllNewsByUserId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSearchAllNewsByUserId]
GO

CREATE PROCEDURE GetSearchAllNewsByUserId
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize
	@gradeId int,
	@userId int,
	@gateId int,
	@manageAll varchar(8000),
	@searchStr varchar(200) = '',	 
	@recordCount int output
AS	
	--转换成索引
	SELECT @currentPage = @currentPage - 1
	
	--初始化查询条件(不带where)
	DECLARE @str nvarchar(4000)
	--设置查询表名称
	DECLARE @tableName nvarchar(100)
--判断当前用户是否属于用户组
	Declare @groupid int 
select @groupid = usersGroupId from users where pkid=@userId
if(@groupid=1)
begin
	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
    --这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test from bigClass b, menu m, moduleAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and usersId=@userId group by b.pkId,m.name
	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and (n.passed=0 or n.passed=1 or n.passed=2)'
	SET @str = @str + ' and n.gradeId<='+STR(@gradeId) 
	SET @str = @str + ' and n.usersId='+STR(@userId) 
	--SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId '
	--SET @str = @str + ' and (n.duplicatedPkId=0 or n.duplicatedPkId is null)'
	IF(@searchStr <> '')
		SET @str = @str + @searchStr
	
	SET @tableName = '#test z, news n,users u,department d,grade g, template t, bigClass b '

end
else
begin

	--用临时表先读取授权的栏目和MenuName，然后在读取授权的新闻，
    --这样设计不会对news表中的menuId产生依赖，避免因栏目移动产生错误
	select b.pkId bigclassId, m.name menuName,max(m.pkId) sourceMenuId into #test1 from bigClass b, menu m, groupAssign a where m.bigClassId=b.pkId and m.pkId=a.menuId and groupId=@groupid group by b.pkId,m.name
	SET @str = ' 1=1 '
	SET @str = @str + ' and n.bigclassId = z.bigclassId'
	SET @str = @str + ' and z.bigclassId = b.pkid'
	SET @str = @str + ' and b.templateId = t.pkId'
	SET @str = @str + ' and (n.passed=0 or n.passed=1 or n.passed=2)'
	SET @str = @str + ' and n.gradeId<='+STR(@gradeId)
	SET @str = @str + ' and n.usersId='+STR(@userId) 
	--SET @str = @str + ' and n.gate<>'+STR(@gateId)
	SET @str = @str + ' and n.usersId=u.pkId and n.gradeId=g.pkId and n.departmentId=d.pkId '
	--SET @str = @str + ' and (n.duplicatedPkId=0 or n.duplicatedPkId is null)'
	IF(@searchStr <> '')
		SET @str = @str + @searchStr
	
	SET @tableName = '#test1 z, news n,users u,department d,grade g, template t, bigClass b '

end

	--设置查询排序（不带order by）
	DECLARE @orderTypeA nvarchar(100)
	DECLARE @orderTypeD nvarchar(100)
	SET @orderTypeA = 'ntime ASC, sourceMenuId ASC, newsId ASC'
	SET @orderTypeD = 'ntime DESC, sourceMenuId DESC, newsId DESC'
	
	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(4000)

	SET @vSQL = N'SELECT @count = Count(n.pkId) from '+Convert(nvarchar(100),@tableName)+' where ' + Convert(nvarchar(4000),@str)
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(4000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department",g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' and b.pkId = n.bigClassId and b.templateId = t.pkId ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId , t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' n.pkId
	,n.specialId
	,n.gradeId
	,n.gate
	,n.departmentId
	,n.listTitle
	,n.intactTitle
	,n.subheading
	,n.isRecommand
	,n.isTop
	,n.useListTitle
	,n.showReviewLink
	,n.Author
	,n.AuthorMail
	,n.CopyFrom
	,n.copyFromLink
	,n.about
	,n.pageType
	,n.autoPageCharNum
	,n.readPoint
	,n.itemType
	,n.descript
	,n.usersId
	,n.editor
	,n.click
	,n.updateTime
	,n.updateTime as ntime 
	,n.passed
	,n.isReview
	,n.isImageNews
	,n.imageName
	,n.width
	,n.height
	,n.MenuId
	,n.bigClassId
	,n.updateIp
	,n.useSubHeading
	,n.isDuplicated
	,n.duplicateFrom
	,n.duplicatedPkId
	,n.validDays
	,n.mediaAutostart
	,n.AuditUserID,n.isStatic,n.staticUrl,n.staticTime,u.userName,u.fullName,d.name "department" ,g.name "grade",z.menuName,z.sourceMenuId, n.pkId as newsId, t.code FROM '+Convert(nvarchar(100),@tableName)+' where '+Convert(nvarchar(4000),@str)+' ORDER BY '+Convert(nvarchar(100),@orderTypeD)+') TempTable1 '
					+ ' ORDER BY '+Convert(nvarchar(100),@orderTypeA)+') TempTable2 ORDER BY '+Convert(nvarchar(100),@orderTypeD)
	END

	--执行查询
	EXEC(@sqlStr)
RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetsearchOptions]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetsearchOptions]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE GetsearchOptions
	-- Add the parameters for the stored procedure here
	(@bigClassId int)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT * from searchOptions where bigClassId = @bigClassId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetServiceClassByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetServiceClassByPkId]
GO









create PROCEDURE [dbo].[GetServiceClassByPkId]
	@pkId int
AS
	
	SELECT *
	FROM ServiceClass	
	where pkId = @pkId
	
	RETURN









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetShowItem]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetShowItem]
GO



CREATE PROCEDURE GetShowItem
@gradeId int=1,
@parentId int,
@gateId int
AS
	select m.*,d.*,t.code,b.name as bigclassname
	  from menu m 
	  inner join bigClass b on b.pkId=m.bigClassId
	  inner join template t on t.pkId=b.templateId
	  left  join bigclassDesign d on  d.menuId =m.pkId
	  where m.parentId=@parentId
	    and m.gate<>@gateId
	    and m.gradeId<=@gradeId
	order by m.pkId desc,ordering desc
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpBuildField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpBuildField]
GO







CREATE PROCEDURE [dbo].[GetSignUpBuildField]
	@PkId int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	*
FROM
	[dbo].[SignUpBuildField]
WHERE
	[PkId] = @PkId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpBuildFieldByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpBuildFieldByBigClassId]
GO







CREATE PROCEDURE [dbo].[GetSignUpBuildFieldByBigClassId]
   @BigClassId int
AS
	SET NOCOUNT ON;
  select f.* from SignUpBuildField f, SignUpRelation r 
  where f.TableName = r.TableName and r.BigClassId = @BigClassId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpBuildFieldByTableName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpBuildFieldByTableName]
GO







CREATE PROCEDURE [dbo].[GetSignUpBuildFieldByTableName]
   @TableName varchar(100)
AS
	SET NOCOUNT ON;
  select * from SignUpBuildField where TableName = @TableName







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpFieldByName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpFieldByName]
GO








CREATE PROCEDURE [dbo].[GetSignUpFieldByName]
@Name varchar(50)
AS
	SET NOCOUNT ON;
SELECT * FROM SignUpField
WHERE Name=@Name







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpFieldByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpFieldByPkId]
GO







CREATE PROCEDURE [dbo].[GetSignUpFieldByPkId]
@PkId int
AS
	SET NOCOUNT ON;
SELECT * FROM SignUpField
WHERE @PkId=PkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSignUpRelationByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSignUpRelationByBigClassId]
GO







CREATE PROCEDURE [dbo].[GetSignUpRelationByBigClassId]
@BigClassId int
AS
	SET NOCOUNT ON;
SELECT * FROM SignUpRelation
WHERE BigClassId=@BigClassId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSiteContributeByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSiteContributeByPkId]
GO








CREATE PROCEDURE [dbo].[GetSiteContributeByPkId]
@pkId int =null
AS
	 SET NOCOUNT ON 
	select *
	from siteContribute
	where pkId=@pkId
	  
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSiteContributeByState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSiteContributeByState]
GO







CREATE PROCEDURE [dbo].[GetSiteContributeByState]
	@currentPage int=1,		--@pageIndex
	@pageSize int=7,		--@pageSize	
	@state int,
	@recordCount int output	
AS	
	declare @searchStr varchar(1000)
	if (@state = -1)
		set @searchStr = '1=1'
	else
		set @searchStr = 'state=' + CONVERT(nvarchar,@state)

	--转换成索引
	SELECT @currentPage = @currentPage-1	

	--先获取记录总数，保存在@recordCount
	DECLARE @tempNum int, @pageCount int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(pkId) FROM siteContribute  WHERE '+@searchStr
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	--计算页数
	SET @pageCount = (@recordCount +@pageSize-1)/@pageSize

	--构造查询语句
	DECLARE @sqlStr varchar(1000),@size int

	--判断分页大小，查询最后一页用
	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	
	
	--查询第一页
	IF (@currentPage = 0) OR (@currentPage > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM siteContribute '
					+ 'WHERE '+@searchStr+' ORDER BY pkId DESC'	
	--查询最后一页
	ELSE IF @currentPage = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM siteContribute '
					+ 'WHERE '+@searchStr+' ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'	
	--查询中间页后面的页
	ELSE IF @currentPage > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @currentPage ) + ' *  FROM siteContribute '
					+ 'WHERE '+@searchStr+' ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'
	--查询中间页前面的页
 	ELSE BEGIN							
		SET @currentPage=@currentPage+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @currentPage ) + ' *  FROM siteContribute '
					+ 'WHERE '+@searchStr+' ORDER BY pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END

	--执行查询
	EXEC(@sqlStr)

RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSiteContributeClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSiteContributeClass]
GO







CREATE PROCEDURE [dbo].[GetSiteContributeClass]

AS
	/* SET NOCOUNT ON */
	SELECT b.*,
	       b.pkId "bigClassId"
	FROM bigClass b,siteContributeClass c
	where b.pkId=c.bigClassId
	  
	order by c.pkId  desc
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSiteContributeClassById]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSiteContributeClassById]
GO








CREATE PROCEDURE [dbo].[GetSiteContributeClassById]
@pkId int
AS
	/* SET NOCOUNT ON */
	SELECT * FROM siteContributeClass where pkId=@pkId
	
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSMSRecord]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSMSRecord]
GO


create PROCEDURE GetSMSRecord
(
	@sphone int,
	@stime int
)
AS
	--东莞局门户留言本短信验证码检测。根据手机号码查询每天已发送短信的条数
	select * from SMSRecord where SPhone=@sphone and STime > @stime
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSMSServer]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSMSServer]
GO



CREATE PROCEDURE [dbo].[GetSMSServer]
(
	@sid int
)
AS
	--东莞局门户的留言本玄武短信接口的服务信息查询
	select * from SMSServer where [Sid]=@sid
	



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSpecialShowTypeBybigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSpecialShowTypeBybigClassId]
GO





CREATE PROCEDURE GetSpecialShowTypeBybigClassId
	-- Add the parameters for the stored procedure her
@bigClassId int
as
SELECT * from DownloadfileSpecialTemplate 
where bigClassId = @bigClassId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetStat]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetStat]
GO

CREATE PROCEDURE GetStat
AS
	 SET NOCOUNT ON 
	 select sum(time)+(select pvalue from sitepara where pcode='stattime') as pvtime from stat

RETURN 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTemplate]
GO







CREATE PROCEDURE [dbo].[GetTemplate]
AS
	SET NOCOUNT ON
	select *
	from template
	order by typeId, pkId	-- 按照类别排序
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTemplateByMenuId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTemplateByMenuId]
GO







CREATE PROCEDURE [dbo].[GetTemplateByMenuId]
(
	@menuId int
)
AS
	SELECT 
		t.pkId as templateId, 
		t.name as templateName, 
		t.code as templateCode, 
		b.pkId as bigClassId,
		b.name as bigClassName,
		m.pkId as menuId,
		m.name as menuName
FROM template t, bigClass b, menu m
WHERE m.bigClassId = b.pkId AND b.templateId = t.pkId AND m.pkId = @menuId









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTemplateBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTemplateBypkId]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE GetTemplateBypkId 
	-- Add the parameters for the stored procedure here
	@newsId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT * from template,bigClass,news where template.pkId = bigClass.templateId and news.bigClassId = bigClass.pkId and news.pkId = @newsId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTemplatePrivilegeSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTemplatePrivilegeSet]
GO








CREATE PROCEDURE [dbo].[GetTemplatePrivilegeSet]
AS
	SELECT 
		t.pkId as templateId, 
		t.name as templateName, 
		t.code as templateCode, 
		p.name as privilegeName, 
		p.nameRemark as nameRemark, 
		p.accreditValue as accreditValue, 
		p.accreditRemark as accreditRemark, 
		p.showType as showType, 
		p.itemEnglish as itemEnglish,
		p.itemChinese as itemChinese
FROM template AS t 
		INNER JOIN
     templatePrivilege AS tp ON t.pkId = tp.templateId 
		INNER JOIN
     privilege AS p ON tp.privilegeId = p.pkId
ORDER BY t.pkId, p.pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTemplateType]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTemplateType]
GO








CREATE PROCEDURE [dbo].[GetTemplateType]
AS
	SET NOCOUNT ON;
SELECT * FROM templateType







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTodayInHistoryByBigClassIdAndisImageNewsAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTodayInHistoryByBigClassIdAndisImageNewsAndPassed]
GO







CREATE PROCEDURE [dbo].[GetTodayInHistoryByBigClassIdAndisImageNewsAndPassed]
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@passed int=1,
@gateId int,
@happenDate varchar(100)
AS
	 SET NOCOUNT ON 	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' n.*,t.happenType,t.happenDate,u.userName,d.name "department" from news n,todayInHistory t,users u,department d where  n.gate<>'+Convert(varchar,@gateId)+' and   n.usersId=u.pkId and n.pkId = t.newsId and month(t.happenDate) = '+ substring(@happenDate,1,2)+' and day(t.happenDate) = '+ substring(@happenDate,3,2)+' and n.departmentId=d.pkId and n.bigClassId in (select pkId from bigClass where templateId = 54) and isImageNews = 1 and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  and n.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from news where  gate<>'+Convert(varchar,@gateId)+' and   bigClassId in (select pkId from bigClass where templateId = 54) and isImageNews = 1 and gradeId<='+convert(varchar,@gradeId)+' and passed='+convert(varchar,@passed)+' order by istop desc,pkId desc)  order by t.happenType asc,n.istop desc,n.pkId desc'
Execute (@vSQL)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTodayInHistoryByBigClassIdAndPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTodayInHistoryByBigClassIdAndPassed]
GO








CREATE PROCEDURE [dbo].[GetTodayInHistoryByBigClassIdAndPassed]
@bigClassId int=null,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@passed int=1,
@gateId int,
@happenDate varchar(100)
AS
	 SET NOCOUNT ON 	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' n.*,t.happenType,t.happenDate,u.userName,d.name "department" from news n,todayInHistory t,users u,department d where  n.gate<>'+Convert(varchar,@gateId)+' and   n.usersId=u.pkId and n.pkId = t.newsId and month(t.happenDate) = '+ substring(@happenDate,1,2)+' and day(t.happenDate) = '+ substring(@happenDate,3,2)+' and n.departmentId=d.pkId and bigClassId='+Convert(varchar,@bigClassId)+' and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  and n.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from news where  gate<>'+Convert(varchar,@gateId)+' and   bigClassId='+Convert(varchar,@bigClassId)+' and gradeId<='+convert(varchar,@gradeId)+' and passed='+convert(varchar,@passed)+' order by istop desc,pkId desc)  order by istop desc,n.pkId desc'
Execute (@vSQL)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTodayInHistoryByBigClassIdCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTodayInHistoryByBigClassIdCount]
GO







create PROCEDURE [dbo].[GetTodayInHistoryByBigClassIdCount]
@bigClassId int=null,
@gradeId int=1,
@gateId int=-1,
@happenDate varchar(100),
@count int output,
@isForeGround int
AS
	SET NOCOUNT ON
	declare @num int
	declare @vSQL varchar(1000)
	IF (@isForeGround=0)
	BEGIN
		select @vSQL=
			'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news n,todayInHistory t where n.gate<> '+convert(varchar,@gateId)+' and n.bigclassId= '+convert(varchar,@bigclassId)+' and n.gradeId <= '+convert(varchar,@gradeId)+' and n.pkId = t.newsId and month(t.happenDate) = '+ substring(@happenDate,1,2)+' and day(t.happenDate) = '+ substring(@happenDate,3,2)+' return'			--魏兆佳20070801
				
	END 
	ELSE IF (@isForeGround=1)
	BEGIN
		select @vSQL=
			 'ALTER procedure GetCount @count int output as set nocount on select @count=Count(*) from news n,todayInHistory t where n.gate<> '+convert(varchar,@gateId)+' and n.bigclassId= '+convert(varchar,@bigclassId)+' and n.gradeId <= '+convert(varchar,@gradeId)+' and n.pkId = t.newsId and month(t.happenDate) = '+ substring(@happenDate,1,2)+' and day(t.happenDate) = '+ substring(@happenDate,3,2)+' and passed = 1 return'		--魏兆佳20070801
				
	END	
	Execute (@vSQL)
	execute GetCount @count=@num output
	select @count=@num
RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTodayInHistoryByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTodayInHistoryByPkId]
GO







CREATE PROCEDURE [dbo].[GetTodayInHistoryByPkId]
@pkId int =null,
@gradeId int=1,
@gateId int
AS
	 SET NOCOUNT ON 
	select n.*,t.happenType,t.happenDate,u.userName,d.name "department" ,g.name "grade" 
	from news n, todayInHistory t, users u, department d, grade g
	where @pkId=n.pkId
	  and n.gradeId<=@gradeId
	  and n.gate<>@gateId and n.pkId = t.newsId and n.departmentId=d.pkId and n.gradeId=g.pkId and n.usersId=u.pkId 
	RETURN 






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTodayInHistoryShortByPassed]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTodayInHistoryShortByPassed]
GO







CREATE PROCEDURE [dbo].[GetTodayInHistoryShortByPassed]
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@passed int=1,
@gateId int,
@happenDate varchar(100)
AS
	 SET NOCOUNT ON 	
	declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' n.*,t.happenType,t.happenDate,u.userName,d.name "department" from news n,todayInHistory t,users u,department d where  n.gate<>'+Convert(varchar,@gateId)+' and   n.usersId=u.pkId and n.pkId = t.newsId and month(t.happenDate) = '+ substring(@happenDate,1,2)+' and day(t.happenDate) = '+ substring(@happenDate,3,2)+' and n.departmentId=d.pkId and n.bigClassId in (select pkId from bigClass where templateId = 54) and n.gradeId<='+convert(varchar,@gradeId)+' and n.passed='+convert(varchar,@passed)+'  and n.pkId <> all (select top '+Convert(varchar,((@currentPage-1)*@pageSize))+' pkId from news where  gate<>'+Convert(varchar,@gateId)+' and   bigClassId in (select pkId from bigClass where templateId = 54) and gradeId<='+convert(varchar,@gradeId)+' and passed='+convert(varchar,@passed)+' order by istop desc,pkId desc)  order by t.happenType asc,n.istop desc,n.pkId desc'
Execute (@vSQL)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTopicReviewByNewsId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTopicReviewByNewsId]
GO





CREATE PROCEDURE [dbo].[GetTopicReviewByNewsId]
	@searchStr varchar(500),
	@pageIndex int,
	@pageSize int,
	@pageCount int output,
	@recordCount int output
AS 
   SELECT @pageIndex = @pageIndex - 1
	
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(review.newsId) FROM review WHERE ' + @searchStr
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	SET @pageCount = (@recordCount + @pageSize-1)/@pageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM review WHERE ' + @searchStr + ' ORDER BY review.pkId DESC'	
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM review WHERE ' + @searchStr + ' ORDER BY review.pkId ASC) TempTable ORDER BY pkId DESC'	
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' * FROM review WHERE ' + @searchStr + ' ORDER BY review.pkId ASC) TempTable ORDER BY pkId DESC'
	ELSE BEGIN							
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' * FROM review WHERE ' + @searchStr + ' ORDER BY review.pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END
	EXEC(@sqlStr)
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTopOneNewsByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetTopOneNewsByBigClassId]
GO



CREATE PROCEDURE GetTopOneNewsByBigClassId
	@bigClassId int 
AS 
	select width,height from news where pkId=(select MAX(pkId) from news where bigClassId=@bigClassId and isDuplicated=0)
	



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUnitsNewsList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUnitsNewsList]
GO




CREATE PROCEDURE [dbo].[GetUnitsNewsList]
(
	          @PageIndex		int,
	          @PageSize		    int,
              @StartTime        datetime,
              @EndTime          datetime,
              @UnitIds           nvarchar(2000),
              @Totalpass        int output,
              @Totalnews        int output
)
AS
BEGIN

	SET @PageIndex = @PageIndex - 1
	IF (@PageIndex < 0)
		BEGIN
			SET @PageIndex = 1
		END


    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT    
    SET @PageLowerBound = @PageSize * @PageIndex
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
      
      DECLARE @sql nvarchar(4000)
      DECLARE @where nvarchar(4000)
      
	  SET @where = ' WHERE ISNUMERIC(CopyFrom)=1 and DATEDIFF(day, ''' + CONVERT(nvarchar, @StartTime, 120) + ''', updatetime) >= 0 and 
						 DATEDIFF(day, ''' + CONVERT(nvarchar, @endtime, 120) + ''', updatetime) <= 0  '
      
      IF(@UnitIds<>'0')
      BEGIN
          SET @where=@where+' and CopyFrom in ('+@UnitIds+')'
      END
      
      SET @sql='
      
      IF object_id(''tempdb..#AllNews'') IS NOT NULL
		BEGIN
			DROP TABLE #AllNews
		END
      
      CREATE TABLE #AllNews
      (
        IndexId int IDENTITY (0, 1) NOT NULL,
        BigClassId   INT,
        intactTitle  NVARCHAR(100),
        updatetime   DATETIME,
        Author       NVARCHAR(50),
        assessorId     INT,
        pass         INT,
        bigClassName  NVARCHAR(50),
        UserName      NVARCHAR(50)
      )
      
      
      insert into #AllNews(BigClassId,intactTitle,updatetime,Author,assessorId,pass)
      select bigClassId,intactTitle,updatetime,Author,AuditUserId,passed
      from news
      '+@where+' and bigClassId in(SELECT b.pkId FROM bigClass b,template t where 
   b.templateId=t.pkId and t.typeId=2) order by updatetime desc
      
      update #AllNews
      set bigClassName=
      (
        select name from bigClass as a where a.pkId=#AllNews.BigClassId
      )
      
      update #AllNews
      set UserName=
      (
        select fullName from Users as a where a.pkId=#AllNews.assessorId 
      )
      
      select bigClassName,intactTitle,convert(char(10),updatetime,120) as times,Author,UserName,pass
      from #AllNews
      where IndexId >= '+str(@PageLowerBound)+' AND IndexId <= '+str(@PageUpperBound)+'
      
      
      select @InternalTotalpass=count(*) from #AllNews where pass=1
      
      select @InternalTotalnews=count(*) from #AllNews
      
      IF object_id(''tempdb..#AllNews'') IS NOT NULL
      BEGIN
        DROP TABLE #AllNews
      END'
      
      EXEC sp_ExecuteSQL @sql,
      N'@InternalTotalpass int output, @InternalTotalnews int output', 
	  @Totalpass output ,@Totalnews output
	RETURN
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUserNewsStatistics]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUserNewsStatistics]
GO






CREATE PROCEDURE [dbo].[GetUserNewsStatistics]
(
	@PageIndex		int,
	@PageSize		int,
	@BigClassIDs	NVARCHAR(4000),
	@StartTime		datetime,
	@EndTime		datetime,
	@FullName		nvarchar(50), 
	@TotalRecords	int output,
	@TotalNews	int output,
	@TotalPassed	int output
)
AS
BEGIN

	SET @PageIndex = @PageIndex - 1
	IF (@PageIndex < 0)
		BEGIN
			SET @PageIndex = 1
		END


    DECLARE @PageLowerBound INT
    DECLARE @PageUpperBound INT    
    SET @PageLowerBound = @PageSize * @PageIndex
    SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    DECLARE @sql nvarchar(4000)
	DECLARE @where nvarchar(4000)
	DECLARE @orderby nvarchar(100)
	
    
--	SET @where = ' WHERE n.UpdateTime >= ''' + CONVERT(nvarchar, @StartTime, 120) + ''' AND ' +
--				 ' n.UpdateTime <= ''' + CONVERT(nvarchar, @EndTime, 120) + ''' ';

	SET @where = ' WHERE DATEDIFF(day, ''' + CONVERT(nvarchar, @StartTime, 120) + ''', updatetime) >= 0 and 
						 DATEDIFF(day, ''' + CONVERT(nvarchar, @endtime, 120) + ''', updatetime) <= 0 and
						 isDuplicated <> 1 ';

	IF(@BigClassIDs <> '')
		BEGIN
			SET @where = @where + ' AND n.bigclassid IN (' + @BigClassIDs + ')';
		END
	
	IF(@FullName <> '')
		BEGIN
			SET @where = @where + ' AND u.fullname LIKE ''%' + @FullName + '%''';
		END	

    SET @sql = '
	IF object_id(''tempdb..#AllNews'') IS NOT NULL
	BEGIN
		DROP TABLE #AllNews
	END
	
	IF object_id(''tempdb..#PageIndexs'') IS NOT NULL
	BEGIN
		DROP TABLE #PageIndexs
	END

	CREATE TABLE #AllNews
	(    
		NewsID	int,
		UserID	int,
		IsPassed	int
	);

	INSERT	INTO #AllNews (NewsID, IsPassed, UserID)
	SELECT	n.pkId, n.passed, u.pkId
	FROM News as n
		INNER JOIN Users as u ON u.pkId = n.UsersID
	' + 
	@where + '; 

	SELECT @InternalTotalNews = COUNT(*) FROM #AllNews;		
	SELECT @InternalTotalPassed	 = COUNT(*) FROM #AllNews WHERE IsPassed = 1;


	CREATE TABLE #PageIndexs
	(
		IndexId int IDENTITY (0, 1) NOT NULL,
		UserID	int,
		Total	int,
		Passed	int 
	);

	INSERT INTO #PageIndexs (UserID, Total, Passed)
	SELECT UserID, COUNT(*) AS Total, 0 as passed
	FROM #AllNews 	
	GROUP BY UserID
	ORDER BY Passed DESC;

	SELECT @InternalTotalRecords = COUNT(*) FROM #PageIndexs;

	UPDATE #PageIndexs
	SET Passed = 
	(
		SELECT	COUNT(*) 
		FROM	[#AllNews] as a
		WHERE	a.UserID = #PageIndexs.UserID AND
				a.IsPassed = 1
	);
	
	SELECT  u.pkId, u.fullname, p.Total, p.Passed
	FROM	[#PageIndexs] as p
			INNER JOIN [Users] AS u ON u.pkId = p.UserID
	WHERE	p.IndexId >= ' + STR(@PageLowerBound) + ' AND p.IndexId <= ' + STR(@PageUpperBound) + '  ORDER BY  p.Total  DESC , p.Passed DESC
	

	IF object_id(''tempdb..#AllNews'') IS NOT NULL
	BEGIN
		DROP TABLE #AllNews
	END
	
	IF object_id(''tempdb..#PageIndexs'') IS NOT NULL
	BEGIN
		DROP TABLE #PageIndexs
	END'

	--print @sql;
	EXEC sp_ExecuteSQL @sql, 
	N'@InternalTotalRecords int output, @InternalTotalPassed int output, @InternalTotalNews int output', 
	@TotalRecords output ,@TotalPassed	output, @TotalNews output

	RETURN
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersByName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersByName]
GO







CREATE PROCEDURE [dbo].[GetUsersByName]
@userName  varchar(100)=NULL
AS
	SET NOCOUNT ON
select top 1 u.*,d.name 'department' 
	from users u,department d
    where @userName =u.userName
      and u.departmentId=d.pkId
	order by u.pkId desc
 RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersByNameAndPassword]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersByNameAndPassword]
GO


CREATE PROCEDURE [dbo].[GetUsersByNameAndPassword]
@userName varchar(100)=NULL,
@password varchar(100)=null,
@loginIp varchar(100)=null
AS
	SET NOCOUNT ON
	BEGIN TRANSACTION
	declare @examine int
	select top 1 @examine=examine from Users where @userName=userName and @password=password
	if @examine=1
    begin
    update Users set loginIp=@loginIp,loginNum=loginNum+1,loginTime=GETDATE() where userName=@userName
    
    end
	select top 1 * from Users where @userName=userName and @password=password
   
   COMMIT TRANSACTION 
 RETURN 





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersByNameForLogin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersByNameForLogin]
GO



CREATE PROCEDURE [dbo].[GetUsersByNameForLogin]
@userName varchar(100)=NULL,
@loginIp varchar(100)=null
AS
	SET NOCOUNT ON
	BEGIN TRANSACTION
	declare @examine int
	select top 1 @examine=examine
	from Users
    where @userName=userName
    
	 if @examine=1
    begin
    update Users
    set loginIp=@loginIp,loginNum=loginNum+1,loginTime=GETDATE()
    where userName=@userName
    
    end
	select top 1 *
	from Users
    where @userName=userName   
   
   COMMIT TRANSACTION 
 RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersByPage]
GO







CREATE PROCEDURE [dbo].[GetUsersByPage]
	@searchStr varchar(500),
	@pageIndex int,
	@pageSize int,
	@pageCount int output,
	@recordCount int output
AS
	SELECT @pageIndex = @pageIndex - 1
	
	DECLARE @tempNum int
	DECLARE @vSQL nvarchar(2000)
	SET @vSQL = N'SELECT @count = Count(u.pkId) FROM users u,grade g,department d  WHERE ' + @searchStr + ' and u.gradeId=g.pkId and u.departmentId=d.pkId'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @recordCount = @tempNum

	SET @pageCount = (@recordCount + @pageSize-1)/@pageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' u.*,g.name "grade",d.name "department" FROM users u,grade g,department d WHERE ' + @searchStr + ' and u.gradeId=g.pkId and u.departmentId=d.pkId ORDER BY u.pkId DESC'	
	ELSE IF @pageIndex = @pageCount -1	
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' u.*,g.name "grade",d.name "department" FROM users u,grade g,department d WHERE ' + @searchStr + ' and u.gradeId=g.pkId and u.departmentId=d.pkId ORDER BY u.pkId ASC) TempTable ORDER BY pkId DESC'	
	ELSE IF @pageIndex > @pageCount/2	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' u.*,g.name "grade",d.name "department" FROM users u,grade g,department d WHERE ' + @searchStr + ' and u.gradeId=g.pkId and u.departmentId=d.pkId ORDER BY u.pkId ASC) TempTable ORDER BY pkId DESC'
	ELSE BEGIN	
		SET @pageIndex=@pageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' u.*,g.name "grade",d.name "department" FROM users u,grade g,department d WHERE ' + @searchStr + ' and u.gradeId=g.pkId and u.departmentId=d.pkId ORDER BY u.pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END
	EXEC(@sqlStr)
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersByUsersGroupId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersByUsersGroupId]
GO



create PROCEDURE GetUsersByUsersGroupId
(
	@usersGroupId int
)
AS

select * from users where usersGroupId=@usersGroupId




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersGroup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersGroup]
GO







CREATE PROCEDURE [dbo].[GetUsersGroup]
AS
	SET NOCOUNT ON;
SELECT * FROM UsersGroup
where pkId!=1







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersGroupByGroupName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersGroupByGroupName]
GO



create PROCEDURE GetUsersGroupByGroupName
@groupName  varchar(100)=NULL
AS

select * from usersGroup where groupName=@groupName




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersGroupByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetUsersGroupByPkId]
GO








CREATE PROCEDURE [dbo].[GetUsersGroupByPkId]
@pkId int
AS
	SET NOCOUNT ON;
SELECT * FROM UsersGroup 
where @pkId=pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearList]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetWebResearList]
	-- Add the parameters for the stored procedure here
	@bigClassPkId int,
@currentPage int=1,
@pageSize int=5,
@gradeid int=1,
@gateId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
declare @vSQL varchar(1000)
select @vSQL = 'select top ' + convert(varchar, @pageSize) + ' *,(select count(*) from question q where q.questionnaireSpecialId=questionnaireSpecial.pkId ) as QuestionCount,layoutTemplate.name from questionnaireSpecial,layoutTemplate where ItemType = layoutTemplate.pkId and questionnaireSpecial.pkId in (select top '+Convert(varchar,((@currentPage)*@pageSize))+' pkId from questionnaireSpecial where   bigClassId = '+Convert(varchar,@bigClassPkId)+' order by pkId asc)  order by questionnaireSpecial.pkId asc'
Execute (@vSQL)
    

END














GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearListByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearListByPkId]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[GetWebResearListByPkId]
	-- Add the parameters for the stored procedure here
	@PkId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select *,(select count(*)  from question where questionnaireSpecialId = questionnaireSpecial.pkId ) as QuestionCount,layoutTemplate.pkId  from questionnaireSpecial,layoutTemplate where questionnaireSpecial.pkId = @PkId and ItemType = layoutTemplate.pkId

END













GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearOptionsBybigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearOptionsBybigClassId]
GO






CREATE PROCEDURE [dbo].[GetWebResearOptionsBybigClassId]
(@bigClassId int)
AS
select * from options
where @bigClassId=options.questionId
order by options.IsTextbox asc,options.pkId asc





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearOptionsByOptionsId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearOptionsByOptionsId]
GO






CREATE PROCEDURE [dbo].[GetWebResearOptionsByOptionsId]
(@pkId int)
AS
select * from options
where @pkId=pkId





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearOptionsBypkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearOptionsBypkId]
GO






CREATE PROCEDURE [dbo].[GetWebResearOptionsBypkId]
(@pkId int)
AS
select * from options
where @pkId=options.questionId
order by options.IsTextbox asc,options.pkId asc





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearOptionsByQuestionId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearOptionsByQuestionId]
GO






CREATE PROCEDURE [dbo].[GetWebResearOptionsByQuestionId]
(@questionId int)
AS
select * from options
where options.questionId=@questionId
order by options.IsTextbox asc,options.pkId asc





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearQuestionByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearQuestionByBigClassId]
GO






CREATE PROCEDURE [dbo].[GetWebResearQuestionByBigClassId]
(@bigClassId int)
AS
SELECT q.pkId, q.questionName, q.optionsType, q.optionsCount, q.NotNull,qS.questionnaireSpecialName,q.questionnaireSpecialId FROM questionnaireSpecial qS,question q
 where @bigClassId=qS.pkId and q.questionnaireSpecialId=qS.pkId
 group by q.pkId , q.questionName , q.optionsType , q.optionsCount , q.NotNull,qS.questionnaireSpecialName,q.questionnaireSpecialId














GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearSchoolUserDetails]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearSchoolUserDetails]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetWebResearSchoolUserDetails]
	-- Add the parameters for the stored procedure here
	@UserId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select SchoolUsers.*, School.SchoolName from SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and SchoolUsers.UserId = @UserId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearSummaryFirstResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearSummaryFirstResear]
GO






CREATE PROCEDURE [dbo].[GetWebResearSummaryFirstResear]



@PageIndex int,
	@PageSize int,
	@PageCount int output,
	@RecordCount int output
AS
	SELECT @PageIndex = @PageIndex - 1
	
	DECLARE @tempNum int	
	DECLARE @vSQL nvarchar(2000)

	SET @vSQL = N'SELECT @count = Count(pkId) FROM SchoolUsers where CanVoteFirst > 0'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @RecordCount = @tempNum

	SET @PageCount = (@RecordCount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @RecordCount%@PageSize	
	IF(@size=0)
		SET @size = @PageSize	

	IF (@PageIndex = 0) OR (@PageIndex > @PageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteFirst > 0 order by SchoolUsers.pkId asc'	
	ELSE IF @PageIndex = @PageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteFirst > 0 order by SchoolUsers.pkId desc) TempTable ORDER BY pkId ASC'	
	ELSE IF @PageIndex > @PageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@RecordCount - @PageSize * @PageIndex ) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteFirst > 0 order by SchoolUsers.pkId desc) TempTable ORDER BY pkId ASC'
	ELSE BEGIN							
		SET @PageIndex=@PageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@PageSize * @PageIndex ) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteFirst > 0 order by SchoolUsers.pkId asc) TempTable1 '
					+ ' ORDER BY pkId DESC) TempTable2 ORDER BY pkId ASC'
	END
	EXEC(@sqlStr)
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearSummarySecondResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearSummarySecondResear]
GO






CREATE PROCEDURE [dbo].[GetWebResearSummarySecondResear]



@PageIndex int,
	@PageSize int,
	@PageCount int output,
	@RecordCount int output
AS
	SELECT @PageIndex = @PageIndex - 1
	
	DECLARE @tempNum int	
	DECLARE @vSQL nvarchar(2000)

	SET @vSQL = N'SELECT @count = Count(pkId) FROM SchoolUsers where CanVoteSecond > 0'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @RecordCount = @tempNum

	SET @PageCount = (@RecordCount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @RecordCount%@PageSize	
	IF(@size=0)
		SET @size = @PageSize	

	IF (@PageIndex = 0) OR (@PageIndex > @PageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteSecond > 0 order by SchoolUsers.pkId asc'	
	ELSE IF @PageIndex = @PageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteSecond > 0 order by SchoolUsers.pkId desc) TempTable ORDER BY pkId ASC'	
	ELSE IF @PageIndex > @PageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@RecordCount - @PageSize * @PageIndex ) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteSecond > 0 order by SchoolUsers.pkId desc) TempTable ORDER BY pkId ASC'
	ELSE BEGIN							
		SET @PageIndex=@PageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@PageSize * @PageIndex ) + ' SchoolUsers.*,School.SchoolName FROM SchoolUsers,School where School.pkId = SchoolUsers.SchoolId and CanVoteSecond > 0 order by SchoolUsers.pkId asc) TempTable1 '
					+ ' ORDER BY pkId DESC) TempTable2 ORDER BY pkId ASC'
	END
	EXEC(@sqlStr)
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetWebResearVoteIPByPkId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetWebResearVoteIPByPkId]
GO






CREATE PROCEDURE [dbo].[GetWebResearVoteIPByPkId]
(@bigClassPkId int, @IP bigint)
AS
select top 1 date
from IPInfo
where questionnaireSpecialId = (@bigclassPkId) and IP = (@IP)
order by date desc





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GuangXiSFW_Add]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GuangXiSFW_Add]
GO





CREATE PROCEDURE [dbo].[GuangXiSFW_Add] 
(
	@PKID		int output,
	@Title		nvarchar(50),
	@FileName	nvarchar(50),
	@FileExtension	varchar(4),
	@JoinTime	datetime,
	@UserID		int,
	@BigClassID	int,
	@Memo		ntext,
	@Users		ntext,
	@IsOver		int
)
AS
BEGIN	
	SET NOCOUNT ON;

    insert into [GuangXiSFW] 
		(	[Title],
			[FileName],
			[FileExtension],
			[JoinTime], 
			[UserID], 
			[BigClassID],
			[Memo],
			[Users],
			[IsOver]
		) 
	values 
		(	@Title,
			@FileName,
			@FileExtension,
			@JoinTime,
			@UserID,
			@BigClassID,
			@Memo,
			@Users,
			@IsOver
		)
	select scope_identity()
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GuangXiSFW_Gets]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GuangXiSFW_Gets]
GO





CREATE PROCEDURE [dbo].[GuangXiSFW_Gets]	
	@PageIndex int,
	@PageSize int,
	@Condition varchar(1000),
	@Sequence char(100),
	@SequenceHelp char(100),	
	@RecordCount int output,
	@PageCount int output
AS
	DECLARE @vSQL nvarchar(2000)
	DECLARE @_where nvarchar(1000)
	DECLARE @_order nvarchar(100)
	DECLARE @_orderHelp nvarchar(100)
	DECLARE @tempNum int	
	
	IF @Condition = ''
		SET @_where = ''
	ELSE
		SET @_where = ' WHERE ' + @Condition

	IF @Sequence = ''
		BEGIN
			SET @_order = ' ORDER BY [PKID] DESC'
			SET @_orderHelp = ' ORDER BY [PKID] ASC'
		END
	ELSE
		BEGIN
			SET	@_order = ' ORDER BY ' + @Sequence
			SET @_orderHelp = ' ORDER BY ' + @SequenceHelp
		END
	
	SET @vSQL = N'SELECT @count = Count(*) FROM [GuangXiSFW]' + @_where
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output

	SELECT @RecordCount = @tempNum	
	SELECT @PageIndex = @PageIndex - 1
	SET @PageCount = (@RecordCount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM [GuangXiSFW] ' + @_where + @_order
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM [GuangXiSFW] ' + @_where + @_orderHelp + ') TempTable ' + @_order	
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' * FROM [GuangXiSFW] ' + @_where + @_orderHelp + ') TempTable ' + @_order
	ELSE 
		BEGIN							
			SET @pageIndex=@pageIndex+1
			SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' * FROM [GuangXiSFW]' + @_where + @_order + ') TempTable1 ' + @_orderHelp + ' ) TempTable2 ' + @_order
		END
	EXEC(@sqlStr)
	RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GuangXiSFW_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GuangXiSFW_Update]
GO





CREATE PROCEDURE [dbo].[GuangXiSFW_Update] 
(
	@PKID		int,
	@Title		nvarchar(50),
	@FileName	nvarchar(50),
	@FileExtension	varchar(4),
	@JoinTime	datetime,
	@UserID		int,
	@BigClassID	int,
	@Memo		ntext,
	@Users		ntext,
	@IsOver		int
)
AS
BEGIN	
	SET NOCOUNT ON;

    update [GuangXiSFW] 
	set	[Title]			= @Title,
		[FileName]		= @FileName,
		[FileExtension] = @FileExtension,
		[JoinTime]		= @JoinTime, 
		[UserID]		= @UserID, 
		[BigClassID]	= @BigClassID,
		[Memo]			= @Memo,
		[Users]			= @Users,
		[IsOver]		= @IsOver
	where	pkId = @PKID
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InfoValidUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InfoValidUpdate]
GO






-- =============================================
-- Author:		<Yinyf>
-- Create date: <2007-11-15>
-- Description:	<信息有效期更新>
-- =============================================
CREATE PROCEDURE InfoValidUpdate 

AS
BEGIN
	SET NOCOUNT ON;
	UPDATE NEWS SET validDays = validDays - 1 WHERE validDays > 0
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertAdmin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertAdmin]
GO









CREATE PROCEDURE [dbo].[InsertAdmin]
(
	@pkId int,
	@adminName varchar(100),
	@fullName varchar(100),
	@password varchar(100),
	@allowAllAssign int,
	@allowEditOthersAdmin int,
	@isEqualAdmin int,
	@allowLogin int,
	@isSbwAdmin int
)
AS
	SET NOCOUNT OFF;
INSERT INTO Admin(adminName, fullName, password, allowAllAssign, allowEditOthersAdmin, isEqualAdmin,allowLogin, isSbwAdmin) VALUES (@adminName, @fullName, @password, @allowAllAssign, @allowEditOthersAdmin, @isEqualAdmin,@allowLogin,@isSbwAdmin)








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertAdminAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertAdminAssign]
GO







CREATE PROCEDURE [dbo].[InsertAdminAssign]
@adminId int,
@menuId int,
@pkId int
AS
	/* SET NOCOUNT ON */
	insert adminAssign (adminId,menuId) values(@adminId,@menuId)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertArtDesignTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertArtDesignTemplate]
GO



CREATE PROCEDURE InsertArtDesignTemplate
(
	@pkId int,
	@name varchar(2000),
	@leftTopFrontImageVar varchar(100),
	@leftMiddleFrontImageVar varchar(100),
	@leftMiddleBackImageVar varchar(100),
	@leftVert varchar(100),
	@leftMiddleFrontWidth varchar(100),
	@leftMiddleFrontHeight varchar(100),
	@leftMiddleFrontLink varchar(100),
	@leftBottomFrontImageVar varchar(100),
	@centerTopFrontImageVar varchar(100),
	@centerTopBackImageVar varchar(100),
	@centerTopFrontWidth varchar(100),
	@centerTopFrontHeight varchar(100),
	@centerTopFrontLink varchar(100),
	@centerMiddleBackImageVar varchar(100),
	@itemImageVar varchar(100),
	@centerBottomBackImageVar varchar(100),
	@centerBottomFrontImageVar varchar(100),
	@rightTopFrontImageVar varchar(100),
	@rightMiddleFrontImageVar varchar(100),
	@rightMiddleBackImageVar varchar(100),
	@rightVert varchar(100),
	@rightMiddleFrontWidth varchar(100),
	@rightMiddleFrontHeight varchar(100),
	@rightMiddleFrontLink varchar(100),
	@rightBottomFrontImageVar varchar(100),
	@fontPosition varchar(100),
	@color varchar(100),
	@size varchar(100),
	@space varchar(100),
	@font varchar(100),
	@bold varchar(100),
	@italic varchar(100),
	@topColor varchar(100),
	@middleColor varchar(100),
	@bottomColor varchar(100),
	@border varchar(100),
	@cellPad varchar(100),
	@cellSpace varchar(100),
	@brdrColor varchar(100),
	@centerTopBackHeight  varchar(100),
	@middleTableBorderColor varchar(100),
	@middleTableBorderShow varchar(100),
	@releaseDateVar int,
	@releaseDepartmentVar int,
	@click int,
	@newImageVar varchar(100),
	@topImageVar varchar(100),
	@showTopMoreInfo int,
	@topMoreInfoImageVar varchar(100),
	@showBottomMoreInfo int,
	@bottomMoreInfoImageVar varchar(100),
	@hotImageVar varchar(100),	
	@zdyName varchar(4000),
    @artCode varchar(100),
	@artStyle varchar(4000),
	@artState int,
	@isSystem int
)
AS
	SET NOCOUNT OFF;
INSERT INTO artDesignTemplate(middleTableBorderColor,middleTableBorderShow,centerTopBackHeight,centerBottomFrontImageVar,name, leftTopFrontImageVar, leftMiddleFrontImageVar, leftMiddleBackImageVar, leftVert, leftMiddleFrontWidth, leftMiddleFrontHeight, leftMiddleFrontLink, leftBottomFrontImageVar, centerTopFrontImageVar, centerTopBackImageVar, centerTopFrontWidth, centerTopFrontHeight, centerTopFrontLink, centerMiddleBackImageVar, itemImageVar, centerBottomBackImageVar, rightTopFrontImageVar, rightMiddleFrontImageVar, rightMiddleBackImageVar, rightVert, rightMiddleFrontWidth, rightMiddleFrontHeight, rightMiddleFrontLink, rightBottomFrontImageVar, fontPosition, color, size, [space], font, bold, italic, topColor, middleColor, bottomColor, border, cellPad, cellSpace, brdrColor,releaseDateVar,releaseDepartmentVar,click,newImageVar,topImageVar,showTopMoreInfo ,topMoreInfoImageVar,	showBottomMoreInfo,bottomMoreInfoImageVar,hotImageVar,zdyName,artCode,artStyle,artState,isSystem) VALUES (@middleTableBorderColor,@middleTableBorderShow,@centerTopBackHeight,@centerBottomFrontImageVar,@name, @leftTopFrontImageVar, @leftMiddleFrontImageVar, @leftMiddleBackImageVar, @leftVert, @leftMiddleFrontWidth, @leftMiddleFrontHeight, @leftMiddleFrontLink, @leftBottomFrontImageVar, @centerTopFrontImageVar, @centerTopBackImageVar, @centerTopFrontWidth, @centerTopFrontHeight, @centerTopFrontLink, @centerMiddleBackImageVar, @itemImageVar, @centerBottomBackImageVar, @rightTopFrontImageVar, @rightMiddleFrontImageVar, @rightMiddleBackImageVar, @rightVert, @rightMiddleFrontWidth, @rightMiddleFrontHeight, @rightMiddleFrontLink, @rightBottomFrontImageVar, @fontPosition, @color, @size, @space, @font, @bold, @italic, @topColor, @middleColor, @bottomColor, @border, @cellPad, @cellSpace, @brdrColor,@releaseDateVar,@releaseDepartmentVar,@click,@newImageVar,@topImageVar,
@showTopMoreInfo ,@topMoreInfoImageVar,	@showBottomMoreInfo,@bottomMoreInfoImageVar,@hotImageVar,@zdyName,@artCode,@artStyle,@artState,@isSystem)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertAspProperty]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertAspProperty]
GO








CREATE PROCEDURE [dbo].[InsertAspProperty]
	@url varchar(100),
	@space int,
	@usedImagesSpace float,
	@usedBackupSpace float,
	@usedSpace float,
	@buildByAsp varchar(200),
	@pkId int
AS

SET NOCOUNT OFF;

INSERT INTO [dbo].[AspProperty] (
	url,
	space,
	usedImagesSpace,
	usedBackupSpace,
	usedSpace,
	buildByAsp
) VALUES (
	@url,
	@space,
	@usedImagesSpace,
	@usedBackupSpace,
	@usedSpace,
	@buildByAsp
)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBackupInfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBackupInfo]
GO







CREATE PROCEDURE [dbo].[InsertBackupInfo]
(
	@name varchar(100),
	@zipName varchar(100),
	@updateTime datetime,
	@isSystem int,
	@pkId int,
	@remark varchar(1000),
	@type int
)
AS
	SET NOCOUNT OFF;
INSERT INTO BackupInfo(name, zipName, updateTime, isSystem,remark,type) VALUES (@name, @zipName, @updateTime, @isSystem,@remark,@type)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBanner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBanner]
GO







CREATE PROCEDURE [dbo].[InsertBanner]
(
	@name varchar(100),
	@explain varchar(100),
	@material varchar(100),
	@background varchar(100),
	@url varchar(100),
	@notInRandom int,
	@grouping int,
	@isSystem int,
	@adminId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
INSERT INTO banner(name, explain, material, background, url, notInRandom, grouping, isSystem, adminId) VALUES (@name, @explain, @material, @background, @url, @notInRandom, @grouping, @isSystem, @adminId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBigClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBigClass]
GO



CREATE PROCEDURE InsertBigClass
(
	@pkId int output,
	@name varchar(100),
	@remark varchar(100),
	@templateId int,
	@adminId int,
	@updateTime datetime,
	@isSystemItem int,
	@cssName varchar(100), -- added on 20060328 by wjh
    @morePageSize int
)
AS
	SET NOCOUNT OFF;
INSERT INTO bigClass(name, remark, templateId,adminId,updateTime,isSystemItem,cssName,morePageSize) VALUES (@name, @remark, @templateId,@adminId,@updateTime,@isSystemItem, @cssName,@morePageSize)
SET @pkId = SCOPE_IDENTITY() 




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBigClassAssociation]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBigClassAssociation]
GO





CREATE PROCEDURE [dbo].[InsertBigClassAssociation]
@pkId int=null,
@menuId	int,
@bigClassId int,
@thisBigClassId int	
AS
	SET NOCOUNT ON 
	insert BigClassAssociation(menuId, bigClassId,thisBigClassId) values(@menuId, @bigClassId,@thisBigClassId)
RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBigClassDesign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBigClassDesign]
GO



CREATE  PROCEDURE InsertBigClassDesign
(
	@pkId int,
	@menuId int,
	@cssname VarChar(100),
	@isFlag Int,
	@imageFlag VarChar(100),
	@isNew Int, 
	@imageNew VarChar(100),
	@isHot Int, 
	@imageHot VarChar(100),
	@isTop Int, 
	@imageTop VarChar(100),
	@isMore Int,
	@imageMore VarChar(100),
	@isDept Int,
	@isClick Int,
	@isTime Int
)
AS
	SET NOCOUNT OFF;
INSERT INTO bigClassDesign(menuId, cssname, isFlag,imageFlag,isNew,imageNew,isHot,imageHot,isTop,imageTop,isMore,imageMore,isDept,isClick,isTime)
 VALUES (@menuId, @cssname,@isFlag,@imageFlag,@isNew,@imageNew,@isHot,@imageHot,@isTop,@imageTop,@isMore,@imageMore,@isDept,@isClick,@isTime)






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertBoard]
GO









CREATE PROCEDURE [dbo].[InsertBoard]
(
	@pkId int output,
	@name varchar(500),
	@boardContent ntext,
	@IsTop int,
	@showDay int,
	@gate int,
	@gradeId int,
	@userId int,
	@updateIp varchar(100),
	@updateTime datetime,
	@passed int,
	@ordering int,
	@bigClassId int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId int
)
AS
	SET NOCOUNT OFF;
	 BEGIN TRANSACTION
INSERT INTO board(name,ordering,bigClassId,boardContent, IsTop, showDay, gate, gradeId, userId, updateIp, updateTime, passed, isDuplicated, duplicateFrom, duplicatedPkId) VALUES (@name,@ordering,@bigClassId,@boardContent, @IsTop, @showDay, @gate, @gradeId, @userId, @updateIp, @updateTime, @passed, @isDuplicated, @duplicateFrom, @duplicatedPkId)
SET @pkId = @@IDENTITY
  COMMIT TRANSACTION









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertContributeClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertContributeClass]
GO







CREATE PROCEDURE [dbo].[InsertContributeClass]
@pkId int=null,
@contributeClassId	int,
@bigClassId int	
AS
	SET NOCOUNT ON 
	insert contributeClass(contributeClassId, bigClassId)
	       values(@contributeClassId, @bigClassId)
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertContributePostil]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertContributePostil]
GO







--region [dbo].[InsertContributePostil]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[InsertContributePostil]
-- Date Generated: 2007骞?2鏈?7鏃?
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[InsertContributePostil]
	@title varchar(250),
	@content text,
	@isSystem int,
	@pkId int OUTPUT
AS

SET NOCOUNT ON

INSERT INTO [dbo].[ContributePostil] (
	[title],
	[content],
	[isSystem]
) VALUES (
	@title,
	@content,
	@isSystem
)

SET @pkId = SCOPE_IDENTITY()

--endregion







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertDownloadFile]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertDownloadFile]
GO





CREATE PROCEDURE InsertDownloadFile
	-- Add the parameters for the stored procedure here
	
(@downloadspecialId int,@FileSize int, @fileName varchar(1000), @filepath varchar(1000), @grade varchar(100), @subject varchar(100), @autotype varchar(100),@createtime Datetime,@updatetime datetime,@pkId int)
as
INSERT INTO resourcelist(downloadspecialId,FileSize, fileName, filepath, grade, subject,autotype,createtime,updatetime) VALUES (@downloadspecialId,@FileSize, @fileName, @filepath, @grade, @subject,@autotype,@createtime,@updatetime)
INSERT INTO resourceclick(ResourceId) 
(
select top 1 pkid 
from ResourceList 
where downloadspecialId=@downloadspecialId and filepath=@filepath and filesize=@FileSize and grade=@grade and subject=@subject and autotype=@autotype
)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertGroupAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertGroupAssign]
GO



create PROCEDURE InsertGroupAssign
@pkId int=null,
@groupId  int,	
@moduleId int,
@privilegeSet varchar(1000)
AS

insert groupAssign(groupId, menuId, privilegeSet) values(@groupId, @moduleId, @privilegeSet)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertInfoSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertInfoSearch]
GO






-- =============================================
-- Author:		<Yinyf>
-- Create date: <2007-11-29>
-- Description:	<插入具体信息查询栏目对应的表信息>
-- =============================================
CREATE PROCEDURE [dbo].[InsertInfoSearch] 
	@pkId int output,
	@MenuID int,
	@BigClassID int,
	@Condition varchar(1500),
	@WhereCondition varchar(1500),
	@FieldList varchar(2000),
	@Result varchar(2000),
	@TableName varchar(255),
	@Memo ntext,
	@NewsID int
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		INSERT INTO InfoSearch(menuId, bigClassId, condition, wherecondition, fieldList, result, tableName, memo,newsId) 
		VALUES (@MenuID, @BigClassID, @Condition, @WhereCondition, @FieldList, @Result, @TableName, @Memo,@NewsID)
	COMMIT TRANSACTION
	SET @pkId = SCOPE_IDENTITY()
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertLayoutTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertLayoutTemplate]
GO






CREATE PROCEDURE [dbo].[InsertLayoutTemplate]
(
	@name varchar(100),
	@template text,
	@simple text,
	@itemNum int,
	@itemName varchar(7000),
	@isArtDesign int,
	@pkId int output,
	@layoutTemplateType int,
	@adminId int,
	@bgpic varchar(100)
)
AS
	SET NOCOUNT OFF;
INSERT INTO layoutTemplate(name, template, simple, itemNum, itemName, isArtDesign,layoutTemplateType,adminId,bgpic) VALUES (@name, @template, @simple, @itemNum, @itemName, @isArtDesign,@layoutTemplateType,@adminId,@bgpic)
SET @pkId = @@IDENTITY





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertLibrariesTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertLibrariesTemplate]
GO








CREATE PROCEDURE [dbo].[InsertLibrariesTemplate]
(
	@name varchar(100),
	@library text,
	
	@adminId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
INSERT INTO librariesTemplate(name, library, adminId) VALUES (@name, @library,  @adminId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertLockIp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertLockIp]
GO







CREATE PROCEDURE [dbo].[InsertLockIp]
@pkId int=null,
@ip1	float,	
@ip2	float,
@sip1	nvarchar(50),
@sip2	nvarchar(50),
@ipString1 varchar(100),
@ipString2 varchar(100)
AS
	SET NOCOUNT ON 
	insert lockIp(ip1,
	              ip2,
	              sip1,
	              sip2,
	              ipString1,
	              ipString2)
	       values(@ip1,
	              @ip2,
	              @sip1,
	              @sip2,
	              @ipString1,
	              @ipString2)
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertMenu]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertMenu]
GO






CREATE PROCEDURE [dbo].[InsertMenu]
(
	@pkId int output,
	@parentId int,
	@name varchar(100),
	@remark varchar(100)=null,
	@ordering int=0,
	@address varchar(100)=null,
	@bigClassId int=0,
	@modeId int,
	@gradeId int,
	@gate int=0,
	@recorderNum int=7,
	@menuPic varchar(100),
	@itemId varchar(100),
	@layoutTemplateId int,
	@adminId int,
	@displayRoot int,
	@userIntercept int,
	@interceptNums int,
    @columnNum int,
    @artDesignTemplateId int,
	@contributeSortType int,
	@navigate int,
	@newsTemplateId int,
	@width int,
	@height int,
	@isStatic int,
	@staticUrl varchar(100),
	@staticTime datetime,
	@deslength int
)
AS
	SET NOCOUNT OFF;
BEGIN TRANSACTION
INSERT INTO menu(parentId, [name], remark, ordering, address, bigClassId, modeId, gradeId,gate,recorderNum,menuPic,itemId,layoutTemplateId,adminId,displayRoot, userIntercept,interceptNums,columnNum,artDesignTemplateId, contributeSortType,navigate,newsTemplateId,width,height,isStatic,staticUrl ,staticTime,deslength ) VALUES (@parentId, @name, @remark, @ordering, @address,@bigClassId, @modeId, @gradeId,@gate,@recorderNum,@menuPic,@itemId,@layoutTemplateId,@adminId,@displayRoot,@userIntercept,@interceptNums,@columnNum,@artDesignTemplateId, @contributeSortType,@navigate,@newsTemplateId,@width,@height,@isStatic,@staticUrl,@staticTime,@deslength)
declare @menuId int
select @menuId=@@IDENTITY
set @pkId=@menuId
if(@modeId='1')
begin
	select @address='index.aspx?pkId='+convert(varchar,@menuId)
	update menu set address=@address where pkId=@menuId

end
if(@modeId='4')
begin
     insert into artDesign([name],menuId,leftVert,rightVert,fontPosition,color,[size],[space],font,bold,italic,topColor,middleColor,bottomColor,border,cellPad,cellSpace,brdrColor,leftMiddleFrontWidth,leftMiddleFrontHeight,centerTopFrontWidth,centerTopFrontHeight,rightMiddleFrontWidth,rightMiddleFrontHeight,centerTopBackHeight,middleTableBorderColor,bottomMoreInfoImageVar) values(@name,@menuId,'middle','middle','left','#000000',3,2,'宋体',0,0,'','','',0,0,0,'',20,20,20,20,20,20,25,'','more.gif')
     select @layoutTemplateId=pkId from layoutTemplate where layoutTemplateType=1 and isSystem=1
     update menu set layoutTemplateId=@layoutTemplateId where pkId=@menuId
end
COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertMenuSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertMenuSet]
GO






CREATE PROCEDURE [dbo].[InsertMenuSet]
(
	@pkId int,
	@name varchar(100),
	@bgPic varchar(100),
	@backGroundPic varchar(100),
	@width int,
	@effect varchar(100),
	@BackgroundColor varchar(100),
	@SelectedColor varchar(100),
	@OuterHighlightColor varchar(100),
	@InnerShadowColor varchar(100),
	@InnerHighlightColor varchar(100),
	@OuterShadowColor varchar(100),
	@SelectedTextColor varchar(100),
	@fontSize varchar(100),
	@fontColor varchar(100),
	@direction varchar(100),
	@menuDirection varchar(100),
	@itemBgPic varchar(100),
	@itemIconWidth int,
	@itemIconHeight int,
	@itemIconRightWidth int,
	@PaddingLeft int,
	@DividerHeight int,
	@DividerColor varchar(100),
	@DividerImageAlignment varchar(100),
	@MenuShow int,
	@adminId int,
    @rowNum int,
    @fontWeight varchar(100),
    @subMenuFontSize varchar(100),
	@menulist text
)
AS
	SET NOCOUNT OFF;

INSERT INTO menuSet([name], bgPic, backGroundPic, width, effect, BackgroundColor, SelectedColor, OuterHighlightColor, InnerShadowColor, InnerHighlightColor, OuterShadowColor, SelectedTextColor, fontSize, fontColor, direction	,menuDirection,itemBgPic,itemIconWidth,itemIconHeight,itemIconRightWidth,PaddingLeft,DividerHeight,DividerColor,DividerImageAlignment,MenuShow,adminId,rowNum,fontWeight,subMenuFontSize,menulist
) VALUES (@name, @bgPic, @backGroundPic, @width, @effect, @BackgroundColor, @SelectedColor, @OuterHighlightColor, @InnerShadowColor, @InnerHighlightColor, @OuterShadowColor, @SelectedTextColor, @fontSize, @fontColor, @direction,@menuDirection,@itemBgPic,@itemIconWidth,@itemIconHeight,@itemIconRightWidth,@PaddingLeft,@DividerHeight,@DividerColor,@DividerImageAlignment,@MenuShow,@adminId,@rowNum,@fontWeight,@subMenuFontSize,@menulist)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertMessageBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertMessageBoard]
GO





CREATE PROCEDURE [dbo].[InsertMessageBoard]
(
	@pkId int,
	@title varchar(100),
	@checked int,
	@author varchar(100),
	@tel varchar(100),
	@isSecrecy int,
	@email varchar(100),
	@sex varchar(10),
	@homePage varchar(100),
	@Content ntext,
	@reContent ntext,
	@userId int,
	@updateTime datetime,
	@updateIp varchar(100),
	@click int,
	@isTop int,
	@MenuId int,
	@gradeId int,
	@bigClassId int,
	@gate int,
	@username varchar(100),
	@password varchar(50),
	@retime datetime,
	@redept varchar(100),
	@reAudit int
)
AS
	SET NOCOUNT OFF;	 
	BEGIN TRANSACTION;
	INSERT INTO messageBoard(
	title, 
	checked, 
	author,
	tel, 
	isSecrecy, 
	email, 
	sex, 
	homePage, 
	[Content], 
	reContent, 
	userId, 
	updateTime, 
	updateIp, 
	click, 
	isTop, 
	MenuId, 
	gradeId, 
	bigClassId, 
	gate, 
	username, 
	[password], 
	retime,
	redept,
	reAudit
	) VALUES (
	@title, 
	@checked, 
	@author,
	@tel, 
	@isSecrecy, 
	@email, 
	@sex, 
	@homePage, 
	@Content, 
	@reContent, 
	@userId, 
	@updateTime, 
	@updateIp, 
	@click, 
	@isTop, 
	@MenuId, 
	@gradeId, 
	@bigClassId, 
	@gate, 
	@username, 
	@password, 
	@retime,
	@redept,
	@reAudit
	)
  COMMIT TRANSACTION;




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertMessageForward]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertMessageForward]
GO





CREATE PROCEDURE [dbo].[InsertMessageForward]
	-- Add the parameters for the stored procedure here
	@messageId int,
	@ForwardToUserName nvarchar(50),
	@Status int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
    if (select count(*) from messageForward where messageId = @messageId and ForwardToUserName = @ForwardToUserName) = 0
	INSERT INTO messageForward(messageId,ForwardToUserName,Status) VALUES(@messageId,@ForwardToUserName,@Status)
	
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertModuleAssign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertModuleAssign]
GO








CREATE PROCEDURE [dbo].[InsertModuleAssign]
@pkId int=null,
@usersId  int,	
@moduleId int,
@privilegeSet varchar(1000)
AS
	SET NOCOUNT ON
	insert ModuleAssign(usersId,
	                   menuId,
					   privilegeSet)
	             values(@usersId,
	                   @moduleId,
	                   @privilegeSet)
	RETURN 








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertNews]
GO

CREATE PROCEDURE InsertNews
(
	@specialId int,
	@gradeId int,
	@gate int,
	@departmentId int,
	@listTitle varchar(1000),
	@intactTitle varchar(500),
	@subheading varchar(500),
	@isRecommand int,
	@isTop int,
	@useListTitle int,
	@showReviewLink int,
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@readPoint int,
	@itemType int,
	@content text,
	@descript varchar(1000),
	@usersId int,
	@editor varchar(100),
	@click int,
	@updateTime datetime,
	@passed int,
	@isReview int,
	@isImageNews int,
	@imageName varchar(100),
	@MenuId int,
	@bigClassId int,
	@width int,
	@height int,
	@pkId int output,
	@updateIp varchar(100),
	@useSubHeading int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId int,
	@validDays int,
	@AuditUserID	int,
        @Priority int,
	@isStatic	int,
        @staticUrl varchar(100),
	@staticTime datetime,
	@albumId	int
)
AS
	SET NOCOUNT OFF;

BEGIN TRY 	 
BEGIN TRANSACTION;

IF (@Priority IS NULL)
	BEGIN
	set @Priority = 0;
	END

IF (@AuditUserID IS NULL)
	BEGIN
		SET @AuditUserID = 0;
	END

IF (@validDays is null)
	set @validDays = -1	

IF (@isDuplicated is null)
	set @isDuplicated = 0

IF (@duplicatedPkId is null)
	set @duplicatedPkId = 0

INSERT INTO news(
	specialId, 
	gradeId, 
	gate, 
	departmentId, 
	listTitle, 
	intactTitle, 
	subheading, 
	isRecommand, 
	isTop, 
	useListTitle, 
	showReviewLink, 
	Author, 
	AuthorMail, 
	CopyFrom, 
	copyFromLink, 
	about, 
	pageType, 
	autoPageCharNum, 
	readPoint, 
	itemType, 
	--content, 
	descript,
	usersId,
	editor, 
	click, 
	updateTime, 
	passed, 
	isReview, 
	isImageNews, 
	imageName, 
	MenuId, 
	bigClassId, 
	width, 
	height,
	updateIp,
	useSubHeading, 
	isDuplicated, 
	duplicateFrom, 
	duplicatedPkId, 
	validDays,
	AuditUserID,
        Priority,
	isStatic,
        staticUrl,
	staticTime,
	albumId) 
VALUES (
	@specialId, 
	@gradeId, 
	@gate, 
	@departmentId, 
	@listTitle, 
	@intactTitle, 
	@subheading, 
	@isRecommand, 
	@isTop, 
	@useListTitle, 
	@showReviewLink, 
	@Author, 
	@AuthorMail, 
	@CopyFrom, 
	@copyFromLink, 
	@about, 
	@pageType, 
	@autoPageCharNum, 
	@readPoint, 
	@itemType, 
	--@content, 
	@descript, 
	@usersId, 
	@editor, 
	@click, 
	@updateTime, 
	@passed, 
	@isReview, 
	@isImageNews, 
	@imageName, 
	@MenuId, 
	@bigClassId, 
	@width, 
	@height,
	@updateIp,
	@useSubHeading, 
	@isDuplicated, 
	@duplicateFrom, 
	@duplicatedPkId, 
	@validDays,
	@AuditUserID,
        @Priority,
	@isStatic,
        @staticUrl ,
	@staticTime,
	@albumId)

SET @pkId = @@IDENTITY;

INSERT INTO newsContent (newsId,content) values (@pkId,@content); --shijian201611 for 7.0
--UPDATE news SET Priority=@pkid  WHERE pkid=@pkid
COMMIT TRANSACTION;
END TRY 
BEGIN CATCH 
	ROLLBACK TRANSACTION ;
	SET @pkId = 0;
END CATCH 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertOperationLog]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertOperationLog]
GO










--region [dbo].[InsertOperationLog]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[InsertOperationLog]
-- Date Generated: 2008骞?鏈?3鏃?
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[InsertOperationLog]
	@operation varchar(255),
	@operationUserName varchar(100),
	@operationIp varchar(100),
	@operationTime datetime,
	@remark text,
	@type int,
	@operationResult varchar(100),
	@pkId int OUTPUT
AS

SET NOCOUNT ON

INSERT INTO [dbo].[OperationLog] (
	[operation],
	[operationUserName],
	[operationIp],
	[operationTime],
	[remark],
	[type],
	[operationResult]
) VALUES (
	@operation,
	@operationUserName,
	@operationIp,
	@operationTime,
	@remark,
	@type,
	@operationResult
)

SET @pkId = SCOPE_IDENTITY()

--endregion










GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertPeople]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertPeople]
GO







CREATE PROCEDURE [dbo].[InsertPeople]
(
	@pkId int,
	@introduceType int,
	@name varchar(100),
	@sex varchar(10),
	@department varchar(100),
	@pic varchar(100),
	@introduce ntext,
	@honorType varchar(100),
	@honorGrade varchar(100),
	@ordering int,
	@userId int,
	
	@updateIp varchar(100),
	@isTop int,
	@passed int,
	@gradeId int,
	@menuId int,
	@bigClassId int,
	
	@width int,
	@height int,
	@gate int
)
AS
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
declare @updateTime datetime
select @updateTime=GETDATE ( );
INSERT INTO people(introduceType, [name], sex, department, pic, introduce, honorType, honorGrade, ordering, userId, updateTime, updateIp, isTop, passed, gradeId, menuId, bigClassId, width, height, gate) VALUES (@introduceType, @name, @sex, @department, @pic, @introduce, @honorType, @honorGrade, @ordering, @userId, @updateTime, @updateIp, @isTop, @passed, @gradeId, @menuId, @bigClassId, @width, @height, @gate)
  COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertpopWindow]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertpopWindow]
GO








CREATE PROCEDURE [dbo].[InsertpopWindow]
(
	@title varchar(100),
	@content ntext,
	@ordering int,
	@popDay int,
	@passed int,
	@popLocation varchar(100),
	@width int,
	@height int,
	@gradeId int,
	@gate int,
	@userId int,
	@updateTime datetime,
	@updateIp varchar(100),
	@isTop int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
INSERT INTO popWindow(title, content, ordering, popDay, passed, popLocation, width, height, gradeId, gate, userId, updateTime, updateIp, isTop) VALUES (@title, @content, @ordering, @popDay, @passed, @popLocation, @width, @height, @gradeId, @gate, @userId, @updateTime, @updateIp, @isTop)
  COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertPushNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertPushNews]
GO

CREATE PROCEDURE InsertPushNews
(
	@pkId int output,
	@type varchar(10),
	@newsid int,
	@auther varchar(100),
	@intactTitle varchar(100),
	@content text,
	@descript varchar(1000),
	@imageUrl varchar(100),
	@Url varchar(100),
	@isCover int,
	@pushTime datetime,
	@state varchar(200)
)
AS
	SET NOCOUNT OFF;	 
    BEGIN TRANSACTION

INSERT INTO pushNews(
	type, 
	newsid, 
	auther, 
	intactTitle, 
	content, 
	descript, 
	imageUrl, 
	Url, 
	isCover, 
	pushTime, 
	state) 
VALUES (
	@type, 
	@newsid, 
	@auther, 
	@intactTitle, 
	@content, 
	@descript, 
	@imageUrl, 
	@Url, 
	@isCover, 
	@pushTime, 
	@state)

SET @pkId = @@IDENTITY

--UPDATE pushNews SET Priority=@pkid  WHERE pkid=@pkid
COMMIT TRANSACTION


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertRegServiceList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertRegServiceList]
GO









CREATE PROCEDURE [dbo].[InsertRegServiceList]
(
	@pkId int,
	@serviceId int,
	@regServiceCode varchar(250),
	@isAllowService int,
	@regTime datetime,
	@auditTime datetime
)
AS
	SET NOCOUNT OFF;
INSERT INTO RegServiceList
(
	serviceId, 
	regServiceCode, 
	isAllowService,
	regTime,
	auditTime
) VALUES (
	@serviceId, 
	@regServiceCode, 
	@isAllowService,
	@regTime,
	@auditTime
)








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertResear]
GO








CREATE PROCEDURE [dbo].[InsertResear]
(
	@title varchar(100),
	@name varchar(100),
	@counter int,
	@bigClassId int,
	@menuId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
INSERT INTO resear(title, name, counter, bigClassId, menuId) VALUES (@title, @name, @counter, @bigClassId, @menuId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertResourceClick]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertResourceClick]
GO





CREATE PROCEDURE InsertResourceClick
	-- Add the parameters for the stored procedure here
  (@pkId int)
as begin
    update ResourceClick set clicks = clicks+1 where ResourceId = @pkId
    end  




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Insertreview]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Insertreview]
GO







CREATE PROCEDURE [dbo].[Insertreview]
@pkId int=null,
@newsId    int,
@name	varchar(100),
@content	ntext,
@title	varchar(100),
@updateTime	datetime,
@ipAddress	varchar(100),
@gradeId  int,
@passed	int ,
@userPhoto nvarchar(100)
AS
		SET NOCOUNT ON
		insert review(newsId,
name,
content,
title,
updateTime,
ipAddress,
gradeId,
passed,	 
userPhoto	               
	               )
	         values(@newsId,
@name,
@content,
@title,
@updateTime,
@ipAddress,
@gradeId,
@passed,	 
@userPhoto	               
	               )
	RETURN 








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertServiceClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertServiceClass]
GO










create PROCEDURE [dbo].[InsertServiceClass]
(
	@pkId int,
	@bigClassId int,
	@regServiceListId int
)
AS
	SET NOCOUNT OFF;
INSERT INTO ServiceClass
(
	bigClassId, 
	regServiceListId
) VALUES (
	@bigClassId, 
	@regServiceListId
)









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertSignUpBuildField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertSignUpBuildField]
GO








CREATE PROCEDURE [dbo].[InsertSignUpBuildField]
	@TableName varchar(100),
	@Name varchar(50),
	@Type varchar(50),
	@Size int,
	@ShowName varchar(500),
	@ShowType varchar(250),
	@ShowValue varchar(1024),
	@ShowComments varchar(1024),
	@AllowNull int,
	@PkId int,
	@IsSystem int,
	@IsOnly int,
	@IsAddMark int,
	@AddMarkField varchar(50)
AS

SET NOCOUNT ON

INSERT INTO [dbo].[SignUpBuildField] (
	[TableName],
	[Name],
	[Type],
	[Size],
	[ShowName],
	[ShowType],
	[ShowValue],
	[ShowComments],
	[AllowNull],
	[IsSystem],
	[IsOnly],
	[IsAddMark],
	[AddMarkField]
) VALUES (
	@TableName,
	@Name,
	@Type,
	@Size,
	@ShowName,
	@ShowType,
	@ShowValue,
	@ShowComments,
	@AllowNull,
	@IsSystem,
	@IsOnly,
	@IsAddMark,
	@AddMarkField
)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertSignUpField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertSignUpField]
GO







CREATE PROCEDURE [dbo].[InsertSignUpField]
	@Name varchar(50),
	@Type varchar(50),
	@Size int,
	@ShowName varchar(500),
	@ShowType varchar(250),
	@ShowValue varchar(1024),
	@ShowComments varchar(1024),
	@AllowNull int,
	@IsSystem int,
	@IsOnly int,
	@PkId int
AS

SET NOCOUNT ON

INSERT INTO [dbo].[SignUpField] (
	[Name],
	[Type],
	[Size],
	[ShowName],
	[ShowType],
	[ShowValue],
	[ShowComments],
	[AllowNull],
	[IsSystem],
	[IsOnly]
) VALUES (
	@Name,
	@Type,
	@Size,
	@ShowName,
	@ShowType,
	@ShowValue,
	@ShowComments,
	@AllowNull,
	@IsSystem,
	@IsOnly
)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertSignUpRelation]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertSignUpRelation]
GO







CREATE PROCEDURE [dbo].[InsertSignUpRelation]
	@BigClassId int,
	@TableName varchar(100),
	@PkId int
AS

SET NOCOUNT ON

INSERT INTO [dbo].[SignUpRelation] (
	[BigClassId],
	[TableName]
) VALUES (
	@BigClassId,
	@TableName
)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertSiteContribute]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertSiteContribute]
GO








CREATE PROCEDURE [dbo].[InsertSiteContribute]
(
	@intactTitle varchar(100),
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@content text,
	
	@updateTime datetime,
	@state int,
	
	@bigClassId int,
	@pkId int,
	@updateIp varchar(100),
	@userName varchar(100),
	@auditRemark text,
	@newsId int,
	@originalNewsId int
)
AS
	SET NOCOUNT OFF;	 BEGIN TRANSACTION
INSERT INTO siteContribute(intactTitle, Author, AuthorMail, CopyFrom, copyFromLink, about, pageType, autoPageCharNum, content, updateTime, state, bigClassId, updateIp, userName,auditRemark,newsId,originalNewsId) VALUES (@intactTitle, @Author, @AuthorMail, @CopyFrom, @copyFromLink, @about, @pageType, @autoPageCharNum, @content, @updateTime, @state, @bigClassId, @updateIp, @userName,@auditRemark,@newsId,@originalNewsId)
  COMMIT TRANSACTION






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertSiteContributeClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertSiteContributeClass]
GO








CREATE PROCEDURE [dbo].[InsertSiteContributeClass]
@pkId int=null,
@bigClassId int	
AS
	SET NOCOUNT ON 
	insert siteContributeClass( bigClassId
	            
	             
	             )
	       values( @bigClassId
	            
	             
	             )
	RETURN







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertTodayInHistory]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertTodayInHistory]
GO







CREATE PROCEDURE [dbo].[InsertTodayInHistory]
(
	@specialId int,
	@gradeId int,
	@gate int,
	@departmentId int,
	@listTitle varchar(1000),
	@intactTitle varchar(100),
	@subheading varchar(100),
	@isRecommand int,
	@isTop int,
	@useListTitle int,
	@showReviewLink int,
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@readPoint int,
	@itemType int,
	@content text,
	@descript varchar(1000),
	@usersId int,
	@editor varchar(100),
	@click int,
	@updateTime datetime,
	@passed int,
	@isReview int,
	@isImageNews int,
	@imageName varchar(100),
	@MenuId int,
	@bigClassId int,
	@width int,
	@height int,
	@pkId int output,
	@updateIp varchar(100),
	@useSubHeading int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId int,
	@happenType int,
	@happenDate datetime
)
AS
	SET NOCOUNT OFF;	 BEGIN TRANSACTION
IF (@isDuplicated is null)
	set @isDuplicated = 0
IF (@duplicatedPkId is null)
	set @duplicatedPkId = 0
INSERT INTO news(specialId, gradeId, gate, departmentId, listTitle, intactTitle, subheading, isRecommand, isTop, useListTitle, showReviewLink, Author, AuthorMail, CopyFrom, copyFromLink, about, pageType, autoPageCharNum, readPoint, itemType, content, descript, usersId, editor, click, updateTime, passed, isReview, isImageNews, imageName, MenuId, bigClassId, width, height,updateIp,useSubHeading, isDuplicated, duplicateFrom, duplicatedPkId) VALUES (@specialId, @gradeId, @gate, @departmentId, @listTitle, @intactTitle, @subheading, @isRecommand, @isTop, @useListTitle, @showReviewLink, @Author, @AuthorMail, @CopyFrom, @copyFromLink, @about, @pageType, @autoPageCharNum, @readPoint, @itemType, @content, @descript, @usersId, @editor, @click, @updateTime, @passed, @isReview, @isImageNews, @imageName, @MenuId, @bigClassId, @width, @height,@updateIp,@useSubHeading, @isDuplicated, @duplicateFrom, @duplicatedPkId)
SET @pkId = @@IDENTITY
insert into todayInHistory(newsId,happenType,happenDate) values(@pkId,@happenType,@happenDate)
  COMMIT TRANSACTION







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertUsers]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertUsers]
GO

CREATE PROCEDURE InsertUsers
(
	@userName varchar(100),
	@fullName varchar(100),
	@password varchar(100),
	@question varchar(100),
	@answer varchar(100),
	@sex varchar(10),
	@born datetime,
	@QQ varchar(100),
	@email varchar(100),
	@intro varchar(100),
	@loginIp varchar(100),
	@loginNum int,
	@loginTime datetime,
	@logIp varchar(100),
	@logTime datetime,
	@tel varchar(100),
	@photo varchar(100),
	@examine int,
	@gradeId int,
	@departmentId int,
	@company varchar(100),
	@appear int,
	@networkTest int,
	@Auditing int,
	@manageAll int,
	@usersGroupId int,
	@readInfoType int,
	@topRight int,
	@recommandRight int,
	@isSbwUser int,
	@isDel int,
	@pwdlevel int,
	@pkId int output
)
AS
SET NOCOUNT OFF;
	
	BEGIN TRANSACTION
	declare @count int
    declare @point int
    declare @days int
	
	select @gradeId=1
	select @topRight=0
	select @recommandRight=0
	select @count=Count(*) from users
	where userName=@userName
if @count!=0
 return 
	select @examine=pvalue from sitepara where ptype='RegUser' and pcode='examine'
 
INSERT INTO Users(userName, fullName, password, question, answer, sex, born, QQ, email, intro, 
loginIp, loginNum, loginTime, logIp, logTime, tel, photo, examine, gradeId, departmentId,
company, appear, networkTest, Auditing, manageAll, usersGroupId, readInfoType, topRight,
recommandRight, isSbwUser,isDel,pwdlevel) VALUES (@userName, @fullName, @password, @question, @answer, @sex, @born, @QQ,
@email, @intro, @loginIp, @loginNum, @loginTime, @logIp, @logTime, @tel, @photo, @examine, 
@gradeId, @departmentId, @company, @appear, @networkTest, @Auditing, @manageAll,
@usersGroupId, @readInfoType, @topRight, @recommandRight, @isSbwUser,@isDel,@pwdlevel)
select @pkId=@@IDENTITY

INSERT INTO moduleAssign (usersId,menuId,privilegeSet) SELECT usersId=@pkId,menuId,privilegeSet FROM moduleAssign WHERE usersId=-1

COMMIT TRANSACTION





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertUsersGroup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertUsersGroup]
GO








CREATE PROCEDURE [dbo].[InsertUsersGroup]
(
	@readInfoType int,
	@Auditing int,
	@topRight int,
	@recommandRight int,
	@allowLogin int,
	@explain varchar(100),
	@pkId int,
	@groupName varchar(100)
)
AS
	SET NOCOUNT OFF;
INSERT INTO UsersGroup(readInfoType, Auditing, topRight, recommandRight, allowLogin, explain,groupName) VALUES (@readInfoType, @Auditing, @topRight, @recommandRight, @allowLogin, @explain,@groupName)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResear]
GO






CREATE PROCEDURE [dbo].[InsertWebResear]
(@questionnaireSpecialId int, @questionName varchar(1000), @NotNull int, @optionsType int, @optionsCount int, @pkId int)
AS
	SET NOCOUNT OFF;
INSERT INTO question(questionnaireSpecialId, questionName, NotNull, optionsType, optionsCount) VALUES (@questionnaireSpecialId, @questionName, @NotNull, @optionsType, @optionsCount)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearAnswer]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearAnswer]
GO






CREATE PROCEDURE [dbo].[InsertWebResearAnswer]
(@pkId int, @questionId int, @optionsId int, @userId int, @IP bigint, @date datetime, @Message text, @userName varchar(100))
AS
insert into answer(questionId,optionsId,userId,IP,date,Message,userName)
values(@questionId,@optionsId,@userId,@IP,@date,@Message,@userName)
update options
set counter = counter +1
where options.pkId =(@optionsId)


update options
set FirstResearCounter = FirstResearCounter +1
where options.pkId =(@optionsId) and options.questionId in (select pkId from question where questionnaireSpecialId=6) and @userName in ( select UserName from users,schoolusers where users.pkId = SchoolUsers.UserId and SchoolUsers.department = 1)
update options
set SecondResearCounter = SecondResearCounter +1
where options.pkId =(@optionsId) and options.questionId in (select pkId from question where questionnaireSpecialId=6) and @userName in ( select UserName from users,schoolusers where users.pkId = SchoolUsers.UserId and SchoolUsers.department = 2)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearAnswerForSave]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearAnswerForSave]
GO





CREATE PROCEDURE [dbo].[InsertWebResearAnswerForSave]
(@pkId int, @questionId int, @optionsId int, @userId int, @IP bigint, @date datetime, @Message text, @userName varchar(100))
AS
insert into answer(questionId,optionsId,userId,IP,date,Message,userName,IsSaved)
values(@questionId,@optionsId,@userId,@IP,@date,@Message,@userName,1)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearAnswerIP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearAnswerIP]
GO






CREATE PROCEDURE [dbo].[InsertWebResearAnswerIP]
(@pkId int, @questionnaireSpecialId int, @IP bigint, @date datetime)
AS
insert into IPInfo(questionnaireSpecialId,IP,date)
values(@questionnaireSpecialId,@IP,@Date)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearByTitle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearByTitle]
GO






CREATE PROCEDURE [dbo].[InsertWebResearByTitle]
(@bigClassId int, @questionnaireSpecialName varchar(1000), @validDays int, @gate int, @gradeId int, @IsAnonymity int, @restrictObject int, @restrictMode int, @restrictTime int, @IsVerify int, @generateDate datetime, @IsShow int,@IsShowNumber int)
AS
	SET NOCOUNT OFF;
insert into questionnaireSpecial(bigClassId,questionnaireSpecialName,validDays,gate,gradeId,IsAnonymity,restrictObject,restrictMode,restrictTime,IsVerify,generateDate,IsShow,IsShowNumber) values(@bigClassId,@questionnaireSpecialName,@validDays,@gate,@gradeId,@IsAnonymity,@restrictObject,@restrictMode,@restrictTime,@IsVerify,@generateDate,@IsShow,@IsShowNumber)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearList]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[InsertWebResearList]
(
	-- Add the parameters for the stored procedure here
	@pkId int,
	@bigClassId int, 
	@questionnaireSpecialName varchar(1000), 
	@validDays int, 
	@gate int, 
	@gradeId int, 
	@IsAnonymity int, 
	@restrictObject int, 
	@restrictMode int, 
	@restrictTime int, 
	@IsVerify int, 
	@generateDate datetime, 
	@IsShow int,
	@IsShowNumber int,
	@ItemType int
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT OFF;
    -- Insert statements for procedure here
	insert into questionnaireSpecial(bigClassId,questionnaireSpecialName,validDays,gate,gradeId,IsAnonymity,restrictObject,restrictMode,restrictTime,IsVerify,generateDate,IsShow,IsShowNumber,ItemType) values(@bigClassId,@questionnaireSpecialName,@validDays,@gate,@gradeId,@IsAnonymity,@restrictObject,@restrictMode,@restrictTime,@IsVerify,@generateDate,@IsShow,@IsShowNumber,@ItemType)

END














GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertWebResearOptions]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[InsertWebResearOptions]
GO






CREATE PROCEDURE [dbo].[InsertWebResearOptions]
(@questionId int, @IsTextbox int, @optionsName varchar(1000), @counter int, @isPic int,@txtName varchar(1000), @pkId int)
AS
	SET NOCOUNT OFF;
INSERT INTO options(questionId, IsTextbox, optionsName, counter, isPic,txtName) VALUES (@questionId, @IsTextbox, @optionsName, @counter, @isPic, @txtName);
update question
set question.optionsCount = question.optionsCount +1
where question.pkId =(@questionId)




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[QueryLayoutTemplateUsedCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[QueryLayoutTemplateUsedCount]
GO







CREATE PROCEDURE [dbo].[QueryLayoutTemplateUsedCount]
(
 @LayoutTemplateId int,
 @count int output
)
AS
	/* SET NOCOUNT ON */
	set nocount on 
	
	select @count=Count(*) from menu 
	where LayoutTemplateId=@LayoutTemplateId  
	declare @num int
	select @num=Count(*) from news 
	where itemType=@LayoutTemplateId  
	declare @num1 int
	select @num1=Count(*) from siteBasicProperty 
	where shareNewsLayoutTemplateID=@LayoutTemplateId  
	
	select @count=@num+@count+@num1
	RETURN 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SensitiveWords_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SensitiveWords_Insert]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SensitiveWords_Insert]	
(
	@ID		char(32),
	@Level	int,
	@Word	nvarchar(50)
)
AS
BEGIN
	
	SET NOCOUNT ON;

	INSERT INTO SensitiveWords 
	(
		[ID], [Level], [Word]
	)

	VALUES
	(
		@ID, @Level, @Word
	)

	SELECT SCOPE_IDENTITY()

END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SensitiveWords_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SensitiveWords_Update]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SensitiveWords_Update]
(
	@pkId	int,
	@ID		char(32),
	@Level	int,
	@Word	nvarchar(50)
)
AS
BEGIN
	
	SET NOCOUNT ON;

    Update SensitiveWords 
	SET [ID] = @ID,
		[Level] = @Level,
		[Word] = @Word
	WHERE pkId = @pkId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_alterdiagram]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_alterdiagram]
GO




	CREATE PROCEDURE dbo.sp_alterdiagram
	(
		@diagramname 	sysname,
		@owner_id	int	= null,
		@version 	int,
		@definition 	varbinary(max)
	)
	WITH EXECUTE AS 'dbo'
	AS
	BEGIN
		set nocount on
	
		declare @theId 			int
		declare @retval 		int
		declare @IsDbo 			int
		
		declare @UIDFound 		int
		declare @DiagId			int
		declare @ShouldChangeUID	int
	
		if(@diagramname is null)
		begin
			RAISERROR ('Invalid ARG', 16, 1)
			return -1
		end
	
		execute as caller;
		select @theId = DATABASE_PRINCIPAL_ID();	 
		select @IsDbo = IS_MEMBER(N'db_owner'); 
		if(@owner_id is null)
			select @owner_id = @theId;
		revert;
	
		select @ShouldChangeUID = 0
		select @DiagId = diagram_id, @UIDFound = principal_id from dbo.sysdiagrams where principal_id = @owner_id and name = @diagramname 
		
		if(@DiagId IS NULL or (@IsDbo = 0 and @theId <> @UIDFound))
		begin
			RAISERROR ('Diagram does not exist or you do not have permission.', 16, 1);
			return -3
		end
	
		if(@IsDbo <> 0)
		begin
			if(@UIDFound is null or USER_NAME(@UIDFound) is null) -- invalid principal_id
			begin
				select @ShouldChangeUID = 1 ;
			end
		end

		-- update dds data			
		update dbo.sysdiagrams set definition = @definition where diagram_id = @DiagId ;

		-- change owner
		if(@ShouldChangeUID = 1)
			update dbo.sysdiagrams set principal_id = @theId where diagram_id = @DiagId ;

		-- update dds version
		if(@version is not null)
			update dbo.sysdiagrams set version = @version where diagram_id = @DiagId ;

		return 0
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_creatediagram]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_creatediagram]
GO




	CREATE PROCEDURE dbo.sp_creatediagram
	(
		@diagramname 	sysname,
		@owner_id		int	= null, 	
		@version 		int,
		@definition 	varbinary(max)
	)
	WITH EXECUTE AS 'dbo'
	AS
	BEGIN
		set nocount on
	
		declare @theId int
		declare @retval int
		declare @IsDbo	int
		declare @userName sysname
		if(@version is null or @diagramname is null)
		begin
			RAISERROR (N'E_INVALIDARG', 16, 1);
			return -1
		end
	
		execute as caller;
		select @theId = DATABASE_PRINCIPAL_ID(); 
		select @IsDbo = IS_MEMBER(N'db_owner');
		revert; 
		
		if @owner_id is null
		begin
			select @owner_id = @theId;
		end
		else
		begin
			if @theId <> @owner_id
			begin
				if @IsDbo = 0
				begin
					RAISERROR (N'E_INVALIDARG', 16, 1);
					return -1
				end
				select @theId = @owner_id
			end
		end
		-- next 2 line only for test, will be removed after define name unique
		if EXISTS(select diagram_id from dbo.sysdiagrams where principal_id = @theId and name = @diagramname)
		begin
			RAISERROR ('The name is already used.', 16, 1);
			return -2
		end
	
		insert into dbo.sysdiagrams(name, principal_id , version, definition)
				VALUES(@diagramname, @theId, @version, @definition) ;
		
		select @retval = @@IDENTITY 
		return @retval
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_dropdiagram]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_dropdiagram]
GO




	CREATE PROCEDURE dbo.sp_dropdiagram
	(
		@diagramname 	sysname,
		@owner_id	int	= null
	)
	WITH EXECUTE AS 'dbo'
	AS
	BEGIN
		set nocount on
		declare @theId 			int
		declare @IsDbo 			int
		
		declare @UIDFound 		int
		declare @DiagId			int
	
		if(@diagramname is null)
		begin
			RAISERROR ('Invalid value', 16, 1);
			return -1
		end
	
		EXECUTE AS CALLER;
		select @theId = DATABASE_PRINCIPAL_ID();
		select @IsDbo = IS_MEMBER(N'db_owner'); 
		if(@owner_id is null)
			select @owner_id = @theId;
		REVERT; 
		
		select @DiagId = diagram_id, @UIDFound = principal_id from dbo.sysdiagrams where principal_id = @owner_id and name = @diagramname 
		if(@DiagId IS NULL or (@IsDbo = 0 and @UIDFound <> @theId))
		begin
			RAISERROR ('Diagram does not exist or you do not have permission.', 16, 1)
			return -3
		end
	
		delete from dbo.sysdiagrams where diagram_id = @DiagId;
	
		return 0;
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_helpdiagramdefinition]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_helpdiagramdefinition]
GO




	CREATE PROCEDURE dbo.sp_helpdiagramdefinition
	(
		@diagramname 	sysname,
		@owner_id	int	= null 		
	)
	WITH EXECUTE AS N'dbo'
	AS
	BEGIN
		set nocount on

		declare @theId 		int
		declare @IsDbo 		int
		declare @DiagId		int
		declare @UIDFound	int
	
		if(@diagramname is null)
		begin
			RAISERROR (N'E_INVALIDARG', 16, 1);
			return -1
		end
	
		execute as caller;
		select @theId = DATABASE_PRINCIPAL_ID();
		select @IsDbo = IS_MEMBER(N'db_owner');
		if(@owner_id is null)
			select @owner_id = @theId;
		revert; 
	
		select @DiagId = diagram_id, @UIDFound = principal_id from dbo.sysdiagrams where principal_id = @owner_id and name = @diagramname;
		if(@DiagId IS NULL or (@IsDbo = 0 and @UIDFound <> @theId ))
		begin
			RAISERROR ('Diagram does not exist or you do not have permission.', 16, 1);
			return -3
		end

		select version, definition FROM dbo.sysdiagrams where diagram_id = @DiagId ; 
		return 0
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_helpdiagrams]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_helpdiagrams]
GO




	CREATE PROCEDURE dbo.sp_helpdiagrams
	(
		@diagramname sysname = NULL,
		@owner_id int = NULL
	)
	WITH EXECUTE AS N'dbo'
	AS
	BEGIN
		DECLARE @user sysname
		DECLARE @dboLogin bit
		EXECUTE AS CALLER;
			SET @user = USER_NAME();
			SET @dboLogin = CONVERT(bit,IS_MEMBER('db_owner'));
		REVERT;
		SELECT
			[Database] = DB_NAME(),
			[Name] = name,
			[ID] = diagram_id,
			[Owner] = USER_NAME(principal_id),
			[OwnerID] = principal_id
		FROM
			sysdiagrams
		WHERE
			(@dboLogin = 1 OR USER_NAME(principal_id) = @user) AND
			(@diagramname IS NULL OR name = @diagramname) AND
			(@owner_id IS NULL OR principal_id = @owner_id)
		ORDER BY
			4, 5, 1
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_renamediagram]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_renamediagram]
GO




	CREATE PROCEDURE dbo.sp_renamediagram
	(
		@diagramname 		sysname,
		@owner_id		int	= null,
		@new_diagramname	sysname
	
	)
	WITH EXECUTE AS 'dbo'
	AS
	BEGIN
		set nocount on
		declare @theId 			int
		declare @IsDbo 			int
		
		declare @UIDFound 		int
		declare @DiagId			int
		declare @DiagIdTarg		int
		declare @u_name			sysname
		if((@diagramname is null) or (@new_diagramname is null))
		begin
			RAISERROR ('Invalid value', 16, 1);
			return -1
		end
	
		EXECUTE AS CALLER;
		select @theId = DATABASE_PRINCIPAL_ID();
		select @IsDbo = IS_MEMBER(N'db_owner'); 
		if(@owner_id is null)
			select @owner_id = @theId;
		REVERT;
	
		select @u_name = USER_NAME(@owner_id)
	
		select @DiagId = diagram_id, @UIDFound = principal_id from dbo.sysdiagrams where principal_id = @owner_id and name = @diagramname 
		if(@DiagId IS NULL or (@IsDbo = 0 and @UIDFound <> @theId))
		begin
			RAISERROR ('Diagram does not exist or you do not have permission.', 16, 1)
			return -3
		end
	
		-- if((@u_name is not null) and (@new_diagramname = @diagramname))	-- nothing will change
		--	return 0;
	
		if(@u_name is null)
			select @DiagIdTarg = diagram_id from dbo.sysdiagrams where principal_id = @theId and name = @new_diagramname
		else
			select @DiagIdTarg = diagram_id from dbo.sysdiagrams where principal_id = @owner_id and name = @new_diagramname
	
		if((@DiagIdTarg is not null) and  @DiagId <> @DiagIdTarg)
		begin
			RAISERROR ('The name is already used.', 16, 1);
			return -2
		end		
	
		if(@u_name is null)
			update dbo.sysdiagrams set [name] = @new_diagramname, principal_id = @theId where diagram_id = @DiagId
		else
			update dbo.sysdiagrams set [name] = @new_diagramname where diagram_id = @DiagId
		return 0
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_upgraddiagrams]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_upgraddiagrams]
GO




	CREATE PROCEDURE dbo.sp_upgraddiagrams
	AS
	BEGIN
		IF OBJECT_ID(N'dbo.sysdiagrams') IS NOT NULL
			return 0;
	
		CREATE TABLE dbo.sysdiagrams
		(
			name sysname NOT NULL,
			principal_id int NOT NULL,	-- we may change it to varbinary(85)
			diagram_id int PRIMARY KEY IDENTITY,
			version int,
	
			definition varbinary(max)
			CONSTRAINT UK_principal_name UNIQUE
			(
				principal_id,
				name
			)
		);


		/* Add this if we need to have some form of extended properties for diagrams */
		/*
		IF OBJECT_ID(N'dbo.sysdiagram_properties') IS NULL
		BEGIN
			CREATE TABLE dbo.sysdiagram_properties
			(
				diagram_id int,
				name sysname,
				value varbinary(max) NOT NULL
			)
		END
		*/

		IF OBJECT_ID(N'dbo.dtproperties') IS NOT NULL
		begin
			insert into dbo.sysdiagrams
			(
				[name],
				[principal_id],
				[version],
				[definition]
			)
			select	 
				convert(sysname, dgnm.[uvalue]),
				DATABASE_PRINCIPAL_ID(N'dbo'),			-- will change to the sid of sa
				0,							-- zero for old format, dgdef.[version],
				dgdef.[lvalue]
			from dbo.[dtproperties] dgnm
				inner join dbo.[dtproperties] dggd on dggd.[property] = 'DtgSchemaGUID' and dggd.[objectid] = dgnm.[objectid]	
				inner join dbo.[dtproperties] dgdef on dgdef.[property] = 'DtgSchemaDATA' and dgdef.[objectid] = dgnm.[objectid]
				
			where dgnm.[property] = 'DtgSchemaNAME' and dggd.[uvalue] like N'_EA3E6268-D998-11CE-9454-00AA00A3F36E_' 
			return 2;
		end
		return 1;
	END
	


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StatNewsByStatType]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[StatNewsByStatType]
GO


CREATE PROCEDURE StatNewsByStatType
(
	@statType int,		-- 统计类型
	@lastStatTime datetime	-- 上次统计时间，处理后传过来
)
AS
	SET NOCOUNT ON
	/*
	@lastStatTime 为开始统计时间，具体到每月1号
	查询范围为：大于开始时间，小于服务器当前时间1号
	解决子站新闻发布量查询统计无效问题
	*/
	
	DECLARE @nowTime varchar(10)	-- 设置当前时间
	SET @nowTime = RTrim(Year(GetDate())) + '-' + RTrim(Month(GetDate())) + '-01'
	
	IF(@statType=1)			-- 月报
	BEGIN
		SELECT Count(*) AS newsCount, Max(updateTime) as lastUpdateTime 
		FROM News 
			WHERE (updateTime >= @lastStatTime) and (updateTime < @nowTime)
			GROUP BY Year(updateTime),Month(updateTime)
			ORDER BY lastUpdateTime DESC
	END
	ELSE IF(@statType=2)	-- 年报
	BEGIN
		DECLARE @years int, @tempTime varchar(10)
		SET @years = Year(@nowTime) - Year(@lastStatTime)
		--IF (@years > 0)
		--BEGIN
			SET	@years = Year(@lastStatTime) + @years
			SET @tempTime = Convert(varchar(4),@years) + '-01-01'
		--END
		SELECT Count(*) AS newsCount, Max(updateTime) as lastUpdateTime 
		FROM News 
		WHERE (updateTime >= @lastStatTime) and (updateTime < @tempTime) --and (year(@lastStatTime)<>RTrim(Year(GetDate()))) 
		GROUP BY Year(updateTime)
		ORDER BY lastUpdateTime DESC		
	END	
	-- 周报 @statType=0
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TasksInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[TasksInsert]
GO





CREATE PROCEDURE [dbo].[TasksInsert]	
	@PKID int output,
	@NO	varchar(20),
	@Title nvarchar(500),	
	@Department           nvarchar(100),
	@JoinTime             datetime,
	@ADUserName1          nvarchar(20),
	@ADUserName1Time      datetime,
	@UserID               int,
	@PassTime             datetime,
	@PassUserID           int,
	@LeadingPassUserID	  int,
	@LeadingPassTime	  datetime,	
	@OverTime             datetime,
	@OverUserID           int,
	@ResponsiblePersons   nvarchar(100),
	@RPSex                int,
	@RPNational           int,
	@RPBirthday           datetime,
	@RPPositions          int,
	@RPTitle              int,
	@RPExpertise          nvarchar(20),
	@RPEducation          int,
	@RPWorkDepartment     nvarchar(100),
	@RPAddress            nvarchar(100),
	@RPPostCode           char(6),
	@RPPhone              varchar(20),
	@RPEmail              varchar(100),
	@Participants         ntext,
	@FinalResultsExpected int,
	@RelatedResearchResults ntext,
	@ResearchUndertakens  ntext,
	@DesignVerificationIssues ntext,
	@CompletedTheFeasibilityAnalysis ntext,
	@MainResults          ntext,
	@FinalResearchResults ntext,
	@TaskApplyDepartmentViews ntext,
	@ADUserName2          nvarchar(20),
	@ADUserName2Time      datetime,
	@TotalGroupViews      ntext,
	@TotalGroupViewsUserName   nvarchar(20),
	@TotalGroupViewsUserNameTime datetime,
	@LeadingGroupViews    ntext,
	@LeadingGroupViewsUserName nvarchar(20),
	@LeadingGroupViewsUserNameTime datetime,
	@Status               int,
	@DocFile              nvarchar(1000),
	@BigClassID           int,
	@Integrity            int
AS
BEGIN
	
	SET NOCOUNT ON;

    INSERT INTO [Tasks]
(
	[NO],
	[Title],
	[Department],
	[JoinTime],
	[ADUserName1],
	[ADUserName1Time],
	[UserID],
	[PassTime],
	[PassUserID],
	[LeadingPassUserID],
	[LeadingPassTime],
	[OverTime],
	[OverUserID],
	[ResponsiblePersons],
	[RPSex],
	[RPNational],
	[RPBirthday],
	[RPPositions],
	[RPTitle],
	[RPExpertise],
	[RPEducation],
	[RPWorkDepartment],
	[RPAddress],
	[RPPostCode],
	[RPPhone],
	[RPEmail],
	[Participants],
	[FinalResultsExpected],
	[RelatedResearchResults],
	[ResearchUndertakens],
	[DesignVerificationIssues],
	[CompletedTheFeasibilityAnalysis],
	[MainResults],
	[FinalResearchResults],
	[TaskApplyDepartmentViews],
	[ADUserName2],
	[ADUserName2Time],
	[TotalGroupViews],
	[TotalGroupViewsUserName],
	[TotalGroupViewsUserNameTime],
	[LeadingGroupViews],
	[LeadingGroupViewsUserName],
	[LeadingGroupViewsUserNameTime],
	[Status],
	[DocFile],
	[BigClassID],
	[Integrity]

)
 VALUES (
   @NO,
   @Title,
   @Department,
   @JoinTime,
   @ADUserName1,
   @ADUserName1Time,
   @UserID,
   @PassTime,
   @PassUserID,
   @LeadingPassUserID,
   @LeadingPassTime,
   @OverTime,
   @OverUserID,
   @ResponsiblePersons,
   @RPSex,
   @RPNational,
   @RPBirthday,
   @RPPositions,
   @RPTitle,
   @RPExpertise,
   @RPEducation,
   @RPWorkDepartment,
   @RPAddress,
   @RPPostCode,
   @RPPhone,
   @RPEmail,
   @Participants,
   @FinalResultsExpected,
   @RelatedResearchResults,
   @ResearchUndertakens,
   @DesignVerificationIssues,
   @CompletedTheFeasibilityAnalysis,
   @MainResults,
   @FinalResearchResults,
   @TaskApplyDepartmentViews,
   @ADUserName2,
   @ADUserName2Time,
   @TotalGroupViews,
   @TotalGroupViewsUserName,
   @TotalGroupViewsUserNameTime,
   @LeadingGroupViews,
   @LeadingGroupViewsUserName,
   @LeadingGroupViewsUserNameTime,
   @Status,
   @DocFile,
   @BigClassID,
   @Integrity
)

SET @PKID = scope_identity()
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TasksList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[TasksList]
GO







CREATE PROCEDURE [dbo].[TasksList]	
	@PageIndex int,
	@PageSize int,
	@Condition char(200),
	@Sequence char(100),
	@SequenceHelp char(100),	
	@RecordCount int output,
	@PageCount int output
AS
	DECLARE @vSQL nvarchar(2000)
	DECLARE @_where nvarchar(200)
	DECLARE @_order nvarchar(50)
	DECLARE @_orderHelp nvarchar(50)
	DECLARE @tempNum int	
	
	IF @Condition = ''
		SET @_where = ''
	ELSE
		SET @_where = ' WHERE ' + @Condition

	IF @Sequence = ''
		BEGIN
			SET @_order = ' ORDER BY [PKID] DESC'
			SET @_orderHelp = ' ORDER BY [PKID] ASC'
		END
	ELSE
		BEGIN
			SET	@_order = ' ORDER BY ' + @Sequence
			SET @_orderHelp = ' ORDER BY ' + @SequenceHelp
		END
	
	SET @vSQL = N'SELECT @count = Count(*) FROM [Tasks]' + @_where
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output

	SELECT @RecordCount = @tempNum	
	SELECT @PageIndex = @PageIndex - 1
	SET @PageCount = (@RecordCount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @recordCount%@pageSize	
	IF(@size=0)
		SET @size = @pageSize	

	IF (@pageIndex = 0) OR (@pageIndex > @pageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM [Tasks] ' + @_where + @_order
	ELSE IF @pageIndex = @pageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM [Tasks] ' + @_where + @_orderHelp + ') TempTable ' + @_order	
	ELSE IF @pageIndex > @pageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@recordCount - @pageSize * @pageIndex ) + ' * FROM [Tasks] ' + @_where + @_orderHelp + ') TempTable ' + @_order
	ELSE 
		BEGIN							
			SET @pageIndex=@pageIndex+1
			SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@pageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@pageSize * @pageIndex ) + ' * FROM [Tasks]' + @_where + @_order + ') TempTable1 ' + @_orderHelp + ' ) TempTable2 ' + @_order
		END
	EXEC(@sqlStr)
	RETURN




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TasksUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[TasksUpdate]
GO





CREATE PROCEDURE [dbo].[TasksUpdate]	
	@PKID int,
	@NO	varchar(20),
	@Title nvarchar(500),	
	@Department           nvarchar(100),
	@JoinTime             datetime,
	@ADUserName1          nvarchar(20),
	@ADUserName1Time      datetime,
	@UserID               int,
	@PassTime             datetime,
	@PassUserID           int,
	@LeadingPassUserID	  int,
	@LeadingPassTime	  datetime,
	@OverTime             datetime,
	@OverUserID           int,
	@ResponsiblePersons   nvarchar(100),
	@RPSex                int,
	@RPNational           int,
	@RPBirthday           datetime,
	@RPPositions          int,
	@RPTitle              int,
	@RPExpertise          nvarchar(20),
	@RPEducation          int,
	@RPWorkDepartment     nvarchar(100),
	@RPAddress            nvarchar(100),
	@RPPostCode           char(6),
	@RPPhone              varchar(20),
	@RPEmail              varchar(100),
	@Participants         ntext,
	@FinalResultsExpected int,
	@RelatedResearchResults ntext,
	@ResearchUndertakens  ntext,
	@DesignVerificationIssues ntext,
	@CompletedTheFeasibilityAnalysis ntext,
	@MainResults          ntext,
	@FinalResearchResults ntext,
	@TaskApplyDepartmentViews ntext,
	@ADUserName2          nvarchar(20),
	@ADUserName2Time      datetime,
	@TotalGroupViews      ntext,
	@TotalGroupViewsUserName   nvarchar(20),
	@TotalGroupViewsUserNameTime datetime,
	@LeadingGroupViews    ntext,
	@LeadingGroupViewsUserName nvarchar(20),
	@LeadingGroupViewsUserNameTime datetime,
	@Status               int,
	@DocFile              nvarchar(1000),
	@BigClassID           int,
	@Integrity            int
AS
BEGIN
	
	SET NOCOUNT ON;

    UPDATE [Tasks] SET 

	[NO] = @NO,
	[Title] = @Title,
	[Department] = @Department,
	[JoinTime] = @JoinTime,
	[ADUserName1] = @ADUserName1,
	[ADUserName1Time] =   @ADUserName1Time,
	[UserID] =   @UserID,
	[PassTime] =    @PassTime,
	[PassUserID] =   @PassUserID,
	[LeadingPassUserID] =	@LeadingPassUserID,
	[LeadingPassTime] =	@LeadingPassTime,
	[OverTime] =   @OverTime,
	[OverUserID] =    @OverUserID,
	[ResponsiblePersons] =   @ResponsiblePersons,
	[RPSex] =   @RPSex,
	[RPNational] =   @RPNational,
	[RPBirthday] =   @RPBirthday,
	[RPPositions] =   @RPPositions,
	[RPTitle] =    @RPTitle,
	[RPExpertise] = @RPExpertise,
	[RPEducation] = @RPEducation,
	[RPWorkDepartment] =   @RPWorkDepartment,
	[RPAddress]=   @RPAddress,
	[RPPostCode] =   @RPPostCode,
	[RPPhone] =   @RPPhone,
	[RPEmail] =   @RPEmail,
	[Participants] =   @Participants,
	[FinalResultsExpected] =   @FinalResultsExpected,
	[RelatedResearchResults] =   @RelatedResearchResults,
	[ResearchUndertakens] =   @ResearchUndertakens,
	[DesignVerificationIssues] =   @DesignVerificationIssues,
	[CompletedTheFeasibilityAnalysis] =  @CompletedTheFeasibilityAnalysis,
	[MainResults] =   @MainResults,
	[FinalResearchResults] =   @FinalResearchResults,
	[TaskApplyDepartmentViews] =   @TaskApplyDepartmentViews,
	[ADUserName2] =    @ADUserName2,
	[ADUserName2Time] =   @ADUserName2Time,
	[TotalGroupViews] =   @TotalGroupViews,
	[TotalGroupViewsUserName] =  @TotalGroupViewsUserName,
	[TotalGroupViewsUserNameTime] =   @TotalGroupViewsUserNameTime,
	[LeadingGroupViews] =   @LeadingGroupViews,
	[LeadingGroupViewsUserName] =   @LeadingGroupViewsUserName,
	[LeadingGroupViewsUserNameTime] =   @LeadingGroupViewsUserNameTime,
	[Status] =   @Status,
	[DocFile] =   @DocFile,
	[BigClassID] =   @BigClassID,
	[Integrity] =   @Integrity

WHERE [PKID] = @PKID

END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateAdmin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateAdmin]
GO








CREATE PROCEDURE [dbo].[UpdateAdmin]
(
	@adminName varchar(100),
	@fullName varchar(100),
	@password varchar(100),
	@allowAllAssign int,
	@allowEditOthersAdmin int,
	@isEqualAdmin int,
	@allowLogin int,
	
	@pkId int,
	@isSbwAdmin int
)
AS
	SET NOCOUNT OFF;
UPDATE Admin SET adminName = @adminName, fullName = @fullName, password = @password, allowAllAssign = @allowAllAssign, allowEditOthersAdmin = @allowEditOthersAdmin, isEqualAdmin = @isEqualAdmin ,allowLogin=@allowLogin, isSbwAdmin=@isSbwAdmin WHERE (pkId = @pkId)








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateArtDesign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateArtDesign]
GO








CREATE PROCEDURE [dbo].[UpdateArtDesign]
(
	@name varchar(2000),
	@leftTopFrontImageVar varchar(100),
	@leftMiddleFrontImageVar varchar(100),
	@leftMiddleBackImageVar varchar(100),
	@leftVert varchar(100),
	@leftMiddleFrontWidth varchar(100),
	@leftMiddleFrontHeight varchar(100),
	@leftMiddleFrontLink varchar(100),
	@leftBottomFrontImageVar varchar(100),
	@centerTopFrontImageVar varchar(100),
	@centerTopBackImageVar varchar(100),
	@centerTopFrontWidth varchar(100),
	@centerTopFrontHeight varchar(100),
	@centerTopFrontLink varchar(100),
	@centerMiddleBackImageVar varchar(100),
	@itemImageVar varchar(100),
	@centerBottomBackImageVar varchar(100),
	@rightTopFrontImageVar varchar(100),
	@rightMiddleFrontImageVar varchar(100),
	@rightMiddleBackImageVar varchar(100),
	@rightVert varchar(100),
	@rightMiddleFrontWidth varchar(100),
	@rightMiddleFrontHeight varchar(100),
	@rightMiddleFrontLink varchar(100),
	@rightBottomFrontImageVar varchar(100),
	@fontPosition varchar(100),
	@color varchar(100),
	@size varchar(100),
	@space varchar(100),
	@font varchar(100),
	@bold varchar(100),
	@italic varchar(100),
	@topColor varchar(100),
	@middleColor varchar(100),
	@bottomColor varchar(100),
	@border varchar(100),
	@cellPad varchar(100),
	@cellSpace varchar(100),
	@brdrColor varchar(100),
	@centerBottomFrontImageVar varchar(100),
	@centerTopBackHeight  varchar(100),
	@menuId int,
	@pkId int,
	@saveMode int,
	@parentId int,
	@middleTableBorderColor varchar(100),
	@middleTableBorderShow varchar(100),
	@releaseDateVar int,
	@releaseDepartmentVar int,
	@click int,
	@newImageVar varchar(100),
	@topImageVar varchar(100),
	@showTopMoreInfo int,
	@topMoreInfoImageVar varchar(100),
	@showBottomMoreInfo int,
	@bottomMoreInfoImageVar varchar(100),
	@hotImageVar varchar(100)
)
AS
	SET  NOCOUNT OFF;
		if @saveMode=-2
	UPDATE artDesign   SET  middleTableBorderColor=@middleTableBorderColor,middleTableBorderShow=@middleTableBorderShow,centerTopBackHeight=@centerTopBackHeight, leftTopFrontImageVar = @leftTopFrontImageVar, leftMiddleFrontImageVar = @leftMiddleFrontImageVar, leftMiddleBackImageVar = @leftMiddleBackImageVar, leftVert = @leftVert, leftMiddleFrontWidth = @leftMiddleFrontWidth, leftMiddleFrontHeight = @leftMiddleFrontHeight, leftMiddleFrontLink = @leftMiddleFrontLink, leftBottomFrontImageVar = @leftBottomFrontImageVar, centerTopFrontImageVar = @centerTopFrontImageVar, centerTopBackImageVar = @centerTopBackImageVar, centerTopFrontWidth = @centerTopFrontWidth, centerTopFrontHeight = @centerTopFrontHeight, centerTopFrontLink = @centerTopFrontLink, centerMiddleBackImageVar = @centerMiddleBackImageVar, itemImageVar = @itemImageVar, centerBottomBackImageVar = @centerBottomBackImageVar, rightTopFrontImageVar = @rightTopFrontImageVar, rightMiddleFrontImageVar = @rightMiddleFrontImageVar, rightMiddleBackImageVar = @rightMiddleBackImageVar, rightVert = @rightVert, rightMiddleFrontWidth = @rightMiddleFrontWidth, rightMiddleFrontHeight = @rightMiddleFrontHeight, rightMiddleFrontLink = @rightMiddleFrontLink, rightBottomFrontImageVar = @rightBottomFrontImageVar, fontPosition = @fontPosition, color = @color, size = @size, space = @space, font = @font, bold = @bold, italic = @italic, topColor = @topColor, middleColor = @middleColor, bottomColor = @bottomColor, border = @border, cellPad = @cellPad, cellSpace = @cellSpace, brdrColor = @brdrColor, centerBottomFrontImageVar = @centerBottomFrontImageVar,releaseDateVar=@releaseDateVar,releaseDepartmentVar=@releaseDepartmentVar,click=@click,newImageVar=@newImageVar, hotImageVar = @hotImageVar,topImageVar=@topImageVar ,showTopMoreInfo=@showTopMoreInfo ,topMoreInfoImageVar=@topMoreInfoImageVar,	showBottomMoreInfo=@showBottomMoreInfo,bottomMoreInfoImageVar=@bottomMoreInfoImageVar from menu m WHERE menuId=m.pkId
		if @saveMode=-1
	UPDATE artDesign   SET  middleTableBorderColor=@middleTableBorderColor,middleTableBorderShow=@middleTableBorderShow,centerTopBackHeight=@centerTopBackHeight, leftTopFrontImageVar = @leftTopFrontImageVar, leftMiddleFrontImageVar = @leftMiddleFrontImageVar, leftMiddleBackImageVar = @leftMiddleBackImageVar, leftVert = @leftVert, leftMiddleFrontWidth = @leftMiddleFrontWidth, leftMiddleFrontHeight = @leftMiddleFrontHeight, leftMiddleFrontLink = @leftMiddleFrontLink, leftBottomFrontImageVar = @leftBottomFrontImageVar, centerTopFrontImageVar = @centerTopFrontImageVar, centerTopBackImageVar = @centerTopBackImageVar, centerTopFrontWidth = @centerTopFrontWidth, centerTopFrontHeight = @centerTopFrontHeight, centerTopFrontLink = @centerTopFrontLink, centerMiddleBackImageVar = @centerMiddleBackImageVar, itemImageVar = @itemImageVar, centerBottomBackImageVar = @centerBottomBackImageVar, rightTopFrontImageVar = @rightTopFrontImageVar, rightMiddleFrontImageVar = @rightMiddleFrontImageVar, rightMiddleBackImageVar = @rightMiddleBackImageVar, rightVert = @rightVert, rightMiddleFrontWidth = @rightMiddleFrontWidth, rightMiddleFrontHeight = @rightMiddleFrontHeight, rightMiddleFrontLink = @rightMiddleFrontLink, rightBottomFrontImageVar = @rightBottomFrontImageVar, fontPosition = @fontPosition, color = @color, size = @size, space = @space, font = @font, bold = @bold, italic = @italic, topColor = @topColor, middleColor = @middleColor, bottomColor = @bottomColor, border = @border, cellPad = @cellPad, cellSpace = @cellSpace, brdrColor = @brdrColor, centerBottomFrontImageVar = @centerBottomFrontImageVar,releaseDateVar=@releaseDateVar,releaseDepartmentVar=@releaseDepartmentVar,click=@click,newImageVar=@newImageVar, hotImageVar=@hotImageVar,topImageVar=@topImageVar ,showTopMoreInfo=@showTopMoreInfo ,topMoreInfoImageVar=@topMoreInfoImageVar,	showBottomMoreInfo=@showBottomMoreInfo,bottomMoreInfoImageVar=@bottomMoreInfoImageVar	from menu m WHERE m.parentId=@parentId  and menuId=m.pkId
	if @saveMode=0
	UPDATE artDesign   SET  middleTableBorderColor=@middleTableBorderColor,middleTableBorderShow=@middleTableBorderShow,centerTopBackHeight=@centerTopBackHeight,name = @name, leftTopFrontImageVar = @leftTopFrontImageVar, leftMiddleFrontImageVar = @leftMiddleFrontImageVar, leftMiddleBackImageVar = @leftMiddleBackImageVar, leftVert = @leftVert, leftMiddleFrontWidth = @leftMiddleFrontWidth, leftMiddleFrontHeight = @leftMiddleFrontHeight, leftMiddleFrontLink = @leftMiddleFrontLink, leftBottomFrontImageVar = @leftBottomFrontImageVar, centerTopFrontImageVar = @centerTopFrontImageVar, centerTopBackImageVar = @centerTopBackImageVar, centerTopFrontWidth = @centerTopFrontWidth, centerTopFrontHeight = @centerTopFrontHeight, centerTopFrontLink = @centerTopFrontLink, centerMiddleBackImageVar = @centerMiddleBackImageVar, itemImageVar = @itemImageVar, centerBottomBackImageVar = @centerBottomBackImageVar, rightTopFrontImageVar = @rightTopFrontImageVar, rightMiddleFrontImageVar = @rightMiddleFrontImageVar, rightMiddleBackImageVar = @rightMiddleBackImageVar, rightVert = @rightVert, rightMiddleFrontWidth = @rightMiddleFrontWidth, rightMiddleFrontHeight = @rightMiddleFrontHeight, rightMiddleFrontLink = @rightMiddleFrontLink, rightBottomFrontImageVar = @rightBottomFrontImageVar, fontPosition = @fontPosition, color = @color, size = @size, space = @space, font = @font, bold = @bold, italic = @italic, topColor = @topColor, middleColor = @middleColor, bottomColor = @bottomColor, border = @border, cellPad = @cellPad, cellSpace = @cellSpace, brdrColor = @brdrColor, centerBottomFrontImageVar = @centerBottomFrontImageVar,releaseDateVar=@releaseDateVar,releaseDepartmentVar=@releaseDepartmentVar,click=@click,newImageVar=@newImageVar,hotImageVar=@hotImageVar,topImageVar=@topImageVar ,showTopMoreInfo=@showTopMoreInfo ,topMoreInfoImageVar=@topMoreInfoImageVar,	showBottomMoreInfo=@showBottomMoreInfo,bottomMoreInfoImageVar=@bottomMoreInfoImageVar	WHERE (menuId=@menuId)
	






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateArtDesignTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateArtDesignTemplate]
GO



CREATE PROCEDURE UpdateArtDesignTemplate
(
	@name varchar(2000),
	@leftTopFrontImageVar varchar(100),
	@leftMiddleFrontImageVar varchar(100),
	@leftMiddleBackImageVar varchar(100),
	@leftVert varchar(100),
	@leftMiddleFrontWidth varchar(100),
	@leftMiddleFrontHeight varchar(100),
	@leftMiddleFrontLink varchar(100),
	@leftBottomFrontImageVar varchar(100),
	@centerTopFrontImageVar varchar(100),
	@centerTopBackImageVar varchar(100),
	@centerTopFrontWidth varchar(100),
	@centerTopFrontHeight varchar(100),
	@centerTopFrontLink varchar(100),
	@centerMiddleBackImageVar varchar(100),
	@itemImageVar varchar(100),
	@centerBottomBackImageVar varchar(100),
	@centerBottomFrontImageVar varchar(100),
	@rightTopFrontImageVar varchar(100),
	@rightMiddleFrontImageVar varchar(100),
	@rightMiddleBackImageVar varchar(100),
	@rightVert varchar(100),
	@rightMiddleFrontWidth varchar(100),
	@rightMiddleFrontHeight varchar(100),
	@rightMiddleFrontLink varchar(100),
	@rightBottomFrontImageVar varchar(100),
	@fontPosition varchar(100),
	@color varchar(100),
	@size varchar(100),
	@space varchar(100),
	@font varchar(100),
	@bold varchar(100),
	@italic varchar(100),
	@topColor varchar(100),
	@middleColor varchar(100),
	@bottomColor varchar(100),
	@border varchar(100),
	@cellPad varchar(100),
	@cellSpace varchar(100),
	@brdrColor varchar(100),
	@centerTopBackHeight   varchar(100),
	@pkId int,
	@middleTableBorderColor varchar(100),
	@middleTableBorderShow varchar(100),
    @releaseDateVar int,@releaseDepartmentVar int,
	@click int,
	@newImageVar varchar(100),
	@topImageVar varchar(100),
	@showTopMoreInfo int,
	@topMoreInfoImageVar varchar(100),
	@showBottomMoreInfo int,
	@bottomMoreInfoImageVar varchar(100),
	@hotImageVar varchar(100),
	@zdyName varchar(100),
	@artCode varchar(100),
	@artStyle varchar(4000),
	@artState int,
	@isSystem int
)
AS
	SET NOCOUNT OFF;
UPDATE artDesignTemplate SET hotImageVar=@hotImageVar, middleTableBorderColor=@middleTableBorderColor,middleTableBorderShow=@middleTableBorderShow,centerTopBackHeight=@centerTopBackHeight,centerBottomFrontImageVar=@centerBottomFrontImageVar,name = @name, leftTopFrontImageVar = @leftTopFrontImageVar, leftMiddleFrontImageVar = @leftMiddleFrontImageVar, leftMiddleBackImageVar = @leftMiddleBackImageVar, leftVert = @leftVert, leftMiddleFrontWidth = @leftMiddleFrontWidth, leftMiddleFrontHeight = @leftMiddleFrontHeight, leftMiddleFrontLink = @leftMiddleFrontLink, leftBottomFrontImageVar = @leftBottomFrontImageVar, centerTopFrontImageVar = @centerTopFrontImageVar, centerTopBackImageVar = @centerTopBackImageVar, centerTopFrontWidth = @centerTopFrontWidth, centerTopFrontHeight = @centerTopFrontHeight, centerTopFrontLink = @centerTopFrontLink, centerMiddleBackImageVar = @centerMiddleBackImageVar, itemImageVar = @itemImageVar, centerBottomBackImageVar = @centerBottomBackImageVar, rightTopFrontImageVar = @rightTopFrontImageVar, rightMiddleFrontImageVar = @rightMiddleFrontImageVar, rightMiddleBackImageVar = @rightMiddleBackImageVar, rightVert = @rightVert, rightMiddleFrontWidth = @rightMiddleFrontWidth, rightMiddleFrontHeight = @rightMiddleFrontHeight, rightMiddleFrontLink = @rightMiddleFrontLink, rightBottomFrontImageVar = @rightBottomFrontImageVar, fontPosition = @fontPosition, color = @color, size = @size, space = @space, font = @font, bold = @bold, italic = @italic, topColor = @topColor, middleColor = @middleColor, bottomColor = @bottomColor, border = @border, cellPad = @cellPad, cellSpace = @cellSpace, brdrColor = @brdrColor ,releaseDateVar=@releaseDateVar ,releaseDepartmentVar=@releaseDepartmentVar,click=@click,newImageVar=@newImageVar,topImageVar=@topImageVar ,showTopMoreInfo=@showTopMoreInfo ,topMoreInfoImageVar=@topMoreInfoImageVar,	showBottomMoreInfo=@showBottomMoreInfo,bottomMoreInfoImageVar=@bottomMoreInfoImageVar,zdyName=@zdyName,artCode=@artCode,artStyle=@artStyle,artState=@artState,isSystem=@isSystem WHERE (pkId = @pkId)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateAspProperty]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateAspProperty]
GO







CREATE PROCEDURE [dbo].[UpdateAspProperty]
	@pkId int,
	@url varchar(100),
	@space int,
	@usedImagesSpace float,
	@usedBackupSpace float,
	@usedSpace float,
	@buildByAsp varchar(200)
AS

SET NOCOUNT OFF;

UPDATE [dbo].[AspProperty] SET
	url = @url,
	space = @space,
	usedImagesSpace = @usedImagesSpace,
	usedBackupSpace = @usedBackupSpace,
	usedSpace = @usedSpace,
	buildByAsp = @buildByAsp
WHERE
	pkId = @pkId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateBackupInfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateBackupInfo]
GO







CREATE PROCEDURE [dbo].[UpdateBackupInfo]
(
	@name varchar(100),
	@zipName varchar(100),
	@updateTime datetime,
	@isSystem int,
	@pkId int,
	@remark varchar(1000),
	@type int
)
AS
	SET NOCOUNT OFF;
UPDATE BackupInfo SET name = @name, zipName = @zipName, updateTime = @updateTime, isSystem = @isSystem, remark=@remark,type=@type WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateBanner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateBanner]
GO







CREATE PROCEDURE [dbo].[UpdateBanner]
(
	@name varchar(100),
	@explain varchar(100),
	@material varchar(100),
	@background varchar(100),
	@url varchar(100),
	@notInRandom int,
	@grouping int,
	@isSystem int,
	@adminId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE banner SET name = @name, explain = @explain, material = @material, background = @background, url = @url, notInRandom = @notInRandom, grouping = @grouping, isSystem = @isSystem, adminId = @adminId WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDateBigClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDateBigClass]
GO






create PROCEDURE [dbo].[UpDateBigClass]
(
	@pkId int,
	@name varchar(100),
	@remark varchar(100),
	@templateId int,
	@adminId int,
	@updateTime datetime,
	@isSystemItem int,
	@cssName varchar(100), -- added on 20060328 by wjh
    @morePageSize int
)
AS
SET NOCOUNT OFF;
BEGIN TRANSACTION;		-- added on 201212 by shijian
	UPDATE bigClass SET name = @name, remark = @remark, templateId = @templateId ,adminId=@adminId,updateTime=@updateTime,isSystemItem=@isSystemItem, cssName = @cssName,morePageSize=@morePageSize  WHERE (pkId = @pkId);
	update menu set name = @name where  bigClassId=@pkId and bigClassId > 0 ; -- added on 201212 by shijian,同步更新menu表的栏目名称
COMMIT TRANSACTION ;	





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDateBigClassDesign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDateBigClassDesign]
GO



CREATE PROCEDURE UpDateBigClassDesign
(
	@pkId int,
	@menuId int,
	@cssname VarChar(100),
	@isFlag Int,
	@imageFlag VarChar(100),
	@isNew Int, 
	@imageNew VarChar(100),
	@isHot Int, 
	@imageHot VarChar(100),
	@isTop Int, 
	@imageTop VarChar(100),
	@isMore Int,
	@imageMore VarChar(100),
	@isDept Int,
	@isClick Int,
	@isTime Int
)
AS
SET NOCOUNT OFF;
BEGIN TRANSACTION;		-- added on 201212 by shijian
	UPDATE bigClassDesign SET cssname = @cssname, isFlag = @isFlag ,imageFlag=@imageFlag,isNew=@isNew,imageNew=@imageNew,isHot=@isHot,imageHot=@imageHot,isTop=@isTop, imageTop=@imageTop,isMore = @isMore,imageMore=@imageMore,isDept=@isDept,isClick=@isClick,isTime=@isTime  WHERE (pkId = @pkId);
COMMIT TRANSACTION ;





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateBigResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateBigResear]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE UpdateBigResear
	-- Add the parameters for the stored procedure here
	@pkId int,
	@Area nvarchar(50),
	@Address nvarchar(50),
	@Zipcode nvarchar(6),
	@Name nvarchar(50),
	@Department nvarchar(50),
	@Duty nvarchar(50),
	@Phone nvarchar(15),
	@DateY nvarchar(5),
	@DateM nvarchar(5),
	@DateD nvarchar(5),
	@Answer ntext
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update BigResear set
                        Area=@Area,
                        Address=@Address,
                        ZipCode=@Zipcode,
                        Name=@Name,
                        Department=@Department,
                        Duty=@Duty,
                        Phone=@Phone,
                        DateY=@Datey,
                        DateM=@Datem,
                        DateD=@Dated,
                        Answer=@Answer 
                        where pkId = @pkId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateBoard]
GO








CREATE PROCEDURE [dbo].[UpdateBoard]
(
	@boardContent ntext,
	@IsTop int,
	@showDay int,
	@gate int,
	@gradeId int,
	@userId int,
	@updateIp varchar(100),
	@updateTime datetime,
	@passed int,
	@pkId int,
	@name varchar(500),
	@bigClassId int,
	@ordering int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId int
)
AS
	SET NOCOUNT OFF;
UPDATE board SET bigClassId=@bigClassId,name=@name,ordering=@ordering,boardContent = @boardContent, IsTop = @IsTop, showDay = @showDay, gate = @gate, gradeId = @gradeId, userId = @userId, updateIp = @updateIp, updateTime = @updateTime, passed = @passed, isDuplicated = @isDuplicated, duplicateFrom = @duplicateFrom, duplicatedPkId = @duplicatedPkId WHERE (pkId = @pkId)
UPDATE board SET passed = @passed WHERE duplicatedPkId = @pkId AND duplicateFrom = 'board'	-- 更新board时，同时更新抄送的信息








GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateContributePostil]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateContributePostil]
GO







--region [dbo].[UpdateContributePostil]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Administrator using CodeSmith 3.2.7.955
-- Template:       StoredProcedures.cst
-- Procedure Name: [dbo].[UpdateContributePostil]
-- Date Generated: 2007骞?2鏈?7鏃?
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[UpdateContributePostil]
	@pkId int,
	@title varchar(250),
	@content text,
	@isSystem int
AS

SET NOCOUNT ON

UPDATE [dbo].[ContributePostil] SET
	[title] = @title,
	[content] = @content,
	[isSystem] = @isSystem
WHERE
	[pkId] = @pkId

--endregion







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDatediscussProperty]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDatediscussProperty]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[UpDatediscussProperty] 
	@discussId int =null,
    @mustLogin int,
    @mustCheckPass int
AS 
BEGIN
   SET NOCOUNT ON;
	if  not exists(select   1   from   discussProperty   where  discussId=@discussId)   
  begin       
  insert into discussProperty(discussId,mustLogin,mustCheckPass) values(@discussId,@mustLogin,@mustCheckPass) 
    end   
  else   
  update discussProperty set  mustLogin=@mustLogin , mustCheckPass = @mustCheckPass where discussId = @discussId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateDownloadFile]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateDownloadFile]
GO





CREATE PROCEDURE UpdateDownloadFile
	-- Add the parameters for the stored procedure here
	(@downloadspecialId int,@FileSize int, @fileName varchar(1000), @filepath varchar(1000), @grade varchar(100), @subject varchar(100), @autotype varchar(100),@createtime Datetime,@updatetime datetime,@pkId int)
as
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	UPDATE resourcelist 
SET downloadspecialId = @downloadspecialId,
Filesize = @FileSize,
fileName = @fileName,
filepath = @filepath,
grade = @grade,
subject = @subject,
autotype = @autotype,
createtime = @createtime,
updatetime = @updatetime

 WHERE pkId = @pkId
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateInfoSearch]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateInfoSearch]
GO


CREATE PROCEDURE [dbo].[UpdateInfoSearch]
	@pkId int,
	@MenuID int,
	@BigClassID int,
	@Condition varchar(1500),
	@WhereCondition varchar(1500),
	@FieldList varchar(2000),
	@Result varchar(2000),
	@TableName varchar(255),
	@Memo ntext,
	@NewsID int
AS
BEGIN
	SET NOCOUNT ON;
	UPDATE InfoSearch SET 
		menuId = @MenuID, 
		bigClassId = @BigClassID, 
		condition = @Condition, 
		wherecondition = @WhereCondition, 
		fieldList = @FieldList,
		result = @Result, 
		tableName = @TableName, 
		memo = @Memo,
		newsId = @NewsID
	WHERE pkId = @pkId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateLayoutTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateLayoutTemplate]
GO





CREATE PROCEDURE [dbo].[UpdateLayoutTemplate]
(
	@name varchar(100),
	@template text,
	@simple text,
	@itemNum int,
	@itemName varchar(7000),
	@isArtDesign int,
	@pkId int,
	@layoutTemplateType int,
	@adminId int,
	@bgpic varchar(100)
)
AS
	SET NOCOUNT OFF;
UPDATE layoutTemplate SET name = @name, template = @template, simple = @simple, itemNum = @itemNum, itemName = @itemName, isArtDesign = @isArtDesign , layoutTemplateType=@layoutTemplateType,adminId=@adminId,bgpic=@bgpic WHERE pkId = @pkId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateLibrariesTemplate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateLibrariesTemplate]
GO







CREATE PROCEDURE [dbo].[UpdateLibrariesTemplate]
(
	@name varchar(100),
	@library text,
	@adminId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE librariesTemplate SET name = @name, library = @library, adminId = @adminId WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateLockIP]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateLockIP]
GO








CREATE PROCEDURE [dbo].[UpdateLockIP]
(
	
	@ip1 float,
	@ip2 float,
	@sip1 nvarchar(50),
	@sip2 nvarchar(50),
	@ipString1 varchar(100),
	@ipString2 varchar(100),
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE LockIP SET ip1 = @ip1, ip2 = @ip2, sip1 = @sip1, sip2 = @sip2 ,ipString1=@ipString1,ipString2=@ipString2 WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateMenu]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateMenu]
GO










CREATE PROCEDURE [dbo].[UpdateMenu]
(
	@parentId int,
	@name varchar(100),
	@remark varchar(100),
	@ordering int,
	@address varchar(100),
	@bigClassId int,
	@modeId int,
	@gradeId int,
	@pkId int,
	@gate int,
	@recorderNum int,
	@menuPic varchar(100),
	@itemId  varchar(100),
	@layoutTemplateId int,
	@adminId int,
	@displayRoot int,
	@userIntercept int,
	@interceptNums int,
    @columnNum int,
    @artDesignTemplateId int,
	@contributeSortType int,
	@navigate int,
	@newsTemplateId int,
	@width int,
	@height int,
	@isStatic int,
	@staticUrl varchar(100),
	@staticTime datetime,
	@deslength int
)
AS
	SET NOCOUNT OFF;
BEGIN TRANSACTION

if(@modeId='4')
begin
UPDATE menu SET  [name] = @name WHERE (bigClassId = @bigClassId) 
update bigClass set [name]=@name where pkId=@bigClassId
end
UPDATE menu SET artDesignTemplateId=@artDesignTemplateId,columnNum=@columnNum,interceptNums=@interceptNums,userIntercept=@userIntercept, displayRoot=@displayRoot,parentId = @parentId, [name] = @name, remark = @remark, ordering = @ordering, [address] = @address, bigClassId = @bigClassId, modeId = @modeId, gradeId = @gradeId,gate=@gate ,recorderNum=@recorderNum,menuPic=@menuPic,itemId=@itemId,layoutTemplateId=@layoutTemplateId ,adminId=@adminId, contributeSortType=@contributeSortType,navigate=@navigate,newsTemplateId=@newsTemplateId,width=@width ,height=@height,isStatic=@isStatic,staticUrl=@staticUrl ,staticTime=@staticTime,deslength=@deslength WHERE (pkId = @pkId) 
COMMIT TRANSACTION




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateMenuArtDesign]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateMenuArtDesign]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE UpdateMenuArtDesign 
	-- Add the parameters for the stored procedure here
	@menuId int,
	@templateId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update menu set artDesignTemplateId = @templateId where pkId = @menuId
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateMenuSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateMenuSet]
GO





CREATE PROCEDURE [dbo].[UpdateMenuSet]
(
    @pkId int,
	@name varchar(100),
	@bgPic varchar(100),
	@backGroundPic varchar(100),
	@width int,
	@effect varchar(100),
	@BackgroundColor varchar(100),
	@SelectedColor varchar(100),
	@OuterHighlightColor varchar(100),
	@InnerShadowColor varchar(100),
	@InnerHighlightColor varchar(100),
	@OuterShadowColor varchar(100),
	@SelectedTextColor varchar(100),
	@fontSize varchar(100),
	@fontColor varchar(100),
	@direction varchar(100),
	@menuDirection varchar(100),
	@itemBgPic varchar(100),
	@itemIconWidth int,
	@itemIconHeight int,
	@itemIconRightWidth int,
	@PaddingLeft int,
	@DividerHeight int,
	@DividerColor varchar(100),
	@DividerImageAlignment varchar(100),
	@MenuShow int,
	@adminId int,
    @rowNum int,
    @fontWeight varchar(100),
	@subMenuFontSize varchar(100),
	@menulist text
	
)
AS
	SET NOCOUNT OFF;
UPDATE menuSet SET adminId=@adminId,[name] = @name, bgPic = @bgPic, backGroundPic = @backGroundPic, width = @width, effect = @effect, BackgroundColor = @BackgroundColor, SelectedColor = @SelectedColor, OuterHighlightColor = @OuterHighlightColor, InnerShadowColor = @InnerShadowColor, InnerHighlightColor = @InnerHighlightColor, OuterShadowColor = @OuterShadowColor, SelectedTextColor = @SelectedTextColor, fontSize = @fontSize, fontColor = @fontColor, direction = @direction  ,menuDirection=@menuDirection,itemBgPic=@itemBgPic,itemIconWidth=@itemIconWidth,itemIconHeight=@itemIconHeight,itemIconRightWidth=@itemIconRightWidth,	paddingLeft=@PaddingLeft,DividerHeight=@DividerHeight,DividerColor=@DividerColor,DividerImageAlignment=@DividerImageAlignment,MenuShow=@MenuShow,rowNum=@rowNum,fontWeight=@fontWeight,subMenuFontSize = @subMenuFontSize, menulist=@menulist WHERE (pkId = @pkId)










GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDateMessageBoard]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDateMessageBoard]
GO



CREATE PROCEDURE [dbo].[UpDateMessageBoard]
(
	@pkId int,
	@title varchar(100),
	@checked int,
	@author varchar(100),
	@tel varchar(100),
	@isSecrecy int,
	@email varchar(100),
	@sex varchar(10),
	@homePage varchar(100),
	@Content ntext,
	@reContent ntext,
	@userId int,
	@updateTime datetime,
	@updateIp varchar(100),
	@click int,
	@isTop int,
	@MenuId int,
	@gradeId int,
	@bigClassId int,
	@gate int,
	@username varchar(100),
	@password varchar(50),
	@retime datetime,
	@redept varchar(100),
	@reAudit int
)
AS
	SET NOCOUNT OFF;
UPDATE messageBoard SET 
	title=@title,
	checked=@checked,
	author = @author, 
	tel = @tel, 
	isSecrecy = @isSecrecy, 
	email = @email, 
	sex = @sex, 
	homePage = @homePage, 
	[Content] = @Content, 
	reContent = @reContent, 
	userId = @userId, 
	updateTime = @updateTime, 
	updateIp = @updateIp, 
	click = @click, 
	isTop = @isTop, 
	MenuId = @MenuId, 
	gradeId = @gradeId, 
	bigClassId = @bigClassId, 
	gate = @gate, 
	username = @username, 
	[password] = @password, 
	retime = @retime,
	redept=@redept,
	reAudit=@reAudit

WHERE (pkId = @pkId)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateNews]
GO

CREATE PROCEDURE UpdateNews
(
	@specialId int,
	@gradeId int,
	@gate int,
	@departmentId int,
	@listTitle varchar(1000),
	@intactTitle varchar(500),
	@subheading varchar(500),
	@isRecommand int,
	@isTop int,
	@useListTitle int,
	@showReviewLink int,
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@readPoint int,
	@itemType int,
	@content text,
	@descript varchar(1000),
	@usersId int,
	@editor varchar(100),
	@click int,
	@updateTime datetime,
	@passed int,
	@isReview int,
	@isImageNews int,
	@imageName varchar(100),
	@MenuId int,
	@bigClassId int,
	@width int,
	@height int,
	@pkId int,
	@updateIp  varchar(100),
	@useSubHeading int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId	int,
	@validDays int,
	@AuditUserID int,
        @Priority int,
	@isStatic	int,
        @staticUrl varchar(100),
	@staticTime datetime,
	@albumId	int
)
AS
	SET NOCOUNT OFF;
IF	(@AuditUserID IS NULL)
	BEGIN
		SET @AuditUserID = 0;
	END

IF (@validDays is null)
	set @validDays = -1	;

BEGIN TRY 	 
BEGIN TRANSACTION;
	UPDATE news SET useSubHeading=@useSubHeading,updateIp=@updateIp,specialId = @specialId, gradeId = @gradeId, gate = @gate, departmentId = @departmentId, listTitle = @listTitle, intactTitle = @intactTitle, subheading = @subheading, isRecommand = @isRecommand, isTop = @isTop, useListTitle = @useListTitle, showReviewLink = @showReviewLink, Author = @Author, AuthorMail = @AuthorMail, CopyFrom = @CopyFrom, copyFromLink = @copyFromLink, about = @about, pageType = @pageType, autoPageCharNum = @autoPageCharNum, readPoint = @readPoint, itemType = @itemType,  descript = @descript, usersId = @usersId, editor = @editor, click = @click, updateTime = @updateTime, passed = @passed, isReview = @isReview, isImageNews = @isImageNews, imageName = @imageName, MenuId = @MenuId, bigClassId = @bigClassId, width = @width, height = @height, isDuplicated = @isDuplicated, duplicateFrom = @duplicateFrom, duplicatedPkId = @duplicatedPkId, validDays = @validDays, AuditUserID = @AuditUserID ,Priority=@Priority,isStatic=@isStatic, staticUrl=@staticUrl ,staticTime=@staticTime, albumId=@albumId WHERE pkId = @pkId;
	UPDATE newsContent set content = @content WHERE newsId = @pkId;
	--UPDATE news SET passed = @passed WHERE duplicatedPkId=@pkId AND duplicateFrom='news'			-- 更新news时，同时更新news抄送的信息
	--UPDATE board SET passed = @passed WHERE duplicatedPkId=@pkId AND duplicateFrom = 'news'		-- 更新news时，同时更新board抄送的信息
COMMIT TRANSACTION;
END TRY 
BEGIN CATCH 
	ROLLBACK TRANSACTION ;
END CATCH


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateNewsNewTemplateByBigClassId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateNewsNewTemplateByBigClassId]
GO





CREATE PROCEDURE [dbo].[UpdateNewsNewTemplateByBigClassId] 
	@bigClassId int,
	@newTemplateId int
AS
	SET NOCOUNT ON;

update news set itemType=@newTemplateId where bigClassId=@bigClassId





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDateNewsStatic]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDateNewsStatic]
GO

CREATE PROCEDURE UpDateNewsStatic
(
	@pkId int,
	@isStatic int,
	@staticUrl varchar(100),
	@staticTime datetime
)
AS
	SET NOCOUNT OFF;

UPDATE news SET isStatic = @isStatic, staticUrl = @staticUrl, staticTime = @staticTime WHERE pkId = @pkId


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdatePeople]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdatePeople]
GO







CREATE PROCEDURE [dbo].[UpdatePeople]
(
	@introduceType int,
	@name varchar(100),
	@sex varchar(10),
	@department varchar(100),
	@pic varchar(100),
	@introduce ntext,
	@honorType varchar(100),
	@honorGrade varchar(100),
	@ordering int,
	@userId int,
	
	@updateIp varchar(100),
	@isTop int,
	@passed int,
	@gradeId int,
	@menuId int,
	@bigClassId int,
	
	@width int,
	@height int,
	@gate int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
	declare @updateTime datetime
select @updateTime=GETDATE ( );
UPDATE people SET introduceType = @introduceType, name = @name, sex = @sex, department = @department, pic = @pic, introduce = @introduce, honorType = @honorType, honorGrade = @honorGrade, ordering = @ordering, userId = @userId, updateTime = @updateTime, updateIp = @updateIp, isTop = @isTop, passed = @passed, gradeId = @gradeId, menuId = @menuId, bigClassId = @bigClassId, width = @width, height = @height, gate = @gate WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdatePopWindow]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdatePopWindow]
GO







CREATE PROCEDURE [dbo].[UpdatePopWindow]
(
	@title varchar(100),
	@content ntext,
	@ordering int,
	@popDay int,
	@passed int,
	@popLocation varchar(100),
	@width int,
	@height int,
	@gradeId int,
	@gate int,
	@userId int,
	@updateTime datetime,
	@updateIp varchar(100),
	@isTop int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE popWindow SET title = @title, content = @content, ordering = @ordering, popDay = @popDay, passed = @passed, popLocation = @popLocation, width = @width, height = @height, gradeId = @gradeId, gate = @gate, userId = @userId, updateTime = @updateTime, updateIp = @updateIp, isTop = @isTop WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpDatePushNews]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpDatePushNews]
GO

CREATE PROCEDURE UpDatePushNews
(
	@pkId int output,
	@type varchar(10),
	@newsid int,
	@auther varchar(100),
	@intactTitle varchar(100),
	@content text,
	@descript varchar(1000),
	@imageUrl varchar(100),
	@Url varchar(100),
	@isCover int,
	@pushTime datetime,
	@state varchar(200)
)
AS
	SET NOCOUNT OFF;

UPDATE pushNews SET type=@type,newsid=@newsid,auther = @auther, intactTitle = @intactTitle, content = @content, descript = @descript, imageUrl = @imageUrl, Url = @Url, isCover = @isCover, pushTime = @pushTime, state = @state   WHERE (newsid = @newsid and type=@type)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateRegServiceList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateRegServiceList]
GO





CREATE PROCEDURE [dbo].[UpdateRegServiceList]
(
	@pkId int,
	@serviceId int,
	@regServiceCode varchar(250),
	@isAllowService int,
	@regTime datetime,
	@auditTime datetime	
)
AS
	SET NOCOUNT OFF;
Update RegServiceList set
	serviceId = @serviceId, 
	regServiceCode = @regServiceCode, 
	isAllowService = @isAllowService,
	regTime = @regTime,
	auditTime = @regTime where pkid=@pkid





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateResear]
GO







CREATE PROCEDURE [dbo].[UpdateResear]
(
	@title varchar(100),
	@name varchar(100),
	@counter int,
	@bigClassId int,
	@menuId int,
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE resear SET title = @title, name = @name, counter = @counter, bigClassId = @bigClassId, menuId = @menuId WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateResearByTitle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateResearByTitle]
GO







CREATE PROCEDURE [dbo].[UpdateResearByTitle]
(
	@title varchar(100),
	@bigClassId int
)
AS
	SET NOCOUNT OFF;
UPDATE resear SET title = @title
 WHERE (bigClassId = @bigClassId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateReview]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateReview]
GO







CREATE PROCEDURE [dbo].[UpdateReview]
(
	@newsId int,
	@name varchar(100),
	@content ntext,
	@title varchar(100),
	@updateTime datetime,
	@ipAddress varchar(100),
	@gradeId int,
	@passed int,
	@userPhoto nvarchar(100),
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE review SET newsId = @newsId, name = @name, content = @content, title = @title, updateTime = @updateTime, ipAddress = @ipAddress, gradeId = @gradeId, passed = @passed, userPhoto=@userPhoto WHERE (pkId = @pkId)







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSearchOptions]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSearchOptions]
GO






-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateSearchOptions]
	-- Add the parameters for the stored procedure here
	(@bigClassId int,
     @OptionsName varchar(5000), 
	@OptionsValue varchar(5000))
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	if  not exists(select   1   from   searchOptions   where   bigClassId=@bigClassId)   
  begin       
  insert into searchOptions(bigClassId,OptionsName,OptionsValue) values(@bigClassId,@OptionsName,@OptionsValue) 
    end   
  else   
  update searchOptions set OptionsName = @OptionsName , OptionsValue=@OptionsValue where bigClassId = @bigClassId
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateServiceClass]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateServiceClass]
GO









create PROCEDURE [dbo].[UpdateServiceClass]
(
	@pkId int,
	@bigClassId int,
	@regServiceListId int
)
AS
	SET NOCOUNT OFF;
Update ServiceClass
set
	bigClassId = @bigClassId, 
	regServiceListId = @regServiceListId









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSignUpBuildField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSignUpBuildField]
GO







CREATE PROCEDURE [dbo].[UpdateSignUpBuildField]
	@PkId int,
	@TableName varchar(100),
	@Name varchar(50),
	@Type varchar(50),
	@Size int,
	@ShowName varchar(500),
	@ShowType varchar(250),
	@ShowValue varchar(1024),
	@ShowComments varchar(1024),
	@AllowNull int,
	@IsSystem int,
	@IsOnly int,
	@IsAddMark int,
	@AddMarkField varchar(50)
AS

SET NOCOUNT ON

UPDATE [dbo].[SignUpBuildField] SET
	[TableName] = @TableName,
	[Name] = @Name,
	[Type] = @Type,
	[Size] = @Size,
	[ShowName] = @ShowName,
	[ShowType] = @ShowType,
	[ShowValue] = @ShowValue,
	[ShowComments] = @ShowComments,
	[AllowNull] = @AllowNull,
	[IsSystem] = @IsSystem,
	[IsOnly] = @IsOnly,
	[IsAddMark] = @IsAddMark,
	[AddMarkField] = @AddMarkField
WHERE
	[PkId] = @PkId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSignUpField]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSignUpField]
GO







CREATE PROCEDURE [dbo].[UpdateSignUpField]
	@PkId int,
	@Name varchar(50),
	@Type varchar(50),
	@Size int,
	@ShowName varchar(500),
	@ShowType varchar(250),
	@ShowValue varchar(1024),
	@ShowComments varchar(1024),
	@AllowNull int,
	@IsSystem int,
	@IsOnly int
AS

SET NOCOUNT ON

UPDATE [dbo].[SignUpField] SET
	[Name] = @Name,
	[Type] = @Type,
	[Size] = @Size,
	[ShowName] = @ShowName,
	[ShowType] = @ShowType,
	[ShowValue] = @ShowValue,
	[ShowComments] = @ShowComments,
	[AllowNull] = @AllowNull,
	[IsSystem] = @IsSystem,
	[IsOnly] = @IsOnly
WHERE
	[PkId] = @PkId






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSignUpRelation]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSignUpRelation]
GO







CREATE PROCEDURE [dbo].[UpdateSignUpRelation]
	@PkId int,
	@BigClassId int,
	@TableName varchar(100)
AS

SET NOCOUNT ON

UPDATE [dbo].[SignUpRelation] SET
	[BigClassId] = @BigClassId,
	[TableName] = @TableName
WHERE
	[PkId] = @PkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSiteContribute]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSiteContribute]
GO







CREATE PROCEDURE [dbo].[UpdateSiteContribute]
(
	@intactTitle varchar(100),
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@content text,
	
	@updateTime datetime,
	@state int,
	
	@bigClassId int,
	@pkId int,
	@updateIp  varchar(100),
	@userName varchar(100),
	@auditRemark text,
	@newsId int,
	@originalNewsId int
)
AS
	SET NOCOUNT OFF;
UPDATE siteContribute SET intactTitle = @intactTitle, 
					  Author = @Author, 
					  AuthorMail = @AuthorMail, 
					  CopyFrom = @CopyFrom, 
					  copyFromLink = @copyFromLink, 
					  about = @about, 
					  pageType = @pageType, 
					  autoPageCharNum = @autoPageCharNum, 
					  content = @content, 
					 
					  updateTime = @updateTime, 
					  state = @state, 
					
					  bigClassId = @bigClassId, 
					  userName = @userName,
					  auditRemark = @auditRemark,
					  newsId = @newsId,
					  updateIp=@updateIp,
					  originalNewsId=@originalNewsId
					  WHERE (pkId = @pkId)






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateSitePara]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateSitePara]
GO



CREATE PROCEDURE UpdateSitePara
@ptype varchar(20),
@pcode varchar(20),
@pvalue varchar(100),
@pseq int,
@pnote varchar(100)

AS
    --先获取记录总数
	DECLARE @tempNum int
	SET @tempNum = (select count(*) from sitePara where pcode=@pcode)
	if(@tempNum=0)
	begin
		insert into sitePara(ptype,pcode,pvalue,pseq,pnote)values(@ptype,@pcode,@pvalue,@pseq,@pnote)
	end
	else
	begin
		update sitePara set ptype = @ptype,pvalue=@pvalue,pseq=@pseq,pnote=@pnote WHERE pcode=@pcode 
	end



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateStat]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateStat]
GO

CREATE PROCEDURE UpdateStat
AS
	SET NOCOUNT OFF;
declare @num int
select @num=count(*) from stat where pvdate=convert(varchar(10),getdate(),120)
if(@num=0)
insert into stat ([time],[pvdate])values(1,convert(varchar(10),getdate(),120))
UPDATE stat SET [time] = [time]+1 where pvdate=convert(varchar(10),getdate(),120)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateTodayInHistory]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateTodayInHistory]
GO







CREATE PROCEDURE [dbo].[UpdateTodayInHistory]
(
	@specialId int,
	@gradeId int,
	@gate int,
	@departmentId int,
	@listTitle varchar(1000),
	@intactTitle varchar(100),
	@subheading varchar(100),
	@isRecommand int,
	@isTop int,
	@useListTitle int,
	@showReviewLink int,
	@Author varchar(100),
	@AuthorMail varchar(100),
	@CopyFrom varchar(100),
	@copyFromLink varchar(100),
	@about varchar(100),
	@pageType int,
	@autoPageCharNum int,
	@readPoint int,
	@itemType int,
	@content text,
	@descript varchar(1000),
	@usersId int,
	@editor varchar(100),
	@click int,
	@updateTime datetime,
	@passed int,
	@isReview int,
	@isImageNews int,
	@imageName varchar(100),
	@MenuId int,
	@bigClassId int,
	@width int,
	@height int,
	@pkId int,
	@updateIp  varchar(100),
	@useSubHeading int,
	@isDuplicated int=0,
	@duplicateFrom varchar(100),
	@duplicatedPkId	int,
	@happenType int,
	@happenDate datetime
)
AS
	SET NOCOUNT OFF;
UPDATE news SET useSubHeading=@useSubHeading,updateIp=@updateIp,specialId = @specialId, gradeId = @gradeId, gate = @gate, departmentId = @departmentId, listTitle = @listTitle, intactTitle = @intactTitle, subheading = @subheading, isRecommand = @isRecommand, isTop = @isTop, useListTitle = @useListTitle, showReviewLink = @showReviewLink, Author = @Author, AuthorMail = @AuthorMail, CopyFrom = @CopyFrom, copyFromLink = @copyFromLink, about = @about, pageType = @pageType, autoPageCharNum = @autoPageCharNum, readPoint = @readPoint, itemType = @itemType, content = @content, descript = @descript, usersId = @usersId, editor = @editor, click = @click, updateTime = @updateTime, passed = @passed, isReview = @isReview, isImageNews = @isImageNews, imageName = @imageName, MenuId = @MenuId, bigClassId = @bigClassId, width = @width, height = @height, isDuplicated = @isDuplicated, duplicateFrom = @duplicateFrom, duplicatedPkId = @duplicatedPkId WHERE (pkId = @pkId)
UPDATE news SET passed = @passed WHERE duplicatedPkId=@pkId AND duplicateFrom='news'			-- 更新news时，同时更新news抄送的信息
UPDATE board SET passed = @passed WHERE duplicatedPkId=@pkId AND duplicateFrom = 'news'		-- 更新news时，同时更新board抄送的信息
update todayInHistory set happenType = @happenType,happenDate = @happenDate where newsId = @pkId







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateUnitInfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateUnitInfo]
GO







CREATE PROCEDURE [dbo].[UpdateUnitInfo]
(
	@siteName varchar(100),
	@siteUrl varchar(100),
	@unitName varchar(100),
	@unitAddress varchar(100),
	@Phone varchar(100),
	@mailName varchar(100),
	@mail varchar(100),
	@copyright varchar(100),
	@version varchar(100),
	@versionId varchar(100),
	@pkId int
)
AS
	SET NOCOUNT OFF;
UPDATE unitInfo SET siteName = @siteName, siteUrl = @siteUrl, unitName = @unitName, unitAddress = @unitAddress, Phone = @Phone, mailName = @mailName, mail = @mail, copyright = @copyright, version = @version, versionId = @versionId 







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateUsers]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateUsers]
GO

CREATE PROCEDURE UpdateUsers
(
	@userName varchar(100),
	@fullName varchar(100),
	@password varchar(100),
	@question varchar(100),
	@answer varchar(100),
	@sex varchar(10),
	@born datetime,
	@QQ varchar(100),
	@email varchar(100),
	@intro varchar(100),
	@loginIp varchar(100),
	@loginNum int,
	@loginTime datetime,
	@logIp varchar(100),
	@logTime datetime,
	@tel varchar(100),
	@photo varchar(100),
	@examine int,
	@gradeId int,
	@departmentId int,
	@company varchar(100),
	@appear int,
	@networkTest int,
	@Auditing int,
	@manageAll int,
	@usersGroupId int,
	@readInfoType int,
	@topRight int,
	@recommandRight int,
	@pkId int,
	@isSbwUser int,
	@isDel int,
	@pwdlevel int
)
AS
	SET NOCOUNT OFF;
UPDATE Users SET isDel=@isDel, userName = @userName, fullName = @fullName, password = @password, question = @question, answer = @answer, sex = @sex, born = @born, QQ = @QQ, email = @email, intro = @intro, loginIp = @loginIp, loginNum = @loginNum, loginTime = @loginTime, logIp = @logIp, logTime = @logTime, tel = @tel, photo = @photo, examine = @examine, gradeId = @gradeId, departmentId = @departmentId, company = @company, appear = @appear, networkTest = @networkTest, Auditing = @Auditing, manageAll = @manageAll, usersGroupId = @usersGroupId, readInfoType = @readInfoType, topRight = @topRight, recommandRight = @recommandRight, isSbwUser=@isSbwUser, pwdlevel=@pwdlevel WHERE (pkId = @pkId)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateUsersGroup]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateUsersGroup]
GO







CREATE PROCEDURE [dbo].[UpdateUsersGroup]
(
	@readInfoType int,
	@Auditing int,
	@topRight int,
	@recommandRight int,
	@allowLogin int,
	@explain varchar(100),
	@pkId int,
	@groupName varchar(100)
)
AS
	SET NOCOUNT OFF;
UPDATE UsersGroup SET readInfoType = @readInfoType, Auditing = @Auditing, topRight = @topRight, recommandRight = @recommandRight, allowLogin = @allowLogin, explain = @explain ,groupName=@groupName WHERE (pkId = @pkId)






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateWebResear]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateWebResear]
GO






CREATE PROCEDURE [dbo].[UpdateWebResear]
(@questionnaireSpecialId int, @questionName varchar(1000), @NotNull int, @optionsType int, @optionsCount int, @pkId int)
AS
	SET NOCOUNT OFF;
UPDATE question SET questionnaireSpecialId = @questionnairespecialId, questionName = @questionName, NotNull = @NotNull, optionsType = @optionsType, optionsCount = @optionsCount WHERE (pkId = @pkId)





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateWebResearByTitle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateWebResearByTitle]
GO






CREATE PROCEDURE [dbo].[UpdateWebResearByTitle]
(@questionnaireSpecialName varchar(1000), @pkId int, @validDays int, @gate int, @gradeId int, @IsAnonymity int, @restrictObject int, @restrictMode int, @restrictTime int, @IsVerify int, @generateDate datetime, @IsShow int, @IsShowNumber int)
AS
	SET NOCOUNT OFF;
UPDATE questionnaireSpecial 
SET questionnaireSpecialName = @questionnaireSpecialName,
validDays = @validDays,
gate = @gate,
gradeId = @gradeId,
IsAnonymity = @IsAnonymity,
restrictObject = @restrictObject,
restrictMode = @restrictMode,
restrictTime = @restrictTime,
IsVerify = @IsVerify,
generateDate =@generateDate,
IsShow = @IsShow,
IsShowNumber = @IsShowNumber
 WHERE (pkId = @pkId)












GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UpdateWebResearList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UpdateWebResearList]
GO





-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateWebResearList]
	-- Add the parameters for the stored procedure here
	@pkId int,
	@bigClassId int,
	@questionnaireSpecialName varchar(1000), 
	@validDays int, 
	@gate int, 
	@gradeId int, 
	@IsAnonymity int, 
	@restrictObject int, 
	@restrictMode int, 
	@restrictTime int, 
	@IsVerify int, 
	@generateDate datetime, 
	@IsShow int,
	@IsShowNumber int,
	@ItemType int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update questionnaireSpecial set bigClassId = @bigClassId,
	questionnaireSpecialName=@questionnaireSpecialName,
	validDays=@validDays,
	gate=@gate,
	gradeId=@gradeId,
	IsAnonymity=@IsAnonymity,
	restrictObject=@restrictObject,
	restrictMode=@restrictMode,
	restrictTime=@restrictTime,
	IsVerify =@IsVerify,
	generateDate=@generateDate,
	IsShow=@IsShow,
	IsShowNumber=@IsShowNumber,
	ItemType=@ItemType 
	where pkId = @pkId

END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserApplyDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UserApplyDelete]
GO






CREATE PROCEDURE UserApplyDelete
	@PkId int
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		DELETE FROM UserApply WHERE pkId = @PkId
	COMMIT TRANSACTION
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserApplyDetails]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UserApplyDetails]
GO






CREATE PROCEDURE UserApplyDetails 	
	@PkId int = 0	
AS
	select top 1 * from UserApply where pkId = @PkId
	RETURN






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserApplyInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UserApplyInsert]
GO






CREATE PROCEDURE [dbo].[UserApplyInsert] 
	@PkId int OUTPUT,	
	@UnitName varchar(150),
	@IBE varchar(150),
	@UserName varchar(20),
    @Password varchar(18),
    @Province varchar(150),
	@City varchar(150),
	@County varchar(150),
    @UnitType int,
    @Phone varchar(50),
    @Email varchar(80),
	@AllowSendSms bit,
	@AllowSmsRemind bit,
	@Memo nText
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		INSERT INTO UserApply (unitName, ibe, userName, password, province, city, county, unitType, phone, email, allowSendSms, allowSmsRemind, memo) 
		VALUES (@UnitName, @IBE, @UserName, @Password, @Province, @City, @County, @UnitType, @Phone, @Email, @AllowSendSms, @AllowSmsRemind, @Memo)
	COMMIT TRANSACTION
	SET @pkId = SCOPE_IDENTITY()
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserApplyList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UserApplyList]
GO






CREATE PROCEDURE [dbo].[UserApplyList]
	@PageIndex int,
	@PageSize int,
	@PageCount int output,
	@RecordCount int output
AS
	SELECT @PageIndex = @PageIndex - 1
	
	DECLARE @tempNum int	
	DECLARE @vSQL nvarchar(2000)

	SET @vSQL = N'SELECT @count = Count(pkId) FROM UserApply'
	EXEC sp_ExecuteSQL @vSQL, N'@count int output', @tempNum output
	SELECT @RecordCount = @tempNum

	SET @PageCount = (@RecordCount + @PageSize-1)/@PageSize

	DECLARE @sqlStr varchar(2000),@size int

	SET @size = @RecordCount%@PageSize	
	IF(@size=0)
		SET @size = @PageSize	

	IF (@PageIndex = 0) OR (@PageIndex > @PageCount-1)	
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM UserApply ORDER BY pkId DESC'	
	ELSE IF @PageIndex = @PageCount -1			
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@size) + ' * FROM UserApply ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'	
	ELSE IF @PageIndex > @PageCount/2			
		SET @sqlStr = 'SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@RecordCount - @PageSize * @PageIndex ) + ' * FROM UserApply ORDER BY pkId ASC) TempTable ORDER BY pkId DESC'
	ELSE BEGIN							
		SET @PageIndex=@PageIndex+1
		SET @sqlStr = 'SELECT * FROM (SELECT TOP ' + STR(@PageSize) + ' * FROM (SELECT TOP ' 
			        + STR(@PageSize * @PageIndex ) + ' * FROM UserApply ORDER BY pkId DESC) TempTable1 '
					+ ' ORDER BY pkId ASC) TempTable2 ORDER BY pkId DESC'
	END
	EXEC(@sqlStr)
	RETURN





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserApplyUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[UserApplyUpdate]
GO






CREATE PROCEDURE UserApplyUpdate 
	@PkId int,	
	@UnitName varchar(150),
	@IBE varchar(150),
	@UserName varchar(20),
    @Province varchar(150),
	@City varchar(150),
	@County varchar(150),
    @UnitType int,
    @Phone varchar(50),
    @Email varchar(80),
	@AllowSendSms bit,
	@AllowSmsRemind bit,
	@Memo nText
AS
BEGIN
	SET NOCOUNT OFF;
	BEGIN TRANSACTION
		UPDATE UserApply SET unitName = @UnitName, ibe =@IBE , userName = @UserName, province = @Province, city  = @City,
							 county = @County, unitType = @UnitType, phone = @Phone, email = @Email, 
							 allowSendSms = @AllowSendSms, allowSmsRemind = @AllowSmsRemind, memo = @Memo
	    WHERE pkId = @PkId		
	COMMIT TRANSACTION
END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ZCGetMessageByTime]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ZCGetMessageByTime]
GO


CREATE PROCEDURE ZCGetMessageByTime
@beginTime datetime,
@endTime datetime,
@bigClassId int
AS
BEGIN
	if(@bigClassId=0)
	begin
		SELECT pkId as ID,intactTitle as Title ,descript as Introduction ,ImageName,[content] as [Content],UpdateTime
		FROM [News] 
		WHERE bigclassid in (272,270,469) and updatetime between @beginTime and @endTime
		ORDER BY istop desc,Priority desc,updateTime desc,pkid desc
	end
	else
	begin
		SELECT pkId as ID,intactTitle as Title ,descript as Introduction ,ImageName,[content] as [Content],UpdateTime
		FROM [News] 
		WHERE bigclassid=@bigClassId and updatetime between @beginTime and @endTime
		ORDER BY istop desc,Priority desc,updateTime desc,pkid desc
	end
	RETURN 
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'GetAspProperty') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure GetAspProperty
GO

CREATE PROCEDURE GetAspProperty
AS
	SET NOCOUNT ON;
SELECT * FROM AspProperty

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

--此存储过程还在站群网站的备份子站功能处调用了，所以不能删除。
if exists (select * from dbo.sysobjects where id = object_id(N'GetUnitInfo') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure GetUnitInfo
GO

CREATE PROCEDURE GetUnitInfo
AS
	SET NOCOUNT ON;
SELECT * FROM unitInfo

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


