/****** Object:  Table [dbo].[K_Visiter]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Visiter](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[userId] [int] NOT NULL,
	[visitId] [int] NOT NULL,
	[addTime] [datetime] NOT NULL,
 CONSTRAINT [PK_K_Visiter] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Video]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Video](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[speId] [int] NOT NULL,
	[channelId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[bTime] [datetime] NOT NULL,
	[state] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
	[vLink] [nvarchar](256) NOT NULL,
	[picId] [int] NOT NULL,
	[trueLink] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_K_Video] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_VideoTitle] ON [dbo].[K_Video] 
(
	[title] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_UserScore]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_UserScore](
	[rank] [int] NOT NULL,
	[score] [int] NOT NULL,
 CONSTRAINT [PK_K_UserScore] PRIMARY KEY CLUSTERED 
(
	[score] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_UserRankType]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_UserRankType](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[rankTypeName] [nvarchar](50) NOT NULL,
	[isDefault] [bit] NOT NULL,
 CONSTRAINT [PK_K_UserRankType] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_UserRank]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_UserRank](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[rankName] [nvarchar](50) NOT NULL,
	[rank] [int] NOT NULL,
	[rankTypeId] [int] NOT NULL,
 CONSTRAINT [PK_K_UserRank] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_User]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_User](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[userName] [nvarchar](50) NOT NULL,
	[userPass] [char](12) NOT NULL,
	[sex] [tinyint] NOT NULL,
	[email] [nvarchar](150) NOT NULL,
	[qq] [varchar](50) NOT NULL,
	[isAdmin] [bit] NOT NULL,
	[score] [int] NOT NULL,
	[addTime] [datetime] NOT NULL,
	[lastTime] [datetime] NOT NULL,
	[lastIp] [varchar](50) NOT NULL,
	[prevTime] [datetime] NOT NULL,
	[prevIp] [varchar](50) NOT NULL,
	[state] [tinyint] NOT NULL,
	[picId] [int] NOT NULL,
	[rankTypeId] [int] NOT NULL,
	[isOpen] [bit] NOT NULL,
	[skinPath] [nvarchar](50) NOT NULL,
	[isActiv] [bit] NOT NULL,
 CONSTRAINT [PK_F_User] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_K_email] ON [dbo].[K_User] 
(
	[email] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_userName] ON [dbo].[K_User] 
(
	[userName] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_userNameApass] ON [dbo].[K_User] 
(
	[userName] ASC,
	[userPass] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Task]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Task](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[taskName] [nvarchar](50) NOT NULL,
	[intro] [nvarchar](256) NOT NULL,
	[spanSecond] [float] NOT NULL,
	[state] [bit] NOT NULL,
	[startHour] [tinyint] NOT NULL,
	[endHour] [tinyint] NOT NULL,
	[lastTime] [datetime] NOT NULL,
 CONSTRAINT [PK_K_Task] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_TagSub]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_TagSub](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[tagId] [int] NOT NULL,
	[subType] [int] NOT NULL,
	[subId] [int] NOT NULL,
 CONSTRAINT [PK_K_TagSub] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_subTypeAsubId] ON [dbo].[K_TagSub] 
(
	[subType] ASC,
	[subId] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Tag]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_Tag](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[tagName] [nvarchar](50) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[state] [bit] NOT NULL,
	[hits] [int] NOT NULL,
	[fileName] [varchar](100) NOT NULL,
 CONSTRAINT [PK_K_Tag] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[K_Special]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Special](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[channelId] [int] NOT NULL,
	[speName] [nvarchar](50) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[upTime] [datetime] NOT NULL,
	[state] [bit] NOT NULL,
	[hits] [int] NOT NULL,
	[picId] [int] NOT NULL,
	[newsLastTime] [datetime] NOT NULL,
	[bowenLastTime] [datetime] NOT NULL,
	[askLastTime] [datetime] NOT NULL,
	[picLastTime] [datetime] NOT NULL,
	[videoLastTime] [datetime] NOT NULL,
	[intro] [nvarchar](256) NOT NULL,
	[askClassId] [int] NOT NULL,
 CONSTRAINT [PK_K_Special] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_upTime] ON [dbo].[K_Special] 
(
	[upTime] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Search]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Search](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[wd] [nvarchar](50) NOT NULL,
	[t] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
 CONSTRAINT [PK_K_Search] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Robots]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_Robots](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[pathInfo] [nvarchar](256) NOT NULL,
	[agent] [nvarchar](256) NOT NULL,
	[IP] [nvarchar](50) NOT NULL,
	[agentType] [varchar](50) NOT NULL,
 CONSTRAINT [PK_K_Robots] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[K_Pic]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Pic](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[speId] [int] NOT NULL,
	[channelId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[addtime] [datetime] NOT NULL,
	[bTime] [datetime] NOT NULL,
	[state] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
	[bigUrl] [nvarchar](256) NOT NULL,
	[picId] [int] NOT NULL,
 CONSTRAINT [PK_K_Pic] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_PicTitle] ON [dbo].[K_Pic] 
(
	[title] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_News]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_News](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[speId] [int] NOT NULL,
	[channelId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[intro] [nvarchar](256) NOT NULL,
	[Content] [ntext] NOT NULL,
	[addTime] [datetime] NOT NULL,
	[bTime] [datetime] NOT NULL,
	[state] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
	[url] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_K_News] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_NewsTitle] ON [dbo].[K_News] 
(
	[title] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_MessageDel]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_MessageDel](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[postId] [int] NOT NULL,
	[userId] [int] NOT NULL,
 CONSTRAINT [PK_K_MessageDel] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Message]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Message](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[title] [nvarchar](100) NOT NULL,
	[content] [nvarchar](256) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[fId] [int] NOT NULL,
	[tId] [int] NOT NULL,
	[isRead] [bit] NOT NULL,
 CONSTRAINT [PK_K_Message] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Filethum]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_Filethum](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[attrid] [int] NOT NULL,
	[thumpath] [varchar](250) NOT NULL,
 CONSTRAINT [PK_F_Filethum] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[K_FilesUser]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_FilesUser](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[postid] [int] NOT NULL,
	[posttype] [tinyint] NOT NULL,
	[fileid] [int] NOT NULL,
 CONSTRAINT [PK_K_FilesUser] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Files]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_Files](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[attrId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[fileName] [nvarchar](250) NOT NULL,
	[fileType] [varchar](5) NOT NULL,
	[addTime] [datetime] NOT NULL,
 CONSTRAINT [PK_F_Files] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_K_attrId] ON [dbo].[K_Files] 
(
	[attrId] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_FileAttr]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_FileAttr](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[fileDNA] [varchar](50) NOT NULL,
	[path] [nvarchar](250) NOT NULL,
	[fileSiize] [bigint] NOT NULL,
 CONSTRAINT [PK_F_Fileattr] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_K_dna] ON [dbo].[K_FileAttr] 
(
	[fileDNA] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_ErrUrl]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_ErrUrl](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[url] [nvarchar](256) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[agent] [nvarchar](256) NOT NULL,
	[ip] [nvarchar](50) NOT NULL,
	[refer] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_K_ErrUrl] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Channel]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Channel](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[channelName] [nvarchar](50) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[sort] [int] NOT NULL,
 CONSTRAINT [PK_K_Channel] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Bowen]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Bowen](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[speId] [int] NOT NULL,
	[channelId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[intro] [nvarchar](256) NOT NULL,
	[Content] [ntext] NOT NULL,
	[addTime] [datetime] NOT NULL,
	[bTime] [datetime] NOT NULL,
	[state] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
	[url] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_K_Bowen] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_BowenTitle] ON [dbo].[K_Bowen] 
(
	[title] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_AskContent]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_AskContent](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[askId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[content] [ntext] NOT NULL,
	[addTime] [datetime] NOT NULL,
	[isBest] [bit] NOT NULL,
	[isNM] [bit] NOT NULL,
	[star] [tinyint] NOT NULL,
	[state] [bit] NOT NULL,
 CONSTRAINT [PK_K_AskContent] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_askId] ON [dbo].[K_AskContent] 
(
	[askId] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_AskClass]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[K_AskClass](
	[id] [int] NOT NULL,
	[name] [nvarchar](50) NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[parent] [int] NOT NULL,
	[parentstr] [varchar](500) NOT NULL,
	[pai] [int] NOT NULL,
	[depth] [int] NOT NULL,
 CONSTRAINT [PK_K_AskClass] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [IX_K_pai] ON [dbo].[K_AskClass] 
(
	[pai] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_parent] ON [dbo].[K_AskClass] 
(
	[parent] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_parentstr] ON [dbo].[K_AskClass] 
(
	[parentstr] ASC
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[K_Ask]    Script Date: 08/04/2011 07:09:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[K_Ask](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[speId] [int] NOT NULL,
	[channelId] [int] NOT NULL,
	[userId] [int] NOT NULL,
	[classId] [int] NOT NULL,
	[title] [nvarchar](150) NOT NULL,
	[pageTitle] [nvarchar](150) NOT NULL,
	[content] [ntext] NOT NULL,
	[added] [nvarchar](1000) NOT NULL,
	[addTime] [datetime] NOT NULL,
	[bTime] [datetime] NOT NULL,
	[xsf] [int] NOT NULL,
	[state] [tinyint] NOT NULL,
	[hits] [int] NOT NULL,
	[url] [nvarchar](256) NOT NULL,
	[isNM] [bit] NOT NULL,
 CONSTRAINT [PK_K_Ask] PRIMARY KEY CLUSTERED 
(
	[id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_AskTitle] ON [dbo].[K_Ask] 
(
	[title] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_K_userId] ON [dbo].[K_Ask] 
(
	[userId] ASC
) ON [PRIMARY]
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskTopList]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskTopList]
		    @row int,
		    @order nvarchar(400),
		    @where nvarchar(400)
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(4000)
		    
		    set @sql='SELECT top '+CAST(@row as nvarchar)+' [id],[title],[addTime],[xsf] From [k_Ask] WITH(NOLOCK) WHERE '+@where+' order by '+@order+''

			execute sp_executesql @sql
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskclassList]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskclassList]
		    @parentId int,
		    @row int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(4000)
		    if @parentId=0
				set @sql='SELECT top '+CAST(@row as nvarchar)+' [id],[name] From [K_Askclass] WITH(NOLOCK) WHERE parent=0 order by pai asc,id asc'
			else
				set @sql='SELECT top '+CAST(@row as nvarchar)+' [id],[name] From [K_Askclass] WITH(NOLOCK) WHERE id<>'+cast(@parentId as nvarchar)+' and parentstr like '','+cast(@parentId as nvarchar)+',%'' order by depth asc,pai asc,id asc'
		
			execute sp_executesql @sql
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_Common_GetRecordsByPageSQLString]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--返回分页的sql语句
CREATE PROCEDURE [dbo].[Kcms_Common_GetRecordsByPageSQLString]
	@TableName varchar(150),
	@IndexField varchar(150)=N'id',--主键字段，定位not in
	@AllFields varchar(1000)=N'*', --需要返回的列 
	@Where varchar(1000)=N'', -- 条件,不要where
	@OrderFields varchar(255)=N'',-- 排序,不要order by 要desc asc
	@PageSize int = 10,
	@PageIndex int = 1,
	@SQLString  nvarchar(4000) output
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @WhereString1 nvarchar(4000);
	DECLARE @WhereString2 nvarchar(4000);
	IF @Where IS NULL OR @Where = N'' BEGIN
		SELECT @WhereString1 = N'';
		SELECT @WhereString2 = N'WHERE ';
	END
	ELSE BEGIN
		SELECT @WhereString1 = N'WHERE ' + @Where;
		SELECT @WhereString2 = N'WHERE ' + @Where + N' AND ';
	END
	
	IF @PageIndex=1 BEGIN
			SELECT @SQLString = N'SELECT TOP ' + CAST(@PageSize as nvarchar)
			+ N' ' + @AllFields
			+ N' FROM ' + @TableName + ' WITH (NOLOCK)
			' + @WhereString1 + '
			ORDER BY ' + @OrderFields
	END
	ELSE BEGIN
			DECLARE @start int
			SET @start = (@PageIndex - 1) * @PageSize
			SELECT @SQLString = 'SELECT TOP ' + CAST(@PageSize as nvarchar) + ' ' +  @AllFields  + ' FROM [' +  +@TableName+ '] '+ @WhereString2 + ' '+ @IndexField  + ' NOT IN (SELECT TOP ' + CAST(@start as nvarchar) + ' ' + @IndexField + ' FROM [' + +@TableName+ '] '+ @WhereString1 + ' ORDER BY ' + @OrderFields +') ORDER BY '+ @OrderFields
	END
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNewsTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNewsTop]
		    @row Int,
		    @order nvarchar(400),
		    @where nvarchar(400)
		AS
		BEGIN
		    SET NOCOUNT ON;
		exec('
		    SELECT top '+@row+' [id],[title] From [k_News] WITH(NOLOCK) WHERE '+@where+' order by '+@order+' 
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowenTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowenTop]
		    @row Int,
		    @order nvarchar(400),
		    @where nvarchar(400)
		AS
		BEGIN
		    SET NOCOUNT ON;
		exec('
		    SELECT top '+@row+' [id],[title] From [k_Bowen] WITH(NOLOCK) WHERE '+@where+' order by '+@order+'
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPicTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPicTop]
		    @row Int,
		    @order nvarchar(400),
		    @where nvarchar(400)
		AS
		BEGIN
		    SET NOCOUNT ON;
		exec('
		    SELECT top '+@row+' [id],[title],[picId] From [k_Pic] WITH(NOLOCK) WHERE '+@where+' order by '+@order+'
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpecialTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpecialTop]
		    @row Int,
		    @order nvarchar(400),
		    @where nvarchar(400)=null
		AS
		BEGIN
		    SET NOCOUNT ON;
		exec('
		    SELECT top '+@row+' [id],[spename],[picid] From [k_Special] WITH(NOLOCK) WHERE '+@where+' order by '+@order+'
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTagTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTagTop]	
		    @row Int,
		    @type Int,
		    @rnd bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    declare @where nvarchar(200),@sort nvarchar(100),@dis nvarchar(30)
		    if @type>=0
		    set @where='and b.subType='+ cast(@type as nvarchar)
		    if @rnd=1
		    set @sort='newid()'
		    else begin
		    set @sort='a.addtime desc'
		    set @dis='distinct'
		    end
		    
		exec('
		   select '+@dis+' top '+@row+' a.id as id,a.tagname,a.addtime from K_Tag a with(nolock) inner join K_TagSub b with(nolock) on a.id=b.tagid where a.state=1 '+@where+' order by '+@sort+'
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideoTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideoTop]
		    @row Int,
		    @order nvarchar(400),
		    @where nvarchar(400)
		AS
		BEGIN
		    SET NOCOUNT ON;
		exec('
		    SELECT top '+@row+' [id],[title],[picId] From [k_Video] WITH(NOLOCK) WHERE '+@where+' order by '+@order+'
		')
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(500)
		    if @tj=1
				set @sql=',btime=getdate()'
		    
		   exec('update K_Ask set state='+@state+@sql+' where id='+@id)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveBowenState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveBowenState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(500)
		    if @tj=1
				set @sql=',btime=getdate()'
		    
		   exec('update K_Bowen set state='+@state+@sql+' where id='+@id)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveNewsState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveNewsState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(500)
		    if @tj=1
				set @sql=',btime=getdate()'
		    
		   exec('update K_News set state='+@state+@sql+' where id='+@id)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveVideoState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveVideoState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(500)
		    if @tj=1
				set @sql=',btime=getdate()'
		    
		   exec('update K_Video set state='+@state+@sql+' where id='+@id)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSearchTop]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSearchTop]	
		    @row int,
		    @rnd bit
		AS
		BEGIN
		    SET NOCOUNT ON;
			
			if @rnd=0
				exec('select top '+@row+' wd,t from K_Search with(nolock) order by hits desc')
			else
				exec('select top '+@row+' wd,t from K_Search with(nolock) order by newid()')
		    
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SavePicState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SavePicState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    declare @sql nvarchar(500)
		    if @tj=1
				set @sql=',btime=getdate()'
		    
		   exec('update K_Pic set state='+@state+@sql+' where id='+@id)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SavePicBigId]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SavePicBigId]
		    @id Int,
		    @bigid int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
			update K_Pic set bigUrl=@bigid where id=@id
		
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SavePic]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SavePic]
		    @action int,
		    @id Int = NULL,
		    @speid Int = NULL,
		    @channelid Int = NULL,
		    @userid Int = NULL,
		    @title NVarChar(150) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @addtime DateTime = NULL,
		    @btime datetime=null,
		    @state TinyInt = NULL,
		    @hits Int = NULL,
		    @bigurl NVarChar(256) = NULL,
		    @picid int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Pic]([speId],[channelid],[userId],[title],[pageTitle],[addtime],[bTime],[state],[hits],[bigUrl],[picId]) 
								    VALUES (@speId,@channelid,@userId,@title,@pageTitle,@addtime,@btime,@state,@hits,@bigUrl,@picid)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Pic] SET 
				    [speid]=@speid,
				    [channelid]=@channelid,
				    [userid]=@userid,
				    [title]=@title,
				    [pagetitle]=@pagetitle,
				    [addtime]=@addtime,
				    [bTime]=@btime,
				    [state]=@state,
				    [hits]=@hits,
				    [bigurl]=@bigurl,
				    [picid]=@picid
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Pic] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetRobotsPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetRobotsPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Robots',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Robots] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetRobotsCount]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetRobotsCount]
	@date datetime
AS
BEGIN

	SET NOCOUNT ON;
	SELECT CONVERT(varchar(13), addTime,120) as sj,
	count(case when AgentType = 'baidu' then 0 end) as baidu,
	count(case when AgentType = 'google' then 0 end) as google,
	count(case when AgentType = 'yahoo' then 0 end) as yahoo,
	count(case when AgentType = 'soso' then 0 end) as soso,
	count(case when AgentType = 'sogou' then 0 end) as sogou,
	count(case when AgentType = '' then 0 end) as other
	FROM K_Robots with(nolock)
	where addtime>=@date and addtime<DATEADD(day,1,@date)
	GROUP BY CONVERT(varchar(13), addTime,120)
	order by sj;


END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_UpdateEndTime]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_UpdateEndTime]
		    @id Int,
		    @userId int,
		    @sc int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_Ask set state=4 where state<4 and id=@id
		    if @@ROWCOUNT>0
			update K_User set score=score-@sc where id=@userId
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetUserLoginUpdate]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetUserLoginUpdate]
	@username nvarchar(50),
	@userpass varchar(12),
	@lastip nvarchar(50),
	@score int
AS
BEGIN
	SET NOCOUNT ON;
		
		update K_User set score=score+@score where lastTime<DATEADD(day,0,GETDATE()) and username=@username and userpass=@userpass
	
		update K_User set
        [lasttime]=getdate(),
        [lastip]=@lastip,
        [prevtime]=lasttime,
        [previp]=lastip 
        where state>0 and username=@username and userpass=@userpass
        
        return @@ROWCOUNT
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetTagExt]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetTagExt]
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    delete from K_Tag where not exists(select 0 from K_TagSub b with(nolock) where K_Tag.id=b.tagId)

			delete from K_Message where addTime<DATEADD(day,-30,GETDATE()) and isRead=1
		    delete from K_ErrUrl where addTime<DATEADD(day,-30,GETDATE())
		    delete from K_Robots where addTime<DATEADD(day,-30,GETDATE())

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetRobotsDelete10]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetRobotsDelete10]

AS
BEGIN
	SET NOCOUNT ON;
	declare @date datetime
	set @date=dateadd(day,-10,getdate())
	
	delete from K_Robots where addtime<@date

END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetFileExt]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetFileExt]
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    delete from K_Files where id in(select top 100 id from K_Files a with(nolock) where addTime<DATEADD(day,-1,GETDATE()) and not exists(select 0 from K_FilesUser b with(nolock) where a.id=b.fileid))
		    
			select top 100 [path] from K_FileAttr a with(nolock) where not exists(select 0 from K_Files b with(nolock) where a.id=b.attrId)
			delete from K_FileAttr where id in(select top 100 id from K_FileAttr a with(nolock) where not exists(select 0 from K_Files b with(nolock) where a.id=b.attrId))
			
			select top 100 thumpath from K_Filethum a with(nolock) where not exists(select 0 from K_FileAttr b with(nolock) where a.attrid=b.id)
			delete from K_Filethum where id in(select top 100 id from K_Filethum a with(nolock) where not exists(select 0 from K_FileAttr b with(nolock) where a.attrid=b.id))

			
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetErrurlDelete10]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetErrurlDelete10]

AS
BEGIN
	SET NOCOUNT ON;
	declare @date datetime
	set @date=dateadd(day,-10,getdate())
	
	delete from K_ErrUrl where addtime<@date

END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetDelFileUserAndTag]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetDelFileUserAndTag]
	@postid int,
	@posttype tinyint
AS
BEGIN
	SET NOCOUNT ON;
	
	delete from K_FilesUser where posttype=@posttype and postid=@postid
	delete from K_TagSub where subType=@posttype and subId=@postid
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SetAskExt]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SetAskExt]
		    @ext datetime,
		    @extscore int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
			update K_User set score=score-@extscore where id in(SELECT userId From [k_Ask] WITH(NOLOCK) WHERE state=1 and addtime<@ext)
			update K_Ask set state=4 where id in(SELECT id From [k_Ask] WITH(NOLOCK) WHERE state=1 and addtime<@ext)
		
		--DECLARE @id int
		--DECLARE @userid int
		
		--DECLARE [attr_cursor] CURSOR FOR (SELECT top 100 id,userId From [k_Ask] WITH(NOLOCK) WHERE addtime<@ext and state=1) FOR READ ONLY
		--OPEN [attr_cursor]
		--FETCH NEXT FROM [attr_cursor] INTO @id,@userid
		--WHILE @@FETCH_STATUS = 0
		--	BEGIN
		--		update K_Ask set state=4 where id=@id
		--		update K_User set score=score-@extscore where id=@userid
				
		--		FETCH NEXT FROM [attr_cursor] INTO @id,@userid
		--	END
		--CLOSE [attr_cursor]
		--DEALLOCATE [attr_cursor]

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveVisiter]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveVisiter]
		    @userId int,
		    @visitId int
		AS
		BEGIN
		    SET NOCOUNT ON;  
		    
		    update K_Visiter set addTime=GETDATE() where visitId=@visitId and userId=@userId
			if @@ROWCOUNT=0
			begin
				delete from K_Visiter where id not in(select top 9 id from K_Visiter with(nolock) where userId=@userId order by addTime desc)

				insert into K_Visiter(userId,visitId,addTime)
						values(@userId,@visitId,GETDATE())
			end
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveVideo]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveVideo]
		    @action int,
		    @id Int = NULL,
		    @speid Int = NULL,
		    @channelid Int = NULL,
		    @userid Int = NULL,
		    @title NVarChar(150) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @addtime DateTime = NULL,
		    @btime datetime =null,
		    @state TinyInt = NULL,
		    @hits Int = NULL,
		    @vlink NVarChar(256) = NULL,
		    @picId int = NULL,
		    @truelink NVarChar(256) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Video]([speId],[channelid],[userId],[title],[pageTitle],[addTime],[bTime],[state],[hits],[vLink],[picId],[trueLink]) 
								    VALUES (@speId,@channelid,@userId,@title,@pageTitle,@addTime,@btime,@state,@hits,@vLink,@picId,@trueLink)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Video] SET 
				    [speid]=@speid,
				    [channelid]=@channelid,
				    [userid]=@userid,
				    [title]=@title,
				    [pagetitle]=@pagetitle,
				    [addtime]=@addtime,
				    [bTime]=@btime,
				    [state]=@state,
				    [hits]=@hits,
				    [vlink]=@vlink,
				    [picId]=@picId,
				    [truelink]=@truelink
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Video] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserState]
		    @id int,
		    @state tinyint,
		    @tj bit
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_User set state=@state where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserSkinPath]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserSkinPath]
		    @id Int,
		    @skinPath NVarChar(50)
		AS
		BEGIN
		    SET NOCOUNT ON;
				    UPDATE [K_User] SET 
				    [skinPath]=@skinPath
				    WHERE id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserScoreSpan]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserScoreSpan]
		    @id Int,
		    @scoreSpan int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
			update K_User set score=score+@scoreSpan where id=@id
		
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserscore]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserscore]
		    @action int,
		    @rank int=null,
		    @score Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				--if exists(select 0 from K_UserScore with(nolock) where score=@score)
				--select 0
				--else begin
				    INSERT INTO [K_Userscore]([rank],[score]) 
								    VALUES (@rank,@score)
					select @rank
				--end
			    RETURN
		    END
		    IF @action=1 BEGIN
				if exists(select 0 from K_UserScore with(nolock) where [rank]<>@rank and score=@score)
				select 0
				else begin
				    UPDATE [K_Userscore] SET 
				    [score]=@score
				    WHERE [rank]=@rank
				    select @rank
				end
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Userscore] 
				    WHERE [rank]=@rank
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserranktypeDefault]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserranktypeDefault]
		    @id Int = NULL
		AS
		BEGIN
			update K_UserRankType set isDefault=0 where isDefault=1
			update K_UserRankType set isDefault=1 where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserranktype]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserranktype]
		    @action int,
		    @id Int = NULL,
		    @ranktypename NVarChar(50) = NULL,
		    @isdefault Bit = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Userranktype]([rankTypeName],[isDefault]) 
								    VALUES (@rankTypeName,@isDefault)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Userranktype] SET 
				    [ranktypename]=@ranktypename,
				    [isdefault]=@isdefault
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Userranktype] 
				    WHERE id=@id
				    
				    delete from K_UserRank where rankTypeId=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserrank]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserrank]
		    @action int,
		    @id Int = NULL,
		    @rankname NVarChar(50) = NULL,
		    @rank Int = NULL,
		    @ranktypeid Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Userrank]([rankName],[rank],[rankTypeId]) 
								    VALUES (@rankName,@rank,@rankTypeId)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Userrank] SET 
				    [rankname]=@rankname
				    --[rank]=@rank,
				    --[ranktypeid]=@ranktypeid
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Userrank] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserPass]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserPass]	
			@id int,
		    @oldpass char(12),
		    @newpass char(12)
		AS
		BEGIN
		    SET NOCOUNT ON;
		    if @oldpass=''
		    update K_User set userPass=@newpass where id=@id
		    else
			update K_User set userPass=@newpass where userPass=@oldpass and id=@id
			return @@ROWCOUNT
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserCenter]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserCenter]
		    @id Int,
		    @sex TinyInt,
		    @email NVarChar(150),
		    @qq VarChar(50),
		    @picid int,
		    @ranktypeid int,
		    @isopen bit
		AS
		BEGIN
		    SET NOCOUNT ON;
				    UPDATE [K_User] SET 
				    [sex]=@sex,
				    [email]=@email,
				    [qq]=@qq,
				    [picId]=@picid,
				    [rankTypeId]=@ranktypeid,
				    [isOpen]=@isopen
				    WHERE id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUserActive]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUserActive]	
			@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_User set state=1, isActiv=1 where isActiv=0 and id=@id
			return @@ROWCOUNT
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveUser]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveUser]
		    @action int,
		    @id Int = NULL,
		    @username NVarChar(50) = NULL,
		    @userpass Char(12) = NULL,
		    @sex TinyInt = NULL,
		    @email NVarChar(150) = NULL,
		    @qq VarChar(50) = NULL,
		    @isadmin Bit = NULL,
		    @score Int = NULL,
		    @addtime DateTime = NULL,
		    @lasttime DateTime = NULL,
		    @lastip VarChar(50) = NULL,
		    @prevtime DateTime = NULL,
		    @previp VarChar(50) = NULL,
		    @state TinyInt = NULL,
		    @picid int=null,
		    @ranktypeid int=null
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_User]([userName],[userpass],[sex],[email],[qq],[isAdmin],[score],[addTime],[lastTime],[lastIp],[prevTime],[prevIp],[state],[picId],[rankTypeId]) 
								    VALUES (@userName,@userpass,@sex,@email,@qq,@isAdmin,@score,@addTime,@lastTime,@lastIp,@prevTime,@prevIp,@state,@picid,@ranktypeid)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_User] SET 
				    [username]=@username,
				    [userpass]=@userpass,
				    [sex]=@sex,
				    [email]=@email,
				    [qq]=@qq,
				    [isadmin]=@isadmin,
				    [score]=@score,
				    [addtime]=@addtime,
				    [lasttime]=@lasttime,
				    [lastip]=@lastip,
				    [prevtime]=@prevtime,
				    [previp]=@previp,
				    [state]=@state,
				    [picId]=@picid,
				    [rankTypeId]=@ranktypeid
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_User] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveTaskLastTime]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveTaskLastTime]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_Task set lastTime=GETDATE() where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveTask]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveTask]
		    @action int,
		    @id Int = NULL,
		    @taskname NVarChar(50) = NULL,
		    @intro NVarChar(256) = NULL,
		    @spansecond float = NULL,
		    @state Bit = NULL,
		    @starthour TinyInt = NULL,
		    @endhour TinyInt = NULL,
		    @lasttime DateTime = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Task]([taskName],[intro],[spanSecond],[state],[startHour],[endHour],[lastTime]) 
								    VALUES (@taskName,@intro,@spanSecond,@state,@startHour,@endHour,@lastTime)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Task] SET 
				    [taskname]=@taskname,
				    --[intro]=@intro,
				    [spansecond]=@spansecond,
				    [state]=@state,
				    [starthour]=@starthour,
				    [endhour]=@endhour
				    --[lasttime]=@lasttime
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Task] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveTagsub]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveTagsub]
		    @id Int = NULL,
		    @tagid Int = NULL,
		    @subtype Int = NULL,
		    @subid Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
			--删除旧的
			
			delete from K_TagSub where subType=@subtype and subId=@subid
			
			INSERT INTO [k_Tagsub]([tagId],[subType],[subId]) 
				VALUES (@tagId,@subType,@subId)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveTag]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveTag]
		    @action int,
		    @id Int = NULL,
		    @tagname NVarChar(50) = NULL,
		    @addtime DateTime = NULL,
		    @state Bit = NULL,
		    @hits Int = NULL,
		    @filename VarChar(100) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
		    
					--如果有相同的tag 就返回Id
					declare @ret int
					select @ret=id from K_Tag with(nolock) where tagName=@tagname
					if @ret>0 begin
					select @ret
					return
					end
		    
				    INSERT INTO [k_Tag]([tagName],[addTime],[state],[hits],[fileName]) 
								    VALUES (@tagName,@addTime,@state,@hits,@fileName)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Tag] SET 
				    [tagname]=@tagname,
				    [addtime]=@addtime,
				    [state]=@state,
				    [hits]=@hits,
				    [filename]=@filename
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Tag] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpeRele]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--释放专题
CREATE PROCEDURE [dbo].[Kcms_SaveSpeRele]	

AS
BEGIN
    SET NOCOUNT ON;
	
	declare @postId int
	set @postId=0
	select top 1 @postId=id from K_Special with(nolock) where state=0 order by NEWID()
	
	update K_Special set state=1 where id=@postId
	
	update K_Tag set state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=0 and subId=@postId)
	
	select @postId

END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpeGather]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSpeGather]
		    @id Int,
		    @uptime datetime,
		    @picid int,
		    @newslasttime datetime,
		    @bowenlasttime datetime,
		    @asklasttime datetime,
		    @piclasttime datetime,
		    @videolasttime datetime,
		    @intro nvarchar(256)
		AS
		BEGIN
		    SET NOCOUNT ON;
			update K_Special set 
			upTime=@uptime,
			[state]=1,
			picId=@picid,
			newsLastTime=@newslasttime,
			bowenLastTime=@bowenlasttime,
			askLastTime=@asklasttime,
			picLastTime=@piclasttime,
			videoLastTime=@videolasttime ,
			intro=@intro
			where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpeContentRele]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--释放专题
CREATE PROCEDURE [dbo].[Kcms_SaveSpeContentRele]	

AS
BEGIN
    SET NOCOUNT ON;
	
	declare @postId int
	set @postId=0
	select top 1 @postId=id from K_News with(nolock) where state=0 order by NEWID()
	update K_News set addTime=GETDATE(), state=1 where id=@postId
	update K_Tag set addTime=GETDATE(), state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=1 and subId=@postId)
	select @postId
	
	set @postId=0
	select top 1 @postId=id from K_Bowen with(nolock) where state=0 order by NEWID()
	update K_Bowen set addTime=GETDATE(), state=1 where id=@postId
	update K_Tag set addTime=GETDATE(), state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=2 and subId=@postId)
	select @postId

	set @postId=0
	select top 1 @postId=id from K_Ask with(nolock) where state=0 order by NEWID()
	update K_Ask set addTime=GETDATE(), state=1 where id=@postId
	update K_Tag set addTime=GETDATE(), state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=3 and subId=@postId)
	select @postId
	
	set @postId=0
	select top 1 @postId=id from K_Pic with(nolock) where state=0 order by NEWID()
	update K_Pic set addTime=GETDATE(), state=1 where id=@postId
	update K_Tag set addTime=GETDATE(), state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=4 and subId=@postId)
	select @postId
	
	set @postId=0
	select top 1 @postId=id from K_Video with(nolock) where state=0 order by NEWID()
	update K_Video set addTime=GETDATE(), state=1 where id=@postId
	update K_Tag set addTime=GETDATE(), state=1 where exists(select 0 from K_TagSub b where K_Tag.id=b.tagId and subType=5 and subId=@postId)
	select @postId

END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpecialState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSpecialState]
		    @id Int = NULL,
		    @state Bit = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		   update K_Special set state=@state where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpecialChannelId]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSpecialChannelId]
		    @id Int = NULL,
		    @channelid int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		   update K_Special set channelId=@channelid where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpecialAskClassId]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSpecialAskClassId]
		    @id Int = NULL,
		    @askclassid int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		   update K_Special set askClassId=@askclassid where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSpecial]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSpecial]
		    @action int,
		    @id Int = NULL,
		    @channelid Int = NULL,
		    @spename NVarChar(50) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @uptime DateTime = NULL,
		    @state Bit = NULL,
		    @hits Int = NULL,
		    @picid Int = NULL,
		    @intro nvarchar(256)=null,
		    @askclassid int=null
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		  --  	if EXISTS(select 0 From [K_Special] WITH(NOLOCK) WHERE id<>@id and [fileName]=@filename)
				--return -1
				
				if EXISTS(select 0 From [K_Special] WITH(NOLOCK) WHERE id<>@id and [speName]=@spename)
				return -2
		    
		    IF @action=0 BEGIN
				    INSERT INTO [K_Special]([channelId],[speName],[pageTitle],[upTime],[state],[hits],[picId],[intro],askClassId) 
								    VALUES (@channelid,@speName,@pageTitle,@upTime,@state,@hits,@picId,@intro,@askclassid)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Special] SET 
				    [channelId]=@channelid,
				    [spename]=@spename,
				    [pagetitle]=@pagetitle,
				    [uptime]=@uptime,
				    [state]=@state,
				    [hits]=@hits,
				    [picid]=@picid,
				    [intro]=@intro,
				    askClassId=@askclassid
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Special] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveSearch]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveSearch]
		    @wd NVarChar(50) = NULL,
		    @t TinyInt = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    delete from K_Search where id not in(select top 100 id from K_Search with(nolock) order by hits desc)
		    
		    update K_Search set hits=hits+1,t=@t where wd=@wd
		    if @@ROWCOUNT=0
		    insert into K_Search(wd,t,hits) values(@wd,@t,1)
		    
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveRobots]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveRobots]
		    @action int,
		    @id Int = NULL,
		    @addtime DateTime = NULL,
		    @pathinfo NVarChar(256) = NULL,
		    @agent NVarChar(256) = NULL,
		    @ip NVarChar(50) = NULL,
		    @agenttype VarChar(50) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Robots]([addTime],[pathInfo],[agent],[IP],[agentType]) 
								    VALUES (@addTime,@pathInfo,@agent,@IP,@agentType)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Robots] SET 
				    [addtime]=@addtime,
				    [pathinfo]=@pathinfo,
				    [agent]=@agent,
				    [ip]=@ip,
				    [agenttype]=@agenttype
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Robots] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveNews]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveNews]
		    @action int,
		    @id Int = NULL,
		    @speid Int = NULL,
		    @channelid Int = NULL,
		    @userid Int = NULL,
		    @title NVarChar(150) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @intro NVarChar(256) = NULL,
		    @content NText = NULL,
		    @addtime DateTime = NULL,
		    @btime datetime=null,
		    @state TinyInt = NULL,
		    @hits Int = NULL,
		    @url NVarChar(256) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_News]([speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url])
								    VALUES (@speId,@channelid,@userId,@title,@pageTitle,@intro,@Content,@addTime,@btime,@state,@hits,@url)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_News] SET 
				    [speid]=@speid,
				    [channelid]=@channelid,
				    [userid]=@userid,
				    [title]=@title,
				    [pagetitle]=@pagetitle,
				    [intro]=@intro,
				    [content]=@content,
				    [addtime]=@addtime,
				    [bTime]=@btime,
				    [state]=@state,
				    [hits]=@hits,
				    [url]=@url
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_News] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveMessagedel]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveMessagedel]
		    @action int,
		    @id Int = NULL,
		    @postid Int = NULL,
		    @userid Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Messagedel]([postId],[userId]) 
								    VALUES (@postId,@userId)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Messagedel] SET 
				    [postid]=@postid,
				    [userid]=@userid
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Messagedel] 
				    WHERE postid=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveMessage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveMessage]
		    @action int,
		    @id Int = NULL,
		    @title NVarChar(100) = NULL,
		    @content NVarChar(256) = NULL,
		    @addtime DateTime = NULL,
		    @fid Int = NULL,
		    @tid Int = NULL,
		    @isread Bit = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Message]([title],[content],[addTime],[fId],[tId],[isRead]) 
								    VALUES (@title,@content,@addTime,@fId,@tId,@isRead)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Message] SET 
				    [title]=@title,
				    [content]=@content,
				    [addtime]=@addtime,
				    [fid]=@fid,
				    [tid]=@tid,
				    [isread]=@isread
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Message] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveFilethum]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveFilethum]
		    @action int,
		    @id Int = NULL,
		    @attrid Int = NULL,
		    @thumpath VarChar(250) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Filethum]([attrid],[thumpath]) 
								    VALUES (@attrid,@thumpath)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Filethum] SET 
				    [attrid]=@attrid,
				    [thumpath]=@thumpath
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Filethum] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveFilesuser]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveFilesuser]
		    @postid Int = NULL,
		    @posttype TinyInt = NULL,
		    @fileid Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
					--先删除旧有的
					delete from K_FilesUser where posttype=@posttype and postid=@postid
					
				    INSERT INTO [K_Filesuser]([postid],[posttype],[fileid]) 
								    VALUES (@postid,@posttype,@fileid)
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveFiles]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveFiles]
		    @action int,
		    @id Int = NULL,
		    @attrid Int = NULL,
		    @userid Int = NULL,
		    @filename NVarChar(250) = NULL,
		    @filetype VarChar(5) = NULL,
		    @addtime DateTime = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
		    	
		    	update [K_Files] set [filename]=@filename,[filetype]=@filetype,[addtime]=@addtime where attrid=@attrid and userid=@userid
	
				if @@ROWCOUNT=0
				begin
						insert into [K_Files]([attrid],[userid],[filename],[filetype],[addtime])
										values(@attrid,@userid,@filename,@filetype,@addtime)
						SELECT @@IDENTITY
				end
				else
						select id from [K_Files] with(nolock) where attrid=@attrid and userid=@userid
	
			    RETURN
		    END
		    --IF @action=1 BEGIN
				  --  UPDATE [K_Files] SET 
				  --  [attrid]=@attrid,
				  --  [userid]=@userid,
				  --  [filename]=@filename,
				  --  [filetype]=@filetype,
				  --  [addtime]=@addtime
				  --  WHERE id=@id
				  --  SELECT @id
			   -- RETURN
		    --END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Files] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveFileattr]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveFileattr]
		    @id Int = NULL,
		    @filedna VarChar(50) = NULL,
		    @path NVarChar(250) = NULL,
		    @filesiize BigInt = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;

		    INSERT INTO [K_Fileattr]([fileDNA],[path],[fileSiize]) 
						    VALUES (@fileDNA,@path,@fileSiize)
		    SELECT @@IDENTITY
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveErrurl]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveErrurl]
		    @action int,
		    @id Int = NULL,
		    @url NVarChar(256) = NULL,
		    @addtime DateTime = NULL,
		    @agent NVarChar(256) = NULL,
		    @ip NVarChar(50) = NULL,
		    @refer NVarChar(256) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Errurl]([url],[addTime],[agent],[ip],[refer]) 
								    VALUES (@url,@addTime,@agent,@ip,@refer)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Errurl] SET 
				    [url]=@url,
				    [addtime]=@addtime,
				    [agent]=@agent,
				    [ip]=@ip,
				    [refer]=@refer
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Errurl] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveChannelSort]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveChannelSort]
		    @id Int,
		    @sort int
		AS
		BEGIN
		    SET NOCOUNT ON;
			UPDATE [k_Channel] set sort=@sort where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveChannel]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveChannel]
		    @action int,
		    @id Int = NULL,
		    @channelname NVarChar(50) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @sort Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    --IF EXISTS(select 0 From [K_Channel] WITH(NOLOCK) WHERE id<>@id and [fileName]=@filename)
				--return -1
		    
		    IF @action=0 BEGIN
				    INSERT INTO [K_Channel]([channelName],[pageTitle],[sort]) 
								    VALUES (@channelName,@pageTitle,@sort)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Channel] SET 
				    [channelname]=@channelname,
				    [pagetitle]=@pagetitle,
				    [sort]=@sort
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [K_Channel] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveBowen]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveBowen]
		    @action int,
		    @id Int = NULL,
		    @speid Int = NULL,
		    @channelid Int = NULL,
		    @userid Int = NULL,
		    @title NVarChar(150) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @intro NVarChar(256) = NULL,
		    @content NText = NULL,
		    @addtime DateTime = NULL,
		    @btime datetime=null,
		    @state TinyInt = NULL,
		    @hits Int = NULL,
		    @url NVarChar(256) = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Bowen]([speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url])
								    VALUES (@speId,@channelid,@userId,@title,@pageTitle,@intro,@Content,@addTime,@btime,@state,@hits,@url) 
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Bowen] SET 
				    [speid]=@speid,
				    [channelid]=@channelid,
				    [userid]=@userid,
				    [title]=@title,
				    [pagetitle]=@pagetitle,
				    [intro]=@intro,
				    [content]=@content,
				    [addtime]=@addtime,
				    [bTime]=@btime,
				    [state]=@state,
				    [hits]=@hits,
				    [url]=@url
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Bowen] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskxsf]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskxsf]
		    @id Int,
		    @xsf int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_Ask set xsf=@xsf where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskwmy]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskwmy]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_Ask set [state]=3 where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskContentState]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskContentState]
		    @id int
		AS
		BEGIN
		    SET NOCOUNT ON;
			update K_AskContent set state=1 where state=0 and id=@id
			if @@ROWCOUNT=0
			update K_AskContent set state=0 where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskcontent]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskcontent]
		    @action int,
		    @id Int = NULL,
		    @askid Int = NULL,
		    @userid Int = NULL,
		    @content NText = NULL,
		    @addtime DateTime = NULL,
		    @isbest bit=null,
		    @isnm bit=null,
		    @state bit=null
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Askcontent]([askId],[userId],[content],[addTime],[isbest],[isNM],[state]) 
								    VALUES (@askId,@userId,@content,@addTime,@isbest,@isnm,@state)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Askcontent] SET 
				    [askid]=@askid,
				    [userid]=@userid,
				    [content]=@content,
				    [addtime]=@addtime,
				    [isbest]=@isbest,
				    [isNM]=@isnm,
				    [state]=@state
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Askcontent] 
				    WHERE id=@id
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskCn]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskCn]
		    @askId Int,
		    @ansId int,
		    @cnjf int,
		    @accjf int,
		    @star tinyint
		AS
		BEGIN
		    SET NOCOUNT ON;
			declare @askUserId int, @ansUserId int
			select @askUserId=userId from K_Ask with(nolock) where id=@askId
		    select @ansUserId=userId from K_AskContent with(nolock) where id=@ansId
		    
		    update K_Ask set [state]=2 where id=@askId
		    
		    update K_AskContent set isBest=1,star=@star where id=@ansId
		    
		    update K_User set score=score+@accjf where id=@askUserId
		    update K_User set score=score+@cnjf where id=@ansUserId
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskclass]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskclass]
		    @action int,
		    @id Int = NULL,
		    @name NVarChar(50) = NULL,
		    @title NVarChar(150) = NULL,
		    @parent Int = NULL,
		    @parentstr VarChar(500) = NULL,
		    @pai Int = NULL,
		    @depth Int = NULL
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [K_Askclass]([id],[name],[title],[parent],[parentstr],[pai],[depth]) 
								    VALUES (@id,@name,@title,@parent,@parentstr,@pai,@depth)
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [K_Askclass] SET 
				    [name]=@name,
				    [title]=@title
				    --[parent]=@parent,
				    --[parentstr]=@parentstr,
				    --[pai]=@pai,
				    --[depth]=@depth
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				declare @parstr varchar(1000)
				select @parstr=parentstr from K_AskClass with(nolock) where id=@id
				
				    DELETE FROM [K_Askclass] 
				    WHERE parentstr like ''+@parstr+'%'
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAskadded]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAskadded]
		    @id Int,
		    @added nvarchar(1000)
		AS
		BEGIN
		    SET NOCOUNT ON;
		    update K_Ask set added=@added where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_SaveAsk]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_SaveAsk]
		    @action int,
		    @id Int = NULL,
		    @speid Int = NULL,
		    @channelid Int = NULL,
		    @userid Int = NULL,
		    @classid int=null,
		    @title NVarChar(150) = NULL,
		    @pagetitle NVarChar(150) = NULL,
		    @content Ntext=null,
		    @added nvarchar(1000)=null,
		    @addtime DateTime = NULL,
		    @btime datetime=null,
		    @xsf int=null,
		    @state TinyInt = NULL,
		    @hits Int = NULL,
		    @url NVarChar(256) = NULL,
		    @isnm bit=null
		AS
		BEGIN
		    SET NOCOUNT ON;
		    IF @action=0 BEGIN
				    INSERT INTO [k_Ask]([speId],[channelid],[userId],[classid],[title],[pageTitle],[content],[added],[addTime],[bTime],[xsf],[state],[hits],[url],[isnm]) 
								    VALUES (@speId,@channelid,@userId,@classid,@title,@pageTitle,@content,@added,@addTime,@btime,@xsf,@state,@hits,@url,@isnm)
				    SELECT @@IDENTITY
			    RETURN
		    END
		    IF @action=1 BEGIN
				    UPDATE [k_Ask] SET 
				    [speid]=@speid,
				    [channelid]=@channelid,
				    [userid]=@userid,
				    [classid]=@classid,
				    [title]=@title,
				    [pagetitle]=@pagetitle,
				    [content]=@content,
				    [added]=@added,
				    [addtime]=@addtime,
				    [bTime]=@btime,
				    [xsf]=@xsf,
				    [state]=@state,
				    [hits]=@hits,
				    [url]=@url,
				    [isNM]=@isnm
				    WHERE id=@id
				    SELECT @id
			    RETURN
		    END
		    IF @action=2 BEGIN
				    DELETE FROM [k_Ask] 
				    WHERE id=@id
				    
			    RETURN
		    END
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideoSameTitle]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideoSameTitle]
	@title nvarchar(150)
AS
BEGIN
	SET NOCOUNT ON;

	if EXISTS(select 0 From [K_Video] WITH(NOLOCK) WHERE title=@title)
	return -1
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideoPN]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideoPN]
		@id int,
		@prevId int output,
		@nextId int output
		AS
		BEGIN
		    SET NOCOUNT ON;

			set @prevId=0
			set @nextId=0
			
		    SELECT top 1 @prevId=[id] from K_Video with(nolock) where id<@id order by id desc
		    if @prevId=0
				select @prevId=MAX(id) from K_Video with(nolock)
				
			Select Top 1 @nextId=[id] from K_Video with(nolock) where id>@id order by id asc
			if	@nextId=0
				select @nextId=MIN(id) from K_Video with(nolock)
		    
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideoPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideoPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Video',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Video] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideoFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideoFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[picId] from K_Video with(nolock) where id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideo0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideo0]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;

		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[addTime],[bTime],[state],[hits],[vLink],[picId],[trueLink] From [k_Video] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetVideo]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetVideo]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    UPDATE K_Video SET hits=hits+1 WHERE state=1 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;

		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[addTime],[bTime],[state],[hits],[vLink],[picId],[trueLink] From [k_Video] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserscorePage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserscorePage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='score desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Userscore',--表名
				    N'score',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Userscore] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserranktypePage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserranktypePage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Userranktype',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Userranktype] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserranktype]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserranktype]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[rankTypeName],[isDefault] From [K_Userranktype] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserrankPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserrankPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Userrank',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Userrank] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserRankName]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserRankName] 
	@userId int,
	@username nvarchar(50)=null output,
	@rankname nvarchar(50) output,
	@userPicId int=null output
AS
BEGIN
	SET NOCOUNT ON;
			declare @rankTypeId int,@score int,@rank int
			set @rankTypeId=0
			set @score=0
			set @rank=0
			
		    select @username=username,@score=score,@userPicId=picid, @rankTypeId=rankTypeId from K_User with(nolock) where id=@userId
		    if @username='' or @username is null begin
				set @username=''
				set @userPicId=0
				select @rankname=[rankName] from K_UserRank a with(nolock) inner join K_UserRankType b with(nolock) on a.rankTypeId=b.id where a.rank=1 and b.isDefault=1
				return
			end
		    select top 1 @rank=[rank] from K_UserScore with(nolock) where score<@score order by [score] desc
		    if @rank=0
				set @rank=1
				
			if @rankTypeId=0
				select @rankname=[rankName] from K_UserRank a with(nolock) inner join K_UserRankType b with(nolock) on a.rankTypeId=b.id where a.rank=@rank and b.isDefault=1
			else
				select @rankname=[rankName] from K_UserRank a with(nolock) inner join K_UserRankType b with(nolock) on a.rankTypeId=b.id where a.rank=@rank and b.id=@rankTypeId
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserrank]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserrank]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[rankName],[rank],[rankTypeId] From [K_Userrank] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_User',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_User] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserInfoByPass]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserInfoByPass]
	@username nvarchar(50),
	@userpass varchar(12)
AS
BEGIN
	SET NOCOUNT ON;
	
	select [id],[userName],[userpass],[sex],[email],[qq],[isAdmin],[score],[addTime],[lastTime],[lastIp],[prevTime],[prevIp],[state],[picid],[ranktypeid],[isOpen],[skinPath] from [K_User] with(nolock) where username=@username and userpass=@userpass
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserCenter]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserCenter]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[userName],[userpass],[sex],[email],[qq],[isAdmin],[score],[addTime],[lastTime],[lastIp],[prevTime],[prevIp],[state],[picId],[rankTypeId],[isopen],[skinPath] From [K_User] WITH(NOLOCK) WHERE id=@id
		    
		    declare @userScore int,@userRankType int
		    select @userScore=score,@userRankType=rankTypeId From [K_User] WITH(NOLOCK) WHERE id=@id
		    
		    --等级方案
		    if @userRankType=0
		    select rankTypeName from K_UserRankType with(nolock) where isDefault=1
		    else
		    select rankTypeName from K_UserRankType with(nolock) where id=@userRankType
		    
		    --等级
		    declare @rank int
		    set @rank=0
		    select top 1 @rank=[rank] from K_UserScore with(nolock) where score<@userScore order by [score] desc
		    if @rank=0
				set @rank=1
		    select @rank
		    
		    --等级名称
		    if @userRankType=0
				select [rankName] from K_UserRank a with(nolock) inner join K_UserRankType b with(nolock) on a.rankTypeId=b.id where a.rank=@rank and b.isDefault=1
			else
				select [rankName] from K_UserRank a with(nolock) inner join K_UserRankType b with(nolock) on a.rankTypeId=b.id where a.rank=@rank and b.id=@userRankType
		    
		    --来访记录
		    select a.userName,b.visitId,b.addTime from K_User a with(nolock) inner join K_Visiter b with(nolock) on a.id=b.visitId where userId=@id order by b.addTime desc
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserByName]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserByName]
		    @username nvarchar(50)
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT id,[userName],[userpass],[email] From [K_User] WITH(NOLOCK) WHERE userName=@username
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUserAskCount]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUserAskCount]
	@userId int,
	@askCount int output,
	@ansCount int output
AS
BEGIN
	SET NOCOUNT ON;
	
	select @askCount=count(0) from K_Ask with(nolock) where userId=@userId
	select @ansCount=count(0) from K_AskContent with(nolock) where userId=@userId
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetUser]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetUser]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[userName],[userpass],[sex],[email],[qq],[isAdmin],[score],[addTime],[lastTime],[lastIp],[prevTime],[prevIp],[state],[picId],[rankTypeId] From [K_User] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTaskPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTaskPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Task',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Task] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTask]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTask]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[taskName],[intro],[spanSecond],[state],[startHour],[endHour],[lastTime] From [K_Task] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTagPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTagPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Tag',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Tag] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTagList]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTagList]
		    @subId Int,
		    @subType int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT distinct a.id,[tagName] From [k_Tag] a with(nolock) inner join K_TagSub b with(nolock) on a.id=b.tagId where subType=@subType and subId=@subId
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetTag]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetTag]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[tagName],[addTime],[state],[hits],[fileName] From [k_Tag] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpeGather]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpeGather]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
			if @id>0
		    SELECT [id],[channelId],[speName],[upTime],[state],[picId],newsLastTime ,bowenLastTime,askLastTime,picLastTime,videoLastTime,intro,askClassId From [K_Special] WITH(NOLOCK) WHERE id=@id
			else
			 SELECT top 1 [id],[channelId],[speName],[upTime],[state],[picId],newsLastTime ,bowenLastTime,askLastTime,picLastTime,videoLastTime,intro,askClassId From [K_Special] WITH(NOLOCK) WHERE state=1 order by upTime asc
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpecialPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpecialPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Special',--表名
				    N'id',--主键字段
				    N'[id],[channelId],[speName],[pageTitle],[upTime],[state],[hits],[picId],[intro],[askclassid]',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Special] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpecialFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpecialFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[speName],[picId] from K_Special with(nolock) where id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpecial0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpecial0]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    SELECT [id],[channelId],[speName],[pageTitle],[upTime],[state],[hits],[picId],[intro],askClassId From [K_Special] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetSpecial]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetSpecial]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    UPDATE K_Special SET hits=hits+1 WHERE state=1 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;
		
		    SELECT [id],[channelId],[speName],[pageTitle],[upTime],[state],[hits],[picId],[intro],askClassId From [K_Special] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPicSameTitle]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPicSameTitle]
	@title nvarchar(150)
AS
BEGIN
	SET NOCOUNT ON;

	if EXISTS(select 0 From [K_Pic] WITH(NOLOCK) WHERE title=@title)
	return -1
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPicPN]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPicPN]
		@id int,
		@prevId int output,
		@nextId int output
		AS
		BEGIN
		    SET NOCOUNT ON;

			set @prevId=0
			set @nextId=0
			
		    SELECT top 1 @prevId=[id] from K_Pic with(nolock) where id<@id order by id desc
		    if @prevId=0
				select @prevId=MAX(id) from K_Pic with(nolock)
				
			Select Top 1 @nextId=[id] from K_Pic with(nolock) where id>@id order by id asc
			if	@nextId=0
				select @nextId=MIN(id) from K_Pic with(nolock)
		    
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPicPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPicPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Pic',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Pic] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPicFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPicFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[picId] from K_Pic with(nolock) where id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPic0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPic0]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[addtime],[bTime],[state],[hits],[bigUrl],[picid] From [k_Pic] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetPic]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetPic]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
			
			UPDATE K_Pic SET hits=hits+1 WHERE state=1 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;
		
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[addtime],[bTime],[state],[hits],[bigUrl],[picid] From [k_Pic] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowenSameTitle]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowenSameTitle]
	@title nvarchar(150)
AS
BEGIN
	SET NOCOUNT ON;

	if EXISTS(select 0 From [K_Bowen] WITH(NOLOCK) WHERE title=@title)
	return -1
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowenPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowenPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Bowen',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Bowen] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowenFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowenFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[content] from K_Bowen with(nolock) where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowen0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowen0]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url] From [k_Bowen] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetBowen]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetBowen]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    UPDATE K_Bowen SET hits=hits+1 WHERE state=1 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;
		
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url] From [k_Bowen] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNewsSameTitle]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNewsSameTitle]
	@title nvarchar(150)
AS
BEGIN
	SET NOCOUNT ON;

	if EXISTS(select 0 From [K_News] with(nolock) WHERE title=@title)
	return -1
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNewsPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNewsPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_News',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_News] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNewsFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNewsFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[content] from K_News with(nolock) where id=@id

		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNews0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNews0]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url] From [k_News] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetNews]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetNews]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
		    UPDATE K_News SET hits=hits+1 WHERE state=1 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;
		
		    SELECT [id],[speId],[channelid],[userId],[title],[pageTitle],[intro],[Content],[addTime],[bTime],[state],[hits],[url] From [k_News] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetMessagePage0]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetMessagePage0]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Message',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Message] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetMessagePage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetMessagePage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @uid int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    
			declare @where NVarChar(4000)
			set @where=N'id not in(select postId from K_Message a with(nolock) inner join K_MessageDel b with(nolock) on a.id=b.postId where userId='+cast(@uid as nvarchar)+') and (tId='+cast(@uid as nvarchar)+' or tId=0)'
 
		   
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Message',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    'tid asc,isread asc,id desc',--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
		 
exec('	 	
		 	DECLARE @varTable table(
				indexID int identity(1,1), 
				[id] int,
				[title] nvarchar(100),
				[content] nvarchar(256),
				[addTime] datetime,
				fid int,
				tid int,
				isread bit,
				fname nvarchar(50)
			)
			
			INSERT INTO @varTable ([id],[title],[content],[addTime],fid,tid,isread) '+@SQLString+' 

			DECLARE @Count int,@I int;
			SELECT @Count = COUNT(0) FROM @varTable;
		 	
		 	SET @I = 1;
		 	WHILE(@I < @Count+1) BEGIN
				DECLARE @fid int
				SELECT @fid=fid FROM @varTable WHERE indexID = @I;
				
				declare @user_name nvarchar(50)
				set @user_name=''''
				select @user_name=userName from K_User with(nolock) where id=@fid
				
				update @varTable set fname=@user_name where indexID=@I
			
			SET @I = @I + 1;
			End

		    select * from @varTable
') 		    
		
			DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Message] WITH (NOLOCK) where '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		    
		    select COUNT(0) from K_Message with(nolock) where isRead=0 and tId=@uid
		    
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetMessage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetMessage]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[content],[addTime],[fId],[tId],[isRead] From [K_Message] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetFilesPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetFilesPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Files',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Files] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetFilesInfo]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetFilesInfo]	
	@id int
AS
BEGIN
	SET NOCOUNT ON;

    select [attrId],[filename],[filetype],[path] from K_Files a with(nolock) inner join K_FileAttr b with(nolock) on a.attrid=b.id where a.id=@id
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetFiles]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetFiles]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[attrId],[userId],[fileName],[fileType],[addTime] From [K_Files] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetFileattrSameFileDNA]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetFileattrSameFileDNA]
	@fileDNA varchar(50)
AS
BEGIN
	SET NOCOUNT ON;
	select [id],[path] from [K_FileAttr] where filedna=@filedna
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetErrurlPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetErrurlPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Errurl',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Errurl] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetCount]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetCount]
		    @from datetime,
		    @to datetime
		AS
		BEGIN
		    SET NOCOUNT ON;
		    select COUNT(0) from K_News with(nolock) where addTime>=@from and addTime<@to
		    select COUNT(0) from K_Bowen with(nolock) where addTime>=@from and addTime<@to
		    select COUNT(0) from K_Ask with(nolock) where addTime>=@from and addTime<@to
		    select COUNT(0) from K_Pic with(nolock) where addTime>=@from and addTime<@to
		    select COUNT(0) from K_Video with(nolock) where addTime>=@from and addTime<@to
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetChannelPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetChannelPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Channel',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Channel] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetChannel]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetChannel]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[channelName],[pageTitle],[sort] From [K_Channel] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_ChkUser]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_ChkUser]
	@username nvarchar(50)=null,
	@email nvarchar(150)=null
AS
BEGIN
	SET NOCOUNT ON;
	if @username is not null begin
		if exists(select 0 from K_User with(nolock) where [username]=@username)
		return -1
	end
	
	if @email is not null begin
		if exists(select 0 from K_User with(nolock) where email=@email)
		return -2
	end
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskPageList]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskPageList]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N'',
		    @classId int
		AS
		BEGIN
		    SET NOCOUNT ON;
		    --子类
		    select id,name from K_AskClass with(nolock) where parent=@classId

			--记录数
			declare @wheresql nvarchar(4000)
			set @wheresql='classid in(SELECT [id] FROM K_AskClass WITH (NOLOCK) WHERE parentstr LIKE (SELECT [parentstr] FROM K_AskClass WITH (NOLOCK) WHERE [id]='+CAST(@classid as nvarchar)+')+''%'')'
		    IF(@where<>N'' AND @where IS NOT NULL)
			    SET @where= @where +' and '+@wheresql
			else
			    SET @where= @wheresql
			
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Ask] WITH (NOLOCK) where '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount

			--取得主数据
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'

		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Ask',--表名
				    N'id',--主键字段
				    N'[id],[userId],[classid],[title],[addTime],[xsf],[isNM]',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT

exec('
			DECLARE @varTable table(
				indexID int identity(1,1), 
				[id] int,
				[userId] int,
				[classid] int,
				[title] nvarchar(150),
				[addTime] datetime,
				[xsf] int,
				[isNM] bit,
				userName nvarchar(50),
				className nvarchar(50),
				ansnum int
			);
			
			INSERT INTO @varTable ([id],[userId],[classid],[title],[addTime],[xsf],[isNM]) '+@SQLString+'
		
			DECLARE @Count int,@I int;
			SELECT @Count = COUNT(0) FROM @varTable;
			
			SET @I = 1;
			WHILE(@I < @Count+1) BEGIN
				DECLARE @Aid int,@UID int,@CID int
				SELECT @Aid=id,@UID=userId,@CID=classid FROM @varTable WHERE indexID = @I;
				
				declare @user_name nvarchar(50)
				select @user_name=userName from K_User with(nolock) where id=@UID
				
				declare @class_name nvarchar(50)
				select @class_name=name from K_AskClass with(nolock) where id=@CID
				
				update @varTable set userName=@user_name,className=@class_name where indexID=@I
				
				declare @ansnum int
				select @ansnum=count(0) from k_askcontent with(nolock) where askid=@Aid
				update @varTable set ansnum=@ansnum where indexID=@I
			
			SET @I = @I + 1;
			End
			
			select * from @varTable
')
			
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Ask',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Ask] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskNum]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskNum]	
		AS
		BEGIN
		    SET NOCOUNT ON;
			select COUNT(0) from K_Ask
			select COUNT(0) from K_AskContent
			select COUNT(0) from K_Ask where state=2
			select COUNT(0) from K_Ask where state<2
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskFullIndex]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskFullIndex]
		@id int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[title],[content] from K_Ask with(nolock) where id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskcontentPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskcontentPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'k_Askcontent',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [k_Askcontent] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskcontentId]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskcontentId]	
		    @askId int
		AS
		BEGIN
		    select id from K_AskContent with(nolock) where askId=@askId
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskcontent]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskcontent]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[askId],[userId],[content],[addTime],[isbest],[isNM],[state] From [k_Askcontent] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskClassRndId]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskClassRndId]
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT top 1 [id] From [K_AskClass] WITH(NOLOCK) order by NEWID()
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskClassParentNav]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- cateNav
CREATE PROCEDURE [dbo].[Kcms_GetAskClassParentNav]
	@curId int=0
AS
BEGIN
	SET NOCOUNT ON;

	declare @parentstr varchar(200)
	select @parentstr=parentstr from K_AskClass where id=@curId
	
	declare @strSQL nvarchar(1000)
	set @strSQL=' select id, name from K_AskClass where id in(0'+ @parentstr +'0) order by id desc'
	exec sp_executesql @strSQL
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskclassPage]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskclassPage]	
		    @PageIndex Int,
		    @PageSize Int,
		    @where NVarChar(4000)=N'',
		    @Sort NVarChar(255)=N''
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    IF @Sort=N'' OR @Sort IS NULL
		    SET @Sort='id desc'
		
		    --取得主数据
		    DECLARE @SQLString Nvarchar(4000)
		    EXECUTE Kcms_Common_GetRecordsByPageSQLString
				    N'K_Askclass',--表名
				    N'id',--主键字段
				    N'*',--返回的字段
				    @where,--查询条件
				    @Sort,--排序
				    @PageSize,--页大小
				    @PageIndex,--当前页
				    @SQLString OUTPUT
				
		    EXECUTE(@SQLString)
		
		    IF(@where<>N'' AND @where IS NOT NULL)
		    SET @where=' where '+@where
		
		    DECLARE @SqlStringCount nvarchar(4000),@TotalCount INT
		    SET @SqlStringCount='SELECT @TotalCount=COUNT(0) FROM [K_Askclass] WITH (NOLOCK) '+@where
		    EXECUTE sp_executesql @SqlStringCount,N'@TotalCount int output',@TotalCount output
		    select @TotalCount
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskClassJson]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskClassJson]
	@parentId int=0
AS
BEGIN
	SET NOCOUNT ON;
	select [id],[name],(select count(id) from [K_AskClass] with(nolock) where parent=a.id) as childs from [K_AskClass] a with(nolock) where parent=@parentId order by pai asc
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskclass]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskclass]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[name],[title],[parent],[parentstr],[pai],[depth] From [K_Askclass] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAsk]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAsk]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT [id],[speId],[channelid],[userId],[classid],[title],[pageTitle],[content],[added],[addTime],[bTime],[xsf],[state],[hits],[url],[isNM] From [k_Ask] WITH(NOLOCK) WHERE id=@id
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskSend]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskSend]
		    @askid Int
		AS
		BEGIN
		    SET NOCOUNT ON;
		
		    SELECT userName,email from K_User a with(nolock) inner join K_Ask b with(nolock) on a.id=b.userId where b.id=@askid
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskSameTitle]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskSameTitle]
	@title nvarchar(150)
AS
BEGIN
	SET NOCOUNT ON;

	if EXISTS(select 0 From [K_Ask] WITH(NOLOCK) WHERE title=@title)
	return -1
END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskRele]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskRele]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;
			select top 6 id,title,addTime from K_Ask with(nolock) where state>0 and id<>@id and id in(select subId from K_TagSub with(nolock) where subType=3 and tagId in(select tagId from K_TagSub with(nolock) where subType=3 and subId=@id)) order by NEWID()
		
		END
GO
/****** Object:  StoredProcedure [dbo].[Kcms_GetAskPer]    Script Date: 08/04/2011 07:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Kcms_GetAskPer]
		    @id Int
		AS
		BEGIN
		    SET NOCOUNT ON;

		    UPDATE K_Ask SET  hits=hits+1 WHERE state>0 and id=@id
		    if @@ROWCOUNT=0
		    RETURN -1;
		    
			declare @userId int,@rankTypeId int,@score int,@rank int
		    declare @username nvarchar(50),@rankname nvarchar(50),@userpicid int
		    
		    declare @varTable table(
				indexID int identity(1,1),
				id int,
				userid int,
				title nvarchar(150),
				pageTitle nvarchar(150),
				content ntext,
				added nvarchar(1000),
				addtime datetime,
				xsf int,
				[state] tinyint,
				hits int,
				url nvarchar(256),
				isnm bit,
				username nvarchar(50),
				rankname nvarchar(50),
				userpicid int
		    )
		    
		    insert into @varTable (id,userid,title,pageTitle,content,added,addtime,xsf,[state],hits,url,isnm) 
					SELECT [id],[userId],[title],[pageTitle],[content],[added],[addTime],[xsf],[state],[hits],[url],[isnm] From [k_Ask] WITH(NOLOCK) WHERE id=@id
			
			select @userId=userid from @varTable
			exec Kcms_GetUserRankName @userId,@username output,@rankname output,@userpicid output
		    update @varTable set username=@username,rankname=@rankname,userpicid=@userpicid
		    
		    select * from @varTable
		    
		    --best
		    declare @varTableAnsBest table(
				indexID int identity(1,1),
				id int,
				askid int,
				userid int,
				content ntext,
				addtime datetime,
				isnm bit,
				username nvarchar(50),
				rankname nvarchar(50),
				userpicid int
		    )
		    
		    insert into @varTableAnsBest(id,askid,userid,content,addtime,isnm) select top 1 [id],[askId],[userId],[content],[addTime],[isNM] from K_AskContent with(nolock) where isBest=1 and askId=@id order by id asc
		    
		    select @userId=userid from @varTableAnsBest
			exec Kcms_GetUserRankName @userId,@username output,@rankname output,@userpicid output
		    update @varTableAnsBest set username=@username,rankname=@rankname ,userpicid=@userpicid
		    
		    select * from @varTableAnsBest
		    
		    --other
		    declare @varTableAns table(
				indexID int identity(1,1),
				id int,
				askid int,
				userid int,
				content ntext,
				addtime datetime,
				isnm bit,
				username nvarchar(50),
				rankname nvarchar(50),
				userpicid int
		    )
		    insert into @varTableAns(id,askid,userid,content,addtime,isnm) select [id],[askId],[userId],[content],[addTime],[isnm] from K_AskContent with(nolock) where isBest=0 and state=1 and askId=@id order by id asc
		  	
		  	DECLARE @Count int,@I int;
			SELECT @Count = COUNT(0) FROM @varTableAns
			SET @I = 1;
			WHILE(@I < @Count+1) BEGIN
				select @userId=userid from @varTableAns where indexID=@I
				exec Kcms_GetUserRankName @userId,@username output,@rankname output,@userpicid output
				update @varTableAns set username=@username,rankname=@rankname,userpicid=@userpicid where indexID=@I
				SET @I = @I + 1;
			END
			
			select * from @varTableAns

		    --path
		    DECLARE @cid int
			SELECT @cId=classId FROM K_Ask WITH(NOLOCK) WHERE id=@id

			DECLARE @parentStr nvarchar(600),@SQL NVARCHAR(600)
			SELECT @parentStr=parentstr FROM K_AskClass WITH(NOLOCK) WHERE id=@cid
			SET @SQL='SELECT [id],[name] FROM K_AskClass WITH(NOLOCK) WHERE id IN(0'+@parentStr+'0)'
			EXEC sp_executesql @SQL
		    
		END
GO
/****** Object:  Default [DF_K_User_picId]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_User] ADD  CONSTRAINT [DF_K_User_picId]  DEFAULT ((0)) FOR [picId]
GO
/****** Object:  Default [DF_K_User_rankTypeId]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_User] ADD  CONSTRAINT [DF_K_User_rankTypeId]  DEFAULT ((0)) FOR [rankTypeId]
GO
/****** Object:  Default [DF_K_User_isOpen]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_User] ADD  CONSTRAINT [DF_K_User_isOpen]  DEFAULT ((0)) FOR [isOpen]
GO
/****** Object:  Default [DF_K_User_skinPath]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_User] ADD  CONSTRAINT [DF_K_User_skinPath]  DEFAULT ('') FOR [skinPath]
GO
/****** Object:  Default [DF_K_User_isActiv]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_User] ADD  CONSTRAINT [DF_K_User_isActiv]  DEFAULT ((0)) FOR [isActiv]
GO
/****** Object:  Default [DF_K_Special_newsLastTime]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_newsLastTime]  DEFAULT ((0)) FOR [newsLastTime]
GO
/****** Object:  Default [DF_K_Special_bowenLastTime]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_bowenLastTime]  DEFAULT ((0)) FOR [bowenLastTime]
GO
/****** Object:  Default [DF_K_Special_askLastTime]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_askLastTime]  DEFAULT ((0)) FOR [askLastTime]
GO
/****** Object:  Default [DF_K_Special_picLastTime]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_picLastTime]  DEFAULT ((0)) FOR [picLastTime]
GO
/****** Object:  Default [DF_K_Special_videoLastTime]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_videoLastTime]  DEFAULT ((0)) FOR [videoLastTime]
GO
/****** Object:  Default [DF_K_Special_askClassId]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_Special] ADD  CONSTRAINT [DF_K_Special_askClassId]  DEFAULT ((0)) FOR [askClassId]
GO
/****** Object:  Default [DF_K_AskContent_star]    Script Date: 08/04/2011 07:09:54 ******/
ALTER TABLE [dbo].[K_AskContent] ADD  CONSTRAINT [DF_K_AskContent_star]  DEFAULT ((0)) FOR [star]
GO
