webForumDet fria alternativet

Kopiera data från gammal tabell till ny tabell

Databaser & SQL

1 svar · 340 visningar · startad av Bjuris

Medlem sedan juli 2001514 inlägg
Frågan#1

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

Medlem sedan juli 2001514 inlägg
#2

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
263 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
131 ms — deklarationer (db)
0 ms — hämta statistik (cache)
130 ms — hämta tråd, inlägg och bilagor (db)
127 ms — ändringar (db)