renholmMedlem sedan apr. 20012 048 inlägg
Har problem med följande SP:
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.
LarsGMedlem sedan dec. 200012 465 inlägg
@id får ju aldrig något värde. (Den är väl null men ett villkor som
col = null
är aldrig sant
renholmMedlem sedan apr. 20012 048 inlägg
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.
LarsGMedlem sedan dec. 200012 465 inlägg
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
och då kan ditt anrop se ut
exec forum_getThreads 4711
renholmMedlem sedan apr. 20012 048 inlägg
hum, den ska ta id från den första select satsen och därifrån hämta de som har parent id lika med första select satsens id.
Data i tablen ser ut så här:
thread_title thread_by thread_postdate thread_views thread_id thread_pid
Test Test 2002-02... 2 1 0
Re: Test Tes 2002-02... 2 2 1
Re: Test Tes 2002-02... 2 4 1
LarsGMedlem sedan dec. 200012 465 inlägg
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.
renholmMedlem sedan apr. 20012 048 inlägg
tackar, precis vad jag ville :)
t fungerar alltså som javascriptets parent
PaceMedlem sedan juni 20015 947 inlägg
t fungerar alltså som javascriptets parent
t är ju aliaset för tabellen threads:
...FROM threads as t WHERE thread...
renholmMedlem sedan apr. 20012 048 inlägg
ok :) såg inte de... nog dags att läsa mer TSQL