---
title: "SQL sats"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/48985-sql-sats"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "renholm"
published: "2002-07-29T12:39:00.000Z"
updated: "2002-07-29T20:28:32.000Z"
replies: 8
views: 239
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/48985-sql-sats"
---

# SQL sats

## #1 — renholm, 2002-07-29T12:39Z

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.

Permalänk: https://www.webforum.nu/p/48985

## #2 — LarsG, 2002-07-29T18:36Z

@id får ju aldrig något värde. (Den är väl null men ett villkor som

col = null

är aldrig sant

Permalänk: https://www.webforum.nu/p/735778

## #3 — renholm, 2002-07-29T18:40Z

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.

Permalänk: https://www.webforum.nu/p/735781

## #4 — LarsG, 2002-07-29T18:56Z

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

Permalänk: https://www.webforum.nu/p/735801

## #5 — renholm, 2002-07-29T19:05Z

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

Permalänk: https://www.webforum.nu/p/735805

## #6 — LarsG, 2002-07-29T19:12Z

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.

Permalänk: https://www.webforum.nu/p/735811

## #7 — renholm, 2002-07-29T19:16Z

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

Permalänk: https://www.webforum.nu/p/735813

## #8 — Pace, 2002-07-29T19:24Z

> t fungerar alltså som javascriptets parent

t är ju aliaset för tabellen threads:
...FROM threads as t WHERE thread...

Permalänk: https://www.webforum.nu/p/735816

## #9 — renholm, 2002-07-29T20:28Z

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

Permalänk: https://www.webforum.nu/p/735843

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/48985-sql-sats
