Solved

How do I copy entire data to different Table

Posted on 2007-12-04
4
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now