webForumDet fria alternativet

SQL sats

8 svar · 239 visningar · startad av renholm

renholmMedlem sedan apr. 20012 048 inlägg
#1

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
#2

@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
#3

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
#4

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
#5

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
#6

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
#7

tackar, precis vad jag ville :)
t fungerar alltså som javascriptets parent

PaceMedlem sedan juni 20015 947 inlägg
#8

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
#9

ok :) såg inte de... nog dags att läsa mer TSQL

Genererad på 393 ms · cache AV · v20260730165559-full.f96bc7eb