---
title: "Delete och select i samma"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/21648-delete-och-select-i-samma"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "bagera"
published: "2001-10-04T05:55:00.000Z"
updated: "2001-10-04T13:05:00.000Z"
replies: 8
views: 166
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/21648-delete-och-select-i-samma"
---

# Delete och select i samma

## #1 — bagera, 2001-10-04T05:55Z

Kan jag execvera alla dom här "samtidigt" på något sätt?
kan man skriva alltihop i en sträng?

strsql = "SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&';"

strsql2 = "DELETE from headtable where headtableid ='"& vart1 &"' ;"

strsql3 = "DELETE from questiontable where headtableid ='"& vart1 &"' ;"

strsql4 = "DELETE from responsetable where questiontableid ='"& vart2 &"' ;"

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

## #2 — Vide, 2001-10-04T06:30Z

Javisst, separera dom med ";" bara. :)

```
strsql = "SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&'; DELETE from headtable where headtableid ='"& vart1 &"'; DELETE from questiontable where headtableid ='"& vart1 &"'; DELETE from responsetable where questiontableid ='"& vart2 &"';"
```

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

## #3 — bagera, 2001-10-04T07:15Z

Så enkelt det kan vara ibland:)
Men hur gör jag då när selectens värde ska in i variabeln "vart2"?

strsql = "SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&'; DELETE from headtable where headtableid ='"& vart1 &"'; DELETE from questiontable where headtableid ='"& vart1 &"'; DELETE from responsetable where questiontableid ='"& vart2 &"';"

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

## #4 — @nders, 2001-10-04T07:22Z

```
strsql = "SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&'; DELETE from headtable where headtableid ='"& vart1 &"'; DELETE from responsetable where questiontableid IN(SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&'); DELETE from questiontable where headtableid ='"& vart1 &"'; "
```

Jag bytte plats på dina två sista DELETE-satser, eftersom det hade blivit lite krångligt annars.

**Redigering:** Behöver du verkligen din första SELECT-fråga där förresten? Ska du använda ID:t till något annat än till DELETE-satsen?

Mvh,

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

## #5 — LarsG, 2001-10-04T07:23Z

Du kan sätta villkoret direkt i delete-satsen.

```
strsql = "DELETE from headtable where headtableid ='"& vart1 &"'; DELETE from responsetable where questiontableid in (SELECT questiontableid FROM questiontable where headtableid ='"& vart1 "&');DELETE from questiontable where headtableid ='"& vart1 &"'"
```

Jag ändrade på ordningen mellan dina delete-satser.

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

## #6 — bagera, 2001-10-04T09:24Z

Vilka hjältar ni är!

Bara därför svänger jag iväg en snabbfråga.

behöver man stänga databasen om man använder denna? 
Set rs = conn.Execute(strsql)

Tack å bock!

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

## #7 — Vide, 2001-10-04T09:54Z

Databasen måste du alltid stänga, och skapar du ett recordset så skall det också stängas...

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

## #8 — bagera, 2001-10-04T12:56Z

Så här ser hela koden ut, tar jag bort stängningen så här funkar den, inte annars.
vad gör jag för fel?

vart1 =request.querystring("id")

strconn = MM_forum_STRING

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strconn

strsql = "DELETE from responsetable where questiontableid in (SELECT questiontableid FROM questiontable where headtableid ='"& vart1 &"');DELETE from questiontable where headtableid ='"& vart1 &"'; DELETE from headtable where headtableid ='"& vart1 &"'"

Set rs = conn.Execute(strsql)
response.redirect"admin.asp"

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

## #9 — LarsG, 2001-10-04T13:05Z

I och med att du gör execute på ett statement som inte returnerar några poster så får du ett tomt och stängt recordset tillbaka.

```
conn.Execute strsql,,128
conn.close
response.redirect"admin.asp"
```

Du kan utelämna tilldelningen av recordsetet.

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

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/21648-delete-och-select-i-samma
