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)
 Copy/Insert Records One Database to Another

Author  Topic 

Littleone
Starting Member

1 Post

Posted - 2009-05-26 : 08:51:57
I put together the following sql below for copying from one SQL database to another the databases are on different SQL servers there fore I have added the SQL server names to the database. The other point that I had to make sure of was that the only the records having the current date from the first database are selected for the insert to the second database. Just wanting to make sure I have done this correctly. Would appreciate a look from anyone familar with this kind of SQL.
INSERT INTO [NAQC00LYO001\Ameri].[Hits_Backup].[Alarms_Backup_01].[dbo].[ALARMS_LY-197-03]
SELECT *
FROM [LY-197-03/ME].[IME].[ALARMS_01]
WHERE [ALM_DATEIN]= CONVERT(varchar(10), GetDate(), 101)

The data has a format in database of mm-yy-yyyy that is why I choose the 101 for the GetDate format.
Thanks
LittleOne

nr
SQLTeam MVY

12543 Posts

Posted - 2009-05-26 : 09:03:17
Looks ok but why are you storing the date as a character? Are you sure it's not a datetime and that's a converted format for display?
I take it the date doesn't contain a time and you're sure there aren't already some rows with that date transferred.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 12:22:34
quote:
Originally posted by Littleone

I put together the following sql below for copying from one SQL database to another the databases are on different SQL servers there fore I have added the SQL server names to the database. The other point that I had to make sure of was that the only the records having the current date from the first database are selected for the insert to the second database. Just wanting to make sure I have done this correctly. Would appreciate a look from anyone familar with this kind of SQL.
INSERT INTO [NAQC00LYO001\Ameri].[Hits_Backup].[Alarms_Backup_01].[dbo].[ALARMS_LY-197-03]
SELECT *
FROM [LY-197-03/ME].[IME].[ALARMS_01]
WHERE [ALM_DATEIN]= CONVERT(varchar(10), GetDate(), 101)

The data has a format in database of mm-yy-yyyy that is why I choose the 101 for the GetDate format.
Thanks
LittleOne




is [ALM_DATEIN] a datetime field? if yes, i think you can do like this rather than converting to varchar

[ALM_DATEIN] > DATEADD(dd,DATEDIFF(dd,0, GetDate()), 0)
AND [ALM_DATEIN] < DATEADD(dd,DATEDIFF(dd,0, GetDate())+1, 0)
Go to Top of Page
   

- Advertisement -