Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Inserting from one table to another

Author  Topic 

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-06-02 : 16:23:35
Hi,

I have some data which is in one table and I need to insert that data into a another table. Both tables have exact columns. How to write query for this ?

Mohammad Azam
www.azamsharp.net

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-02 : 16:25:03
INSERT INTO DestinationTable(Column1, Column2)
SELECT Column1, Column2
FROM SourceTable
WHERE...

Tara
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-06-02 : 16:26:40
Thanks TARA you are the QUEEN OF SQL :D



Mohammad Azam
www.azamsharp.net
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-06-02 : 16:30:45
hmm one problem:

I am inserting data from my server to another server and it gives me this error:

Any ideas. I have matched the columns and there are 15 columns.

Insert Error: Column name or number of supplied values does not match table definition.

Mohammad Azam
www.azamsharp.net
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-02 : 16:33:51
If you are going across servers, you'll need to use linked servers and use the 4 part naming convention.

On your error, are you using * instead of a column list? If not, please post the query that gave the error as well as the DDL for both tables.

Tara
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-06-02 : 16:40:21
This is my Query:

INSERT INTO Square008.CWCourses.dbo.tblStudents

SELECT tblInstDescLU_ID,SSN,tblInstIDTypeLU_ID
,UnivID,LName,FName,MName,Major,Classification,Phone,Email,
Active,Comments,MTDS,CTDS

FROM tblStudents WHERE

tblStudentsID IN ('12737','17259','17260','17261','17262','17265')

And I also RAN sp_AddLinkedServer and it joined the Server. I know this because I queries the server table and it gave me the result.

Mohammad Azam
www.azamsharp.net
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-02 : 16:45:57
You need to provide a column list on the INSERT INTO line like in my example.

Lose the tbl prefix on your tables. That's an Access thing. You might want to also reconsider your ID names: tblInstDescLU_ID and tblInstIDTypeLU_ID. Just suggestions of course and nothing to do with the answer to your question.

Tara
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2005-06-02 : 16:54:36
thanks again TARA

:D

Mohammad Azam
www.azamsharp.net
Go to Top of Page
   

- Advertisement -