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

~

CREATE PROCEDURE dnt_createpost
@fid int,
@tid int,
@parentid int,
@layer int,
@poster varchar(20),
@posterid int,
@title nvarchar(60),
@topictitle nvarchar(60),
@postdatetime datetime,
@message ntext,
@ip varchar(15),
@lastedit varchar(50),
@invisible int,
@usesig int,
@htmlon int,
@smileyoff int,
@bbcodeoff int,
@parseurloff int,
@attachment int,
@rate int,
@ratetimes int,
@ishide int

AS


DEClARE @postid int

DELETE FROM [dnt_postid] WHERE DATEDIFF(n, postdatetime, GETDATE()) >5

INSERT INTO [dnt_postid] ([postdatetime]) VALUES(GETDATE())

SELECT @postid=SCOPE_IDENTITY()

INSERT INTO [dnt_posts1]([pid], [fid], [tid], [parentid], [layer], [poster], [posterid], [title], [postdatetime], [message], [ip], [lastedit], [invisible], [usesig], [htmlon], [smileyoff], [bbcodeoff], [parseurloff], [attachment], [rate], [ratetimes]) VALUES(@postid, @fid, @tid, @parentid, @layer, @poster, @posterid, @title, @postdatetime, @message, @ip, @lastedit, @invisible, @usesig, @htmlon, @smileyoff, @bbcodeoff, @parseurloff, @attachment, @rate, @ratetimes)

IF @parentid=0
BEGIN

UPDATE [dnt_posts1] SET [parentid]=@postid WHERE [pid]=@postid
END

IF @@ERROR=0
BEGIN

IF  @invisible = 0
BEGIN

UPDATE [dnt_statistics] SET [totalpost]=[totalpost] + 1



DECLARE @fidlist AS VARCHAR(1000)
DECLARE @strsql AS VARCHAR(4000)

SET @fidlist = '';

SELECT @fidlist = ISNULL([parentidlist],'') FROM [dnt_forums] WHERE [fid] = @fid
IF RTRIM(@fidlist)<>''
BEGIN
SET @fidlist = RTRIM(@fidlist) + ',' + CAST(@fid AS VARCHAR(10))
END
ELSE
BEGIN
SET @fidlist = CAST(@fid AS VARCHAR(10))
END


UPDATE [dnt_forums] SET
[posts]=[posts] + 1,
[todayposts]=CASE
WHEN DATEDIFF(day, [lastpost], GETDATE())=0 THEN [todayposts] + 1
ELSE 1
END,
[lasttid]=@tid,
[lasttitle]=@topictitle,
[lastpost]=@postdatetime,
[lastposter]=@poster,
[lastposterid]=@posterid

WHERE fid IN (SELECT [item] FROM [dnt_split](@fidlist, ','))


UPDATE [dnt_users] SET
[lastpost] = @postdatetime,
[lastpostid] = @postid,
[lastposttitle] = @title,
[posts] = [posts] + 1,
[lastactivity] = GETDATE()
WHERE [uid] = @posterid

IF @layer>0
UPDATE [dnt_topics] SET [replies]=[replies] + 1,[lastposter]=@poster,[lastpost]=@postdatetime,[lastposterid]=@posterid,[lastpostid]=@postid WHERE [tid]=@tid
END

IF @ishide=0 AND EXISTS(SELECT [hide] FROM [dnt_topics] WHERE [tid]=@tid AND [hide]=1)
SET @ishide=1

IF @layer<=0
BEGIN
UPDATE [dnt_topics] SET [replies]=0,[lastposter]=@poster,[lastpost]=@postdatetime,[lastposterid]=@posterid,[lastpostid]=@postid,[hide]=@ishide WHERE [tid]=@tid
END
IF @posterid <> -1
BEGIN
INSERT [dnt_myposts]([uid], [tid], [pid], [dateline]) VALUES(@posterid, @tid, @postid, @postdatetime)
END

END

SELECT @postid AS postid

~

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

~

CREATE PROCEDURE dnt_getfirstpostid
@tid int
AS
SELECT TOP 1 [pid] FROM [dnt_posts1] WHERE [tid]=@tid ORDER BY [pid]

~

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

~

CREATE PROCEDURE dnt_getpostcount
@tid int
AS
SELECT COUNT(pid) FROM [dnt_posts1] WHERE [tid]=@tid AND [invisible]=0 AND layer>0

~


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

~

CREATE  PROCEDURE dnt_deletepostbypid
@pid int,
@chanageposts AS BIT
AS

DECLARE @fid int
DECLARE @tid int
DECLARE @posterid int
DECLARE @lastforumposterid int
DECLARE @layer int
DECLARE @postdatetime smalldatetime
DECLARE @poster varchar(50)
DECLARE @postcount int
DECLARE @title nchar(60)
DECLARE @lasttid int
DECLARE @postid int
DECLARE @todaycount int


SELECT @fid = [fid],@tid = [tid],@posterid = [posterid],@layer = [layer], @postdatetime = [postdatetime] FROM [dnt_posts1] WHERE pid = @pid

DECLARE @fidlist AS VARCHAR(1000)

SET @fidlist = '';

SELECT @fidlist = ISNULL([parentidlist],'') FROM [dnt_forums] WHERE [fid] = @fid
IF RTRIM(@fidlist)<>''
BEGIN
SET @fidlist = RTRIM(@fidlist) + ',' + CAST(@fid AS VARCHAR(10))
END
ELSE
BEGIN
SET @fidlist = CAST(@fid AS VARCHAR(10))
END


IF @layer<>0

BEGIN

IF @chanageposts = 1
BEGIN
UPDATE [dnt_statistics] SET [totalpost]=[totalpost] - 1

UPDATE [dnt_forums] SET
[posts]=[posts] - 1,
[todayposts]=CASE
WHEN DATEPART(yyyy, @postdatetime)=DATEPART(yyyy,GETDATE()) AND DATEPART(mm, @postdatetime)=DATEPART(mm,GETDATE()) AND DATEPART(dd, @postdatetime)=DATEPART(dd,GETDATE()) THEN [todayposts] - 1
ELSE [todayposts]
END
WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +
(SELECT @fidlist AS [fid]) + ',') > 0)

UPDATE [dnt_users] SET [posts] = [posts] - 1 WHERE [uid] = @posterid

UPDATE [dnt_topics] SET [replies]=[replies] - 1 WHERE [tid]=@tid
END

DELETE FROM [dnt_posts1] WHERE [pid]=@pid

END
ELSE
BEGIN

SELECT @postcount = COUNT([pid]) FROM [dnt_posts1] WHERE [tid] = @tid
SELECT @todaycount = COUNT([pid]) FROM [dnt_posts1] WHERE [tid] = @tid AND DATEDIFF(d, [postdatetime], GETDATE()) = 0

IF @chanageposts = 1
BEGIN
UPDATE [dnt_statistics] SET [totaltopic]=[totaltopic] - 1, [totalpost]=[totalpost] - @postcount

UPDATE [dnt_forums] SET [posts]=[posts] - @postcount, [topics]=[topics] - 1,[todayposts]=[todayposts] - @todaycount WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +(SELECT @fidlist AS [fid]) + ',') > 0)

UPDATE [dnt_users] SET [posts] = [posts] - @postcount WHERE [uid] = @posterid

END

DELETE FROM [dnt_posts1] WHERE [tid] = @tid

DELETE FROM [dnt_topics] WHERE [tid] = @tid

END


IF @layer<>0
BEGIN
SELECT TOP 1 @pid = [pid], @posterid = [posterid], @postdatetime = [postdatetime], @title = [title], @poster = [poster] FROM [dnt_posts1] WHERE [tid]=@tid ORDER BY [pid] DESC
UPDATE [dnt_topics] SET [lastposter]=@poster,[lastpost]=@postdatetime,[lastpostid]=@pid,[lastposterid]=@posterid WHERE [tid]=@tid
END



SELECT @lasttid = [lasttid] FROM [dnt_forums] WHERE [fid] = @fid


IF @lasttid = @tid
BEGIN




SELECT TOP 1 @pid = [pid], @tid = [tid],@lastforumposterid = [posterid], @title = [title], @postdatetime = [postdatetime], @poster = [poster] FROM [dnt_posts1] WHERE [fid] = @fid ORDER BY [pid] DESC



UPDATE [dnt_forums] SET

[lastpost]=@postdatetime,
[lastposter]=ISNULL(@poster,''),
[lastposterid]=ISNULL(@lastforumposterid,'0')

WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +
(SELECT @fidlist AS [fid]) + ',') > 0)



SELECT TOP 1 @pid = [pid], @tid = [tid],@posterid = [posterid], @postdatetime = [postdatetime], @title = [title], @poster = [poster] FROM [dnt_posts1] WHERE [posterid]=@posterid ORDER BY [pid] DESC

UPDATE [dnt_users] SET

[lastpost] = @postdatetime,
[lastpostid] = @pid,
[lastposttitle] = ISNULL(@title,'')

WHERE [uid] = @posterid

END


~

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

~

CREATE PROCEDURE dnt_getposttree
@tid int
AS
SELECT [pid], [layer], [title], [poster], [posterid],[postdatetime],[message] FROM [dnt_posts1] WHERE [tid]=@tid AND [invisible]=0 ORDER BY [parentid];


~

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

~

CREATE PROCEDURE dnt_getsinglepost
@tid int,
@pid int
AS
SELECT [aid], [tid], [pid], [postdatetime], [readperm], [filename], [description], [filetype], [filesize], [attachment], [downloads], [attachprice], [width], [height], [uid]  FROM [dnt_attachments] WHERE [tid]=@tid

SELECT TOP 1
[dnt_posts1].[pid],
[dnt_posts1].[fid],
[dnt_posts1].[title],
[dnt_posts1].[layer],
[dnt_posts1].[message],
[dnt_posts1].[ip],
[dnt_posts1].[lastedit],
[dnt_posts1].[postdatetime],
[dnt_posts1].[attachment],
[dnt_posts1].[poster],
[dnt_posts1].[invisible],
[dnt_posts1].[usesig],
[dnt_posts1].[htmlon],
[dnt_posts1].[smileyoff],
[dnt_posts1].[parseurloff],
[dnt_posts1].[bbcodeoff],
[dnt_posts1].[rate],
[dnt_posts1].[ratetimes],
[dnt_posts1].[posterid],
[dnt_users].[nickname],
[dnt_users].[username],
[dnt_users].[groupid],
[dnt_users].[spaceid],
[dnt_users].[gender],
[dnt_users].[bday],
[dnt_users].[email],
[dnt_users].[showemail],
[dnt_users].[digestposts],
[dnt_users].[credits],
[dnt_users].[extcredits1],
[dnt_users].[extcredits2],
[dnt_users].[extcredits3],
[dnt_users].[extcredits4],
[dnt_users].[extcredits5],
[dnt_users].[extcredits6],
[dnt_users].[extcredits7],
[dnt_users].[extcredits8],
[dnt_users].[posts],
[dnt_users].[joindate],
[dnt_users].[onlinestate],
[dnt_users].[lastactivity],
[dnt_users].[oltime],
[dnt_users].[lastvisit],
[dnt_users].[invisible],
[dnt_userfields].[avatar],
[dnt_userfields].[avatarwidth],
[dnt_userfields].[avatarheight],
[dnt_userfields].[medals],
[dnt_userfields].[sightml] AS signature,
[dnt_userfields].[location],
[dnt_userfields].[customstatus],
[dnt_userfields].[website],
[dnt_userfields].[icq],
[dnt_userfields].[qq],
[dnt_userfields].[msn],
[dnt_userfields].[yahoo],
[dnt_userfields].[skype]
FROM [dnt_posts1] LEFT JOIN [dnt_users] ON [dnt_users].[uid]=[dnt_posts1].[posterid] LEFT JOIN [dnt_userfields] ON [dnt_userfields].[uid]=[dnt_users].[uid] WHERE [dnt_posts1].[pid]=@pid


~

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

~

CREATE PROCEDURE dnt_updatepost
@pid int,
@title nvarchar(160),
@message ntext,
@lastedit nvarchar(50),
@invisible int,
@usesig int,
@htmlon int,
@smileyoff int,
@bbcodeoff int,
@parseurloff int
AS
UPDATE dnt_posts1 SET
[title]=@title,
[message]=@message,
[lastedit]=@lastedit,
[invisible]=@invisible,
[usesig]=@usesig,
[htmlon]=@htmlon,
[smileyoff]=@smileyoff,
[bbcodeoff]=@bbcodeoff,

[parseurloff]=@parseurloff WHERE [pid]=@pid

~

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

~

CREATE PROCEDURE [dnt_getpostlist1]
@tid int,
@pagesize int,
@pageindex int,
@invisible int
AS
DECLARE @startRow int,
@endRow int
SET @startRow = (@pageIndex - 1) * @pageSize +1
SET @endRow = @startRow + @pageSize -1

SELECT
[POSTS].[pid],
[POSTS].[fid],
[POSTS].[title],
[POSTS].[layer],
[POSTS].[message],
[POSTS].[ip],
[POSTS].[lastedit],
[POSTS].[postdatetime],
[POSTS].[attachment],
[POSTS].[poster],
[POSTS].[posterid],
[POSTS].[invisible],
[POSTS].[usesig],
[POSTS].[htmlon],
[POSTS].[smileyoff],
[POSTS].[parseurloff],
[POSTS].[bbcodeoff],
[POSTS].[rate],
[POSTS].[ratetimes],
[POSTS].[nickname],
[POSTS].[username],
[POSTS].[groupid],
[POSTS].[spaceid],
[POSTS].[gender],
[POSTS].[bday],
[POSTS].[email],
[POSTS].[showemail],
[POSTS].[digestposts],
[POSTS].[credits],
[POSTS].[extcredits1],
[POSTS].[extcredits2],
[POSTS].[extcredits3],
[POSTS].[extcredits4],
[POSTS].[extcredits5],
[POSTS].[extcredits6],
[POSTS].[extcredits7],
[POSTS].[extcredits8],
[POSTS].[posts],
[POSTS].[joindate],
[POSTS].[onlinestate],
[POSTS].[lastactivity],
[POSTS].[userinvisible],
[POSTS].[avatar],
[POSTS].[avatarwidth],
[POSTS].[avatarheight],
[POSTS].[medals],
[POSTS].[signature],
[POSTS].[location],
[POSTS].[customstatus],
[POSTS].[website],
[POSTS].[icq],
[POSTS].[qq],
[POSTS].[msn],
[POSTS].[yahoo],
[POSTS].[oltime],
[POSTS].[lastvisit],
[POSTS].[skype]
FROM (SELECT ROW_NUMBER() OVER(ORDER BY pid)AS ROWID,
[dnt_posts1].[pid],
[dnt_posts1].[fid],
[dnt_posts1].[title],
[dnt_posts1].[layer],
[dnt_posts1].[message],
[dnt_posts1].[ip],
[dnt_posts1].[lastedit],
[dnt_posts1].[postdatetime],
[dnt_posts1].[attachment],
[dnt_posts1].[poster],
[dnt_posts1].[posterid],
[dnt_posts1].[invisible],
[dnt_posts1].[usesig],
[dnt_posts1].[htmlon],
[dnt_posts1].[smileyoff],
[dnt_posts1].[parseurloff],
[dnt_posts1].[bbcodeoff],
[dnt_posts1].[rate],
[dnt_posts1].[ratetimes],
[dnt_users].[nickname],
[dnt_users].[username],
[dnt_users].[groupid],
[dnt_users].[spaceid],
[dnt_users].[gender],
[dnt_users].[bday],
[dnt_users].[email],
[dnt_users].[showemail],
[dnt_users].[digestposts],
[dnt_users].[credits],
[dnt_users].[extcredits1],
[dnt_users].[extcredits2],
[dnt_users].[extcredits3],
[dnt_users].[extcredits4],
[dnt_users].[extcredits5],
[dnt_users].[extcredits6],
[dnt_users].[extcredits7],
[dnt_users].[extcredits8],
[dnt_users].[posts],
[dnt_users].[joindate],
[dnt_users].[onlinestate],
[dnt_users].[lastactivity],
[dnt_users].[oltime],
[dnt_users].[lastvisit],
[dnt_users].[invisible] AS [userinvisible],
[dnt_userfields].[avatar],
[dnt_userfields].[avatarwidth],
[dnt_userfields].[avatarheight],
[dnt_userfields].[medals],
[dnt_userfields].[sightml] AS [signature],
[dnt_userfields].[location],
[dnt_userfields].[customstatus],
[dnt_userfields].[website],
[dnt_userfields].[icq],
[dnt_userfields].[qq],
[dnt_userfields].[msn],
[dnt_userfields].[yahoo],
[dnt_userfields].[skype]
FROM [dnt_posts1] LEFT JOIN [dnt_users] ON [dnt_users].[uid]=[dnt_posts1].[posterid] LEFT JOIN [dnt_userfields] ON [dnt_userfields].[uid]=[dnt_users].[uid]
WHERE [tid] = @tid AND [dnt_posts1].[invisible] <=@invisible) AS POSTS
WHERE ROWID BETWEEN @startRow AND @endRow
~

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

~

CREATE PROCEDURE [dnt_deletetopicbytidlist1]
@tidlist AS VARCHAR(2000),
@chanageposts AS BIT
AS

DECLARE @postcount int
DECLARE @topiccount int
DECLARE @todaycount int
DECLARE @sqlstr nvarchar(4000)
DECLARE @fid varchar(2000)
DECLARE @posterid varchar(200)
DECLARE @tempFid int
DECLARE @tempPosterid int
DECLARE @tempLayer int
DECLARE @temppostdatetime datetime

DECLARE @tempfidlist AS VARCHAR(1000)

SET @fid = ''
SET @posterid = ''
SET @postcount=0
SET @topiccount=0
SET @todaycount=0

SET @tempfidlist = '';


IF @tidlist<>''
BEGIN
EXEC('DECLARE cu_dnt_posts CURSOR FOR SELECT [fid],[posterid],[layer],[postdatetime] FROM [dnt_posts1] WHERE [dnt_posts1].[tid] IN ('+@tidlist+')')

OPEN cu_dnt_posts
FETCH NEXT FROM cu_dnt_posts into @tempFid,@tempPosterid,@tempLayer,@temppostdatetime
WHILE @@FETCH_STATUS = 0
BEGIN
SET @postcount = @postcount + 1
IF @tempLayer = 0
BEGIN
SET @topiccount = @topiccount + 1

END

IF DATEDIFF(d,@temppostdatetime,GETDATE()) = 0
BEGIN
SET @todaycount = @todaycount + 1
END


IF CHARINDEX(',' + LTRIM(STR(@tempFid)) + ',',@fid + ',') = 0
BEGIN
--SET @fid = @fid + ',' + LTRIM(STR(@tempFid))
SELECT @tempfidlist = ISNULL([parentidlist],'') FROM [dnt_forums] WHERE [fid] = @tempFid
IF RTRIM(@tempfidlist)<>''
BEGIN
SET @fid = RTRIM(@fid) + ',' +  RTRIM(@tempfidlist) + ',' + CAST(@tempFid AS VARCHAR(10))
END
ELSE
BEGIN
SET @fid =RTRIM(@fid) + ',' +  CAST(@tempFid AS VARCHAR(10))
END


END
IF @chanageposts = 1
BEGIN
UPDATE [dnt_users] SET [posts] = [posts] - 1 WHERE [uid] = @tempPosterid
END

FETCH NEXT FROM cu_dnt_posts into @tempFid,@tempPosterid,@tempLayer,@temppostdatetime
END

CLOSE cu_dnt_posts
DEALLOCATE cu_dnt_posts


IF LEN(@fid)>0
BEGIN


SET @fid = SUBSTRING(@fid,2,LEN(@fid)-1)

IF @chanageposts = 1
BEGIN

UPDATE [dnt_statistics] SET [totaltopic]=[totaltopic] - @topiccount, [totalpost]=[totalpost] - @postcount

EXEC('UPDATE [dnt_forums]
SET [posts]=[posts] - ' + @postcount + ',
[topics]=[topics] - ' + @topiccount + ',
[todayposts] = [todayposts] - ' + @todaycount + '
WHERE [fid] IN ('+@fid+')')
END

EXEC('DELETE FROM [dnt_favorites] WHERE [tid] IN ('+@tidlist+') AND [typeid]=0')

EXEC('DELETE FROM [dnt_polls] WHERE [tid] IN ('+@tidlist+')')

EXEC('DELETE FROM [dnt_posts1] WHERE [tid] IN ('+@tidlist+')')

EXEC('DELETE FROM [dnt_mytopics] WHERE [tid] IN ('+@tidlist+')')
END
EXEC('DELETE FROM [dnt_topics] WHERE [closed] IN ('+@tidlist+') OR [tid] IN ('+@tidlist+')')

EXEC('UPDATE [dnt_tags] SET [count]=[count]-1, [fcount]=[fcount]-1 WHERE [tagid] IN (SELECT [tagid] FROM [dnt_topictags] WHERE [tid] IN ('+@tidlist+'))')

EXEC('DELETE FROM [dnt_topictags] WHERE [tid] IN ('+@tidlist+')')

EXEC('DELETE FROM [dnt_topictagcaches] WHERE [tid] IN ('+@tidlist+') OR [linktid] IN ('+@tidlist+')')
END
~

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

~

CREATE PROCEDURE [dnt_getreplypid1]
@uid int,
@tid int

AS
SELECT TOP 1 [pid] FROM [dnt_posts1] WHERE [tid] =@tid AND [posterid]=@uid

~

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

~

CREATE PROCEDURE [dnt_getnewtopics1]
@fidlist VARCHAR(500)
AS
IF @fidlist <> ''
BEGIN
SELECT TOP(20)
[dnt_posts1].[tid],
[dnt_posts1].[title],
[dnt_posts1].[poster],
[dnt_posts1].[postdatetime],
[dnt_posts1].[message],
[dnt_forums].[name]
FROM [dnt_posts1]
LEFT JOIN [dnt_forums] ON [dnt_posts1].[fid]=[dnt_forums].[fid]
LEFT JOIN [dnt_topics] ON [dnt_posts1].[tid]=[dnt_topics].[tid]
WHERE CHARINDEX(','+RTRIM([dnt_forums].[fid])+',', ','+@fidlist+',') > 0
AND [dnt_posts1].[layer]=0 AND [dnt_topics].[displayorder] >= 0
ORDER BY [dnt_posts1].[tid] DESC
END
ELSE
BEGIN
SELECT TOP(20)
[dnt_posts1].[tid],
[dnt_posts1].[title],
[dnt_posts1].[poster],
[dnt_posts1].[postdatetime],
[dnt_posts1].[message],
[dnt_forums].[name]
FROM [dnt_posts1]
LEFT JOIN [dnt_forums] ON [dnt_posts1].[fid]=[dnt_forums].[fid]
LEFT JOIN [dnt_topics] ON [dnt_posts1].[tid]=[dnt_topics].[tid]
WHERE [dnt_posts1].[layer]=0 AND [dnt_topics].[displayorder] >= 0
ORDER BY [dnt_posts1].[tid] DESC
END
~

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

~

CREATE PROCEDURE [dnt_getlastpostlist1]
@tid int,
@pageindex int,
@postnum int,
@posttablename varchar(20)
AS
DECLARE @startRow int,
@endRow int
SET @startRow = (@pageIndex - 1) * @postnum +1
SET @endRow = @startRow + @postnum -1

SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY [pid]) AS ROWID,
[dnt_posts1].[pid],
[dnt_posts1].[fid],
[dnt_posts1].[layer],
[dnt_posts1].[posterid],
[dnt_posts1].[title],
[dnt_posts1].[message],
[dnt_posts1].[postdatetime],
[dnt_posts1].[attachment],
[dnt_posts1].[poster],
[dnt_posts1].[invisible],
[dnt_posts1].[usesig],
[dnt_posts1].[htmlon],
[dnt_posts1].[smileyoff],
[dnt_posts1].[parseurloff],
[dnt_posts1].[bbcodeoff],
[dnt_posts1].[rate],
[dnt_posts1].[ratetimes],
[dnt_users].[username],
[dnt_users].[email],
[dnt_users].[showemail],
[dnt_userfields].[avatar],
[dnt_userfields].[avatarwidth],
[dnt_userfields].[avatarheight],
[dnt_userfields].[sightml] AS [signature],
[dnt_userfields].[location],
[dnt_userfields].[customstatus]
FROM [dnt_posts1]
LEFT JOIN [dnt_users] ON [dnt_users].[uid]=[dnt_posts1].[posterid]
LEFT JOIN [dnt_userfields] ON [dnt_userfields].[uid]=[dnt_users].[uid]
WHERE [dnt_posts1].[tid] = @tid AND [dnt_posts1].[invisible] <=0 AND [dnt_posts1].layer <> 0) AS POST
WHERE ROWID BETWEEN @startRow AND @endRow

~

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

~

IF OBJECT_ID('[dnt_getdebatepostlist1]','P') IS NOT NULL
DROP PROC [dnt_getdebatepostlist1]
~
CREATE PROCEDURE [dnt_getdebatepostlist1]
@tid int,
@opinion int,
@pagesize int,
@pageindex int
AS
DECLARE @startRow int,
@endRow int
SET @startRow = (@pageindex - 1) * @pagesize + 1
SET	@endRow = @startRow + @pagesize - 1

SELECT
[DEBATEPOST].[attachment],
[DEBATEPOST].[bbcodeoff],
[DEBATEPOST].[fid],
[DEBATEPOST].[htmlon],
[DEBATEPOST].[invisible],
[DEBATEPOST].[ip],
[DEBATEPOST].[lastedit],
[DEBATEPOST].[layer],
[DEBATEPOST].[message],
[DEBATEPOST].[parentid],
[DEBATEPOST].[parseurloff],
[DEBATEPOST].[pid],
[DEBATEPOST].[postdatetime],
[DEBATEPOST].[poster],
[DEBATEPOST].[posterid],
[DEBATEPOST].[rate],
[DEBATEPOST].[ratetimes],
[DEBATEPOST].[smileyoff],
[DEBATEPOST].[tid],
[DEBATEPOST].[title],
[DEBATEPOST].[usesig],
[DEBATEPOST].[accessmasks],
[DEBATEPOST].[adminid],
[DEBATEPOST].[avatarshowid],
[DEBATEPOST].[bday],
[DEBATEPOST].[credits],
[DEBATEPOST].[digestposts],
[DEBATEPOST].[email],
[DEBATEPOST].[extcredits1],
[DEBATEPOST].[extcredits2],
[DEBATEPOST].[extcredits3],
[DEBATEPOST].[extcredits4],
[DEBATEPOST].[extcredits5],
[DEBATEPOST].[extcredits6],
[DEBATEPOST].[extcredits7],
[DEBATEPOST].[extcredits8],
[DEBATEPOST].[extgroupids],
[DEBATEPOST].[gender],
[DEBATEPOST].[groupexpiry],
[DEBATEPOST].[groupid],
[DEBATEPOST].[joindate],
[DEBATEPOST].[lastactivity],
[DEBATEPOST].[lastip],
[DEBATEPOST].[lastpost],
[DEBATEPOST].[lastpostid],
[DEBATEPOST].[lastposttitle],
[DEBATEPOST].[lastvisit],
[DEBATEPOST].[newpm],
[DEBATEPOST].[newpmcount],
[DEBATEPOST].[newsletter],
[DEBATEPOST].[nickname],
[DEBATEPOST].[oltime],
[DEBATEPOST].[onlinestate],
[DEBATEPOST].[pageviews],
[DEBATEPOST].[password],
[DEBATEPOST].[pmsound],
[DEBATEPOST].[posts],
[DEBATEPOST].[ppp],
[DEBATEPOST].[regip],
[DEBATEPOST].[secques],
[DEBATEPOST].[showemail],
[DEBATEPOST].[sigstatus],
[DEBATEPOST].[spaceid],
[DEBATEPOST].[templateid],
[DEBATEPOST].[tpp],
[DEBATEPOST].[uid],
[DEBATEPOST].[username],
[DEBATEPOST].[authflag],
[DEBATEPOST].[authstr],
[DEBATEPOST].[authtime],
[DEBATEPOST].[avatar],
[DEBATEPOST].[avatarheight],
[DEBATEPOST].[avatarwidth],
[DEBATEPOST].[bio],
[DEBATEPOST].[customstatus],
[DEBATEPOST].[icq],
[DEBATEPOST].[idcard],
[DEBATEPOST].[ignorepm],
[DEBATEPOST].[location],
[DEBATEPOST].[medals],
[DEBATEPOST].[mobile],
[DEBATEPOST].[msn],
[DEBATEPOST].[phone],
[DEBATEPOST].[qq],
[DEBATEPOST].[realname],
[DEBATEPOST].[sightml],
[DEBATEPOST].[signature],
[DEBATEPOST].[skype],
[DEBATEPOST].[website],
[DEBATEPOST].[yahoo]
FROM (SELECT ROW_NUMBER() OVER(ORDER BY [pid]) AS ROWID,
[dnt_posts1].[attachment],
[dnt_posts1].[bbcodeoff],
[dnt_posts1].[fid],
[dnt_posts1].[htmlon],
[dnt_posts1].[invisible],
[dnt_posts1].[ip],
[dnt_posts1].[lastedit],
[dnt_posts1].[layer],
[dnt_posts1].[message],
[dnt_posts1].[parentid],
[dnt_posts1].[parseurloff],
[dnt_posts1].[pid],
[dnt_posts1].[postdatetime],
[dnt_posts1].[poster],
[dnt_posts1].[posterid],
[dnt_posts1].[rate],
[dnt_posts1].[ratetimes],
[dnt_posts1].[smileyoff],
[dnt_posts1].[tid],
[dnt_posts1].[title],
[dnt_posts1].[usesig],
[dnt_users].[accessmasks],
[dnt_users].[adminid],
[dnt_users].[avatarshowid],
[dnt_users].[bday],
[dnt_users].[credits],
[dnt_users].[digestposts],
[dnt_users].[email],
[dnt_users].[extcredits1],
[dnt_users].[extcredits2],
[dnt_users].[extcredits3],
[dnt_users].[extcredits4],
[dnt_users].[extcredits5],
[dnt_users].[extcredits6],
[dnt_users].[extcredits7],
[dnt_users].[extcredits8],
[dnt_users].[extgroupids],
[dnt_users].[gender],
[dnt_users].[groupexpiry],
[dnt_users].[groupid],
[dnt_users].[joindate],
[dnt_users].[lastactivity],
[dnt_users].[lastip],
[dnt_users].[lastpost],
[dnt_users].[lastpostid],
[dnt_users].[lastposttitle],
[dnt_users].[lastvisit],
[dnt_users].[newpm],
[dnt_users].[newpmcount],
[dnt_users].[newsletter],
[dnt_users].[nickname],
[dnt_users].[oltime],
[dnt_users].[onlinestate],
[dnt_users].[pageviews],
[dnt_users].[password],
[dnt_users].[pmsound],
[dnt_users].[posts],
[dnt_users].[ppp],
[dnt_users].[regip],
[dnt_users].[secques],
[dnt_users].[showemail],
[dnt_users].[sigstatus],
[dnt_users].[spaceid],
[dnt_users].[templateid],
[dnt_users].[tpp],
[dnt_users].[uid],
[dnt_users].[username],
[dnt_userfields].[authflag],
[dnt_userfields].[authstr],
[dnt_userfields].[authtime],
[dnt_userfields].[avatar],
[dnt_userfields].[avatarheight],
[dnt_userfields].[avatarwidth],
[dnt_userfields].[bio],
[dnt_userfields].[customstatus],
[dnt_userfields].[icq],
[dnt_userfields].[idcard],
[dnt_userfields].[ignorepm],
[dnt_userfields].[location],
[dnt_userfields].[medals],
[dnt_userfields].[mobile],
[dnt_userfields].[msn],
[dnt_userfields].[phone],
[dnt_userfields].[qq],
[dnt_userfields].[realname],
[dnt_userfields].[sightml],
[dnt_userfields].[signature],
[dnt_userfields].[skype],
[dnt_userfields].[website],
[dnt_userfields].[yahoo]
FROM [dnt_posts1]
LEFT JOIN [dnt_users] ON [dnt_users].[uid] = [dnt_posts1].[posterid]
LEFT JOIN [dnt_userfields] ON [dnt_userfields].[uid] = [dnt_posts1].[posterid]
WHERE [dnt_posts1].invisible=0
AND [dnt_posts1].pid IN (SELECT pid FROM [dnt_posts1] WHERE [dnt_posts1].tid=@tid and [dnt_posts1].layer=0 UNION ALL SELECT pid FROM dnt_postdebatefields WHERE tid=@tid AND opinion=@opinion)) AS DEBATEPOST
WHERE ROWID BETWEEN @startRow AND @endRow

~

IF OBJECT_ID('dnt_getpostcountbycondition1','P') IS NOT NULL
DROP PROC [dnt_getpostcountbycondition1]
~

CREATE PROCEDURE [dnt_getpostcountbycondition1]
@tid int,
@posterid int
AS
SELECT COUNT(pid) FROM [dnt_posts1] WHERE [tid] = @tid AND [posterid] = @posterid  AND [layer]>=0
~

IF OBJECT_ID('dnt_getpostlistbycondition1','P') IS NOT NULL
DROP PROC [dnt_getpostlistbycondition1]
~

CREATE PROCEDURE [dnt_getpostlistbycondition1]
@tid int,
@pagesize int,
@pageindex int,
@posterid int
AS
DECLARE @startRow int,
@endRow int

SET @startRow = (@pageindex-1)*@pagesize
SET @endRow = @startRow + @pagesize - 1

SELECT
POSTS.[pid],
POSTS.[fid],
POSTS.[title],
POSTS.[layer],
POSTS.[message],
POSTS.[ip],
POSTS.[lastedit],
POSTS.[postdatetime],
POSTS.[attachment],
POSTS.[poster],
POSTS.[posterid],
POSTS.[invisible],
POSTS.[usesig],
POSTS.[htmlon],
POSTS.[smileyoff],
POSTS.[parseurloff],
POSTS.[bbcodeoff],
POSTS.[rate],
POSTS.[ratetimes],
POSTS.[nickname],
POSTS.[username],
POSTS.[groupid],
POSTS.[spaceid],
POSTS.[gender],
POSTS.[bday],
POSTS.[email],
POSTS.[showemail],
POSTS.[digestposts],
POSTS.[credits],
POSTS.[extcredits1],
POSTS.[extcredits2],
POSTS.[extcredits3],
POSTS.[extcredits4],
POSTS.[extcredits5],
POSTS.[extcredits6],
POSTS.[extcredits7],
POSTS.[extcredits8],
POSTS.[posts],
POSTS.[joindate],
POSTS.[onlinestate],
POSTS.[lastactivity],
POSTS.[invisible] AS usersinvisible,
POSTS.[avatar],
POSTS.[avatarwidth],
POSTS.[avatarheight],
POSTS.[medals],
POSTS.[signature],
POSTS.[location],
POSTS.[customstatus],
POSTS.[website],
POSTS.[icq],
POSTS.[qq],
POSTS.[msn],
POSTS.[yahoo],
POSTS.[oltime],
POSTS.[lastvisit],
POSTS.[skype]
FROM(SELECT ROW_NUMBER() OVER(ORDER BY [pid]) AS ROWID,
[dnt_posts1].[pid],
[dnt_posts1].[fid],
[dnt_posts1].[title],
[dnt_posts1].[layer],
[dnt_posts1].[message],
[dnt_posts1].[ip],
[dnt_posts1].[lastedit],
[dnt_posts1].[postdatetime],
[dnt_posts1].[attachment],
[dnt_posts1].[poster],
[dnt_posts1].[posterid],
[dnt_posts1].[invisible],
[dnt_posts1].[usesig],
[dnt_posts1].[htmlon],
[dnt_posts1].[smileyoff],
[dnt_posts1].[parseurloff],
[dnt_posts1].[bbcodeoff],
[dnt_posts1].[rate],
[dnt_posts1].[ratetimes],
[dnt_users].[nickname],
[dnt_users].[username],
[dnt_users].[groupid],
[dnt_users].[spaceid],
[dnt_users].[gender],
[dnt_users].[bday],
[dnt_users].[email],
[dnt_users].[showemail],
[dnt_users].[digestposts],
[dnt_users].[credits],
[dnt_users].[extcredits1],
[dnt_users].[extcredits2],
[dnt_users].[extcredits3],
[dnt_users].[extcredits4],
[dnt_users].[extcredits5],
[dnt_users].[extcredits6],
[dnt_users].[extcredits7],
[dnt_users].[extcredits8],
[dnt_users].[posts],
[dnt_users].[joindate],
[dnt_users].[onlinestate],
[dnt_users].[lastactivity],
[dnt_users].[oltime],
[dnt_users].[lastvisit],
[dnt_users].[invisible] AS usersinvisible,
[dnt_userfields].[avatar],
[dnt_userfields].[avatarwidth],
[dnt_userfields].[avatarheight],
[dnt_userfields].[medals],
[dnt_userfields].[sightml] AS signature,
[dnt_userfields].[location],
[dnt_userfields].[customstatus],
[dnt_userfields].[website],
[dnt_userfields].[icq],
[dnt_userfields].[qq],
[dnt_userfields].[msn],
[dnt_userfields].[yahoo],
[dnt_userfields].[skype]
FROM [dnt_posts1]
LEFT JOIN [dnt_users] ON [dnt_users].[uid]=[dnt_posts1].[posterid]
LEFT JOIN [dnt_userfields] ON [dnt_userfields].[uid]=[dnt_users].[uid]
WHERE [dnt_posts1].[tid]=@tid AND [dnt_posts1].[invisible]=0 AND [posterid]=@posterid) AS POSTS
WHERE ROWID BETWEEN @startROW AND @endROW
