---
title: "Kopiera data från gammal tabell till ny tabell"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/170057-kopiera-data-från-gammal-tabell-till-ny-tabell"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "Bjuris"
published: "2008-03-19T13:39:31.000Z"
updated: "2008-03-20T08:04:27.000Z"
replies: 1
views: 359
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/170057-kopiera-data-från-gammal-tabell-till-ny-tabell"
---

# Kopiera data från gammal tabell till ny tabell

## #1 — Bjuris, 2008-03-19T13:39Z

Jag har en gammal tabell (Question) enl
\-QuestionID - int identity
\-BAS nvarchar(50)
\-TopicLevel nvarchar(50)
\-Topic nText

Jag har skapat tre nya tabeller enl
1\. Base
\-BaseID  uniqueIdentifier newID()
\-BaseName nvarchar(50)

2\. Topic
TopicId uniqueIdentifier newID()
TopicName

3\. Goal
GoalId  uniqueIdentifier newID()
BaseId uniqueIdentifier 
TopicId uniqueIdentifier 
GoalText nvarchar(max)

Och jag skulle alltså vilja få in det gamla datat från Question in i goal.

För Question.Bas gäller att jag skall gå in i Base och där Question.Bas = Base.BaseName så skall jag putta in Base.BaseId i Goal.BaseId

För Question.TopicLevel gäller att jag skall gå in i Topic och där Question.TopicLevel = Topic.TopicName så skall jag putta in Topic.TopicId i Goal.TopicId

För Question.Topic gäller att den skall in på Goal.GoalText

Kör Microsoft SQL Server

Problemet är att jag inte vet hur jag skall lösa detta...

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

## #2 — Bjuris, 2008-03-20T08:04Z

om någon är intresserad så löste jag det enl:

```
INSERT INTO Goal(GoalId, BaseId, TopicId, GoalText)
SELECT     NEWID() AS Expr1, Base.BaseId, Topic.TopicId, Q.Topic
FROM        Question AS Q 
LEFT OUTER JOIN Base AS Base ON Q.BAS = Base.BaseName 
LEFT OUTER JOIN Topic AS Topic ON Q.TopicLevel = Topic.TopicName
```

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

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/170057-kopiera-data-från-gammal-tabell-till-ny-tabell
