Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I copy entire data to different Table

Posted on 2007-12-04
4
Medium Priority
?
218 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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this article, we’ll look at how to deploy ProxySQL.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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