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 2005 Forums
 Transact-SQL (2005)
 Insert values from one database to another

Author  Topic 

V.V.
Starting Member

32 Posts

Posted - 2010-11-12 : 05:06:33
Hello guys,

I'm new here and kinda new on SQL and that's way I want to ask you regarding a query. I need to import from Table_1 (Database_1) some columns for a specific number of records into Table_2 (Database_2). The structure of both databases are same (I'm referring of the columns that should be inserted).

So i was thinking at a query like this:

INSERT INTO Database_2.dbo.Table_2 D2 (column_1,column_2,etc)
SELECT column_1,column_2,etc
FROM Database_1.dbo.Table_1 D1
where D1.id in
(
id1,
id2,
id3
)
and D1.nr in
(
nr1,
nr2,
nr3
)

I mention the fact that nr1 belongs to id1 and so on.

Sorry for my bad english and thank you.

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-11-12 : 06:16:07
Are you facing any issue / getting any error ?
Go to Top of Page

V.V.
Starting Member

32 Posts

Posted - 2010-11-12 : 08:01:16
Yes, it gives an error:

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ID_Ag', table 'Database_2.dbo.Table_2'; column does not allow nulls. INSERT fails.
The statement has been terminated.


The column ID_Ag it isn't in my list of columns which should be inserted in Database_2.dbo.Table_2. The query tries to insert more columns than I specified or what?
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-11-12 : 08:08:13
quote:
Originally posted by V.V.

Yes, it gives an error:

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ID_Ag', table 'Database_2.dbo.Table_2'; column does not allow nulls. INSERT fails.
The statement has been terminated.


The column ID_Ag it isn't in my list of columns which should be inserted in Database_2.dbo.Table_2. The query tries to insert more columns than I specified or what?



Agree that column ID_Ag is not in your list but it is present in the destination table and is not null column.
Check it and pass some default value for it.
Go to Top of Page

V.V.
Starting Member

32 Posts

Posted - 2010-11-12 : 09:27:29
Yes, you're perfectly right. I made it in the end.

Now that I solved the problem with the insert of the records I have another question: can I UPDATE some columns of some records in Database_2.dbo.Table_2 with values from Database_1.dbo.Table_1? I found that the second database have been inactive for some while and now I have to bring it at same level with the first one. Again, there are too many records to be updated and manually takes a lot of time, if there is a way to be done automatic I want to try it. :)

Thanks so far because you ringing me the bell. :)

L.E.:

I discovered how to update second table with values from first table.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-12 : 12:11:03
I would do a dump and restore

unless there is data written that is different that db1



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

V.V.
Starting Member

32 Posts

Posted - 2010-11-12 : 13:36:40
Yes, that's the problem. Db2 is a new version of Db1, more performant and i can't do a dump and restore it. For a while, we are using both databases and until Db2 isn't 100% operational we have to work on both of them. Any way, i solved the problem, i found out how to update T2 from Db2.

By updating Db2 with these values it offers me the way to filter data from an application. The problem is now that some screens form this application aren't populated with data even if they are present in database.
I have to find where is the bug.
Go to Top of Page
   

- Advertisement -