PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > How Do You Copy Data From One Table to Another?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How Do You Copy Data From One Table to Another?

Réponse
 
LinkBack Outils de la discussion
Vieux 28/03/2008, 18h47   #1
alvinstraight38@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How Do You Copy Data From One Table to Another?

I can't believe this function is not simple. All I need to do is copy
the entire contents of one database table into another existing
table. The two tables exist on separate databases. I am trying to
load the patient list from Database A patients_visits table to
Database B patients_visits table.

I tried some SQL commands, and the best I could do was to create a
brand new table in my database with the copied contents. The new table
is called patients_two. So I tried this commmand:

insert into patients_visits select * from patients_two

Freaking SQL complains:

"An explicit value for the identity column in table 'patients_visits'
can only be specified when a column list is used and IDENTITY_INSERT
is ON."

Can anyone tell me what I am doing wrong, or is there an easier
method?

Thanks!
  Réponse avec citation
Vieux 28/03/2008, 19h35   #2
Linchi Shea
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: How Do You Copy Data From One Table to Another?

INSERT ... SELECT ... FROM is the right method. If you want to also copy the
identity values instead of letting SQL Server generate them, you need to run

SET IDENTITY_INSERT <target table> ON

before the INSERT statement. Look up SET IDENTITY_INSERT in Books Online.

Linchi

"alvinstraight38@hotmail.com" wrote:

> I can't believe this function is not simple. All I need to do is copy
> the entire contents of one database table into another existing
> table. The two tables exist on separate databases. I am trying to
> load the patient list from Database A patients_visits table to
> Database B patients_visits table.
>
> I tried some SQL commands, and the best I could do was to create a
> brand new table in my database with the copied contents. The new table
> is called patients_two. So I tried this commmand:
>
> insert into patients_visits select * from patients_two
>
> Freaking SQL complains:
>
> "An explicit value for the identity column in table 'patients_visits'
> can only be specified when a column list is used and IDENTITY_INSERT
> is ON."
>
> Can anyone tell me what I am doing wrong, or is there an easier
> method?
>
> Thanks!
>

  Réponse avec citation
Vieux 28/03/2008, 19h36   #3
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How Do You Copy Data From One Table to Another?

To copy between databases you have to qualify the tables with their
database: DBName.User_Schema.TableName

To insert the data into existing table you have to list the columns when
running the statement. If you do not want to copy the exact values of the
IDENTITY column you can exclude it from the list, it will automatically
generate values in the target table. If you want to copy the values, then
set IDENTITY_INSERT to ON.

Examples below assuming the tables are under the 'dbo' user/schema:

1) Without IDENTITY column:
INSERT INTO DatabaseB.dbo.patients_visits (column1, column2, ...)
SELECT column1, column2, ... FROM DatabaseA.dbo.patients_visits

2). With IDENTITY column - set IDENTITY_INSERT to ON:
SET IDENTITY_INSERT DatabaseB.dbo.patients_visits ON

INSERT INTO DatabaseB.dbo.patients_visits (ident_column, column1, column2,
....)
SELECT ident_column, column1, column2, ... FROM
DatabaseA.dbo.patients_visits

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 30/05/2008, 14h50   #4
Thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: How Do You Copy Data From One Table to Another?

Try http://www.sqlscripter.com to generate data scripts.


"alvinstraight38@hotmail.com" wrote:

> I can't believe this function is not simple. All I need to do is copy
> the entire contents of one database table into another existing
> table. The two tables exist on separate databases. I am trying to
> load the patient list from Database A patients_visits table to
> Database B patients_visits table.
>
> I tried some SQL commands, and the best I could do was to create a
> brand new table in my database with the copied contents. The new table
> is called patients_two. So I tried this commmand:
>
> insert into patients_visits select * from patients_two
>
> Freaking SQL complains:
>
> "An explicit value for the identity column in table 'patients_visits'
> can only be specified when a column list is used and IDENTITY_INSERT
> is ON."
>
> Can anyone tell me what I am doing wrong, or is there an easier
> method?
>
> Thanks!
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12244 seconds with 12 queries