Solved

How do I copy entire data to different Table

Posted on 2007-12-04
4
184 Views
Last Modified: 2010-03-19
Hello. I have MS SQL 2005
I have two tables in same DataBase.
I want to copy Test table into DealerTable
1. Test
DealerName nvarchar(255)
DelaerMPA


2.Dealer
DealerID       int   Primarykey
DealerName nvarchar(255)
DealerMPA  int


How do I do it?
0
Comment
Question by:erin027
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 250 total points
ID: 20401948
1. Change Dealer table defination and set IDENTITY(1,1) to DealerID
2. Insert Into Dealer (DealerName, DealerMPA) Select DealerName, DealerMPA From Test

all should be good.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20402029
Is Dealer completely empty? - doesn't really matter
Is DealerID an identity column? - assume no

insert into Dealer
select row_number() over (order by DealerName), DealerName, DealerMPA
from Test
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20402037
Is Dealer completely empty? - NO. Okay try not to clash with existing PK
Is DealerID an identity column? - assume no

insert into Dealer
select isnull((select max(DealerID) from Dealer),0) + row_number() over (order by DealerName), DealerName, DealerMPA
from Test
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20402069
since dealerid is a primary key we cannot insert it directly. so that we can do a small workaround assuming that I am adding 7 with delaermpa and using it as dealer Id. This is just to give idea for our situation.Please note that the fieldname in Test table is delaermpa. if it is correct then proceed or else change the fieldname in the below given query

insert into dealer  select '7'+left(delaermpa,1) as dealerid, * from  test
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question