CREATE PROCEDURE forum_getThreads AS
DECLARE @id int
SELECT thread_title,
thread_by,
thread_postdate,
thread_views,
thread_id = @id,
(SELECT TOP 1 thread_by FROM threads WHERE thread_pid = @id ORDER BY thread_postdate DESC) As LastPostBy,
(SELECT TOP 1 thread_postdate FROM threads WHERE thread_pid = @id ORDER BY thread_postdate DESC) As LastPostDate,
(SELECT COUNT(*) FROM threads WHERE thread_pid = @id) As Replys
FROM threads WHERE thread_pid = 0 ORDER BY thread_postdate
LastPostBy, LastPostDate, Replys innehåller ingen data.
ok, då nästa fråga, hur sätter jag ett värde på @id då?
SPn går ut på att hämta alla trådar i forumet och sedan hämta från samma tabell senaste svaret på tråden. försökte med thread_pid = thread_id men fungerade inte.
Borde inte ID vara en inparameter till proceduren eller eventuellt att du skickar in namn på forumet och därifrån tar fram ID.
CREATE PROCEDURE forum_getThreads(@id int)
AS
SELECT thread_title,
thread_by, thread_postdate, thread_views, thread_id = @id,
(SELECT TOP 1 thread_by FROM threads WHERE thread_pid = @id ORDER BY thread_postdate DESC) As LastPostBy,
(SELECT TOP 1 thread_postdate FROM threads WHERE thread_pid = @id ORDER BY thread_postdate DESC) As LastPostDate,
(SELECT COUNT(*) FROM threads WHERE thread_pid = @id) As Replys
FROM threads WHERE thread_pid = 0 ORDER BY thread_postdate
Då har jag helt och hållet missförstått. (Hoppas jag förstår rätt nu.)
CREATE PROCEDURE forum_getThreads AS
SELECT thread_title,
thread_by, thread_postdate, thread_views, thread_id,
(SELECT TOP 1 thread_by FROM threads WHERE thread_pid = t.thread_id
ORDER BY thread_postdate DESC) As LastPostBy,
(SELECT TOP 1 thread_postdate FROM threads WHERE thread_pid = t.thread_id ORDER BY thread_postdate DESC) As LastPostDate,
(SELECT COUNT(*) FROM threads WHERE thread_pid = t.thread_id ) As Replys
FROM threads as t WHERE thread_pid = 0 ORDER BY thread_postdate
Du behöver alltså ingen variabel utan du kan referera direkt till den yttre tabellen.