Firebird copy from one database using isql - database

I've got 2 firebird databases
c:\db1.gdb
c:\db2.gdb
both databases schema's are the same so both contain a table
MyTable
-------
Id int
Name varchar(50)
...etc
Its guaranteed that the data is different on both databases, however I need to copy from db2.MyTable into db1.MyTable
A requirement is that I do this using the firebird isql tool.
How would I firstly using isql
-Connect to both db's in one isql command window
-run a sql statement that would do a select all from one table in db2 and insert it into the same table in db1
I'm using firebird 1.5

This is not possible with FB 1.5. You can do this with Firebird 2.5 using the new "execute statement...external" feature that makes it possible to access another firebird database from inside triggers, procedures and code blocks.

If you really must do it using isql then you could write select statement which produces insert statements; run the select in db2, save the result into some file and then execute the statements in db1. The select statement would be something like
select 'insert into MyTable(id, name) values ('|| cast(id as varchar(10)) ||','''|| name ||''');' from MyTable;
However the job is much easier to do using something like Clever Components' Interbase DataPump.

Related

Where did my new table go

I have a database called mbt. I wanted to write some data from temporary table to real table.
--I used this query.
SELECT * INTO new_table FROM #tmp
when i runned the query it returned normal message.
15813 row(s) affected
After that i checked my tables in mbt database, but i couldn't see 'new_table'
how could such a thing be, where the table might have gone.
I may have forgotten to use 'use MBT' statment at the beginning of the query. Does it make problem
I'm using ms sql server 2014(SP2)(KB3171021)-12.0.5000.0(X64)
ANSWER
It gone to Master DB
select 'master' as DatabaseName,
T.name collate database_default as TableName
from master.sys.tables as T
It Will create a new table on your database. but you did not use so it will store in master database on your server.
Run the query below to find databases which have the object new_table:
sp_MSForEachDB 'Use [?] IF EXISTS (SELECT 1 FROM sys.objects WHERE name= ''new_table'')
SELECT DB_NAME()'
I had the same problem. What i did is, I rewrite the statement of use Database and then refresh the database browser after that i got Result. You can try it. may be it will help you.
Always use command "USE db_name" to make sure that you are querying right database.
Below command will show all databases available on the server.
SHOW DATABASES;
If you are using GUI tool to connect DB server, there is a possibility that at the time of connection you got connected to different DB. If you executed the query to create table and inserted record. These records are inserted in new table in different DB than mbt.

How can I create backup script from diagram in SQL Server?

I use SQL Server 2012 and SQL Server 2008 R2.
I create a script from all object (tables / trigger / stored procedure / function ...) in my database.
I generated this script from SQL Server Management Studio. I can recreate my database with this scrips on the other server. But I miss all diagrams of my database after run my script for create another database.
Therefore, I need create backup script from all diagrams that exist in my database.
I need execute this script on the destination database for recreating all my diagrams.
I found this Link. but i need some thinks that create all script (Insert Command) automatically.
I have found a reasonable solution. The problem is that Management Studio cannot display more that 65535 characters for Non-XML data, and cannot be set to display more than 65535.
See code for documentation :)
Backup script:
-- 1. Read from DB, using XML to workaround the 65535 character limit
declare #definition varbinary(max)
select #definition = definition from dbo.sysdiagrams where name = 'ReportingDBDiagram'
select
'0x' + cast('' as xml).value('xs:hexBinary(sql:variable("#definition") )', 'varchar(max)')
for xml path('')
-- 2. Open the result XML in Management Studio
-- 3. Copy the result
-- 4. Paste this in backup script for #definition variable
Restore script:
declare #definition varbinary(max)
set #definition = 0xD0CF -- Paste 0x0 value from Backup script
-- Create diagram using 'official' Stored Procedure
exec dbo.sp_creatediagram
#diagramname = 'ReportingDBDiagramCopy',
#owner_id = null,
#version = 1,
#definition = #definition
Scripting your database does not include diagrams as they are not server objects in the same way as a table or stored procedure; they exist as data in the sysdiagrams table.
A similar question on SO asked How do you migrate SQL Server Database Diagrams to another Database?
The accepted answer is to copy the contents of the sysdiagrams table to the new database, so you could include the table contents in your script. The answer with the most up-votes has a link to a way of scripting diagrams.
I've tried backing up and then restoring a database to the same server, deleting the diagram I had created (I only had one) and then running the following query:
INSERT INTO database2.dbo.sysdiagrams
(
NAME
,principal_id
,version
,DEFINITION
)
SELECT NAME
,principal_id
,version
,DEFINITION
FROM database1.dbo.sysdiagrams
The diagram was successfully restored, however I did do this on a restored backup, I should really test it with a new database generated from a script.
UPDATE:
I scripted a database and then created a new database from it. When trying to rebuild the diagrams using an INSERT statement I got the error
So although it seems possible it's not trivial to create diagrams in a new database created from a script. Go with the answer given regarding scripting diagrams and modify it for your own needs.
Perhaps you can investigate further and post your own answer :)
Here's a quick & dirty method I use. Since the query window won't display the full varbinary(max) value of the definition field, but the XML editor will, I output the rows to XML as follows:
Run the following query on the server/database that contains the diagrams:
SELECT 'INSERT sysdiagrams(name,principal_id,diagram_id,version,definition) VALUES('''+name+''','
+CONVERT(varchar(2),principal_id)+','+CONVERT(varchar(2),diagram_id)+','+CONVERT(varchar(2),version)+','
+'0x' + CAST('' as xml).value('xs:hexBinary(sql:column("definition"))','varchar(max)') +')'
FROM RCSQL_ClaimStatus.dbo.sysdiagrams
FOR XML PATH
Click on the generated link to open the XML result, and ctrl-a & ctrl-c to copy all rows generated.
Paste that output back into your query window. I usually paste it between a pair of IDENTITY_INSERT's like this:
--TRUNCATE TABLE sysdiagrams
SET IDENTITY_INSERT sysdiagrams ON;
<row>INSERT sysdiagrams(name,principal_id,diagram_id,version,definition) VALUES('ERD1',1,1,1,0xD0CF11E0A1B11AE100000...)</row>
<row>INSERT sysdiagrams(name,principal_id,diagram_id,version,definition) VALUES('ERD2',1,2,1,0xD0CF11E0A1B11AE100000...)</row>
<row>INSERT sysdiagrams(name,principal_id,diagram_id,version,definition) VALUES('ERD3',1,3,1,0xD0CF11E0A1B11AE100000...)</row>
SET IDENTITY_INSERT sysdiagrams OFF;
Remove the row & /row XML tags from your inserts, and run them on the target server. You can truncate the sysdiagrams table if you're replacing all values with new values.

Best way to access SQL statements and Stored Procedures in a database

Where is SQL information such as SQL statements and stored procedures located in a database? And How do I store them in a separate database?
For MS SQL 2008 Stored Procedures are stored under:
DatabaseName
Programmability
Stored Procedures
You can read SQL statements using system stored procedure sp_helptext:
declare #t table (RowNumber int identity(1,1), Line nvarchar(4000));
insert #t (Line) exec sp_helptext 'sp_helptext';
select * from #t order by RowNumber;
Or to see how sp_helptext does that.
Logically (and generally) all such 'meta' data in the database are treated structurally the same way as user data; the system simply maintains one or more schemas dedicated to system level data.
Physical organization of databases are very much vendor specific e.g. an Oracle 10g database will organize the data on disk in a manner specific to it. For this case, you are more likely to get concrete feedback if you note which database product you are using.
You can use the information_schema view.
select routine_name, routine_definition from INFORMATION_SCHEMA.routines

Transferring data from one database to another

I have two databases, lets say Database A and B, with different datastructures.
In Database A there is a table contacts.
In Database B there is a table Accounts.
I want to transfer data from Database A table contacts to Database B table Accounts.
I am use SQL Server 2005 and want to write sql scripts to do this.
Can someone tell me what's the easiest and most efficient way to achieve this:
a) If they are on the same SQL server
b) If they are on different SQL servers.
The easiest method isn't necessarily the most efficient. SSIS is likely the most efficient, as Mitch already indicated.
The easiest (if you don't know SSIS already) is to just set up a linked server to the remote DB, and SELECT the data across using four-part naming. You set up a linked server using sp_addlinkedserver and sp_addlinkedsrvlogin (check BOL for the syntax), and query as follows:
INSERT INTO MyLocalTable (ColumnList)
SELECT <Columns> FROM RemoteServer.RemoteDB.RemoteSchema.RemoteTable
Use SSIS. It will work for both local and remote cases, and will enable you to set up a transform between the tables, to map columns to lother columns etc.
Is it a one off transfer? If it's a simple transfer I write a SELECT statement to create the INSERT statements, i.e.
SELECT 'INSERT INTO Accounts (ID, Name) VALUES (' + CAST(ID as VARCHAR) + ', ''' + Name + ''')'
FROM Contacts
Run this on Database A - and it will spit out the all INSERT statements, which you copy and paste so you can run them on Database B.
Or, on the same database:
INSERT INTO DatabaseA.dbo.Accounts (ID, Name)
SELECT Id, Name
FROM DatabaseB.dbo.Contacts
Still not happy - try setting up linked servers: http://msdn.microsoft.com/en-us/library/aa213778(SQL.80).aspx

Is it possible to copy the data in a remote MSSQL table to a local Table?

What is the best way to go about doing this? I have found some INSERT statements, but they all seem to work just for inner-database copying.
My rendition:
INSERT INTO [PROGRAM003].[dbo].[faq]
([id], [category], [question], [answer], [tags])
SELECT ([id], [category], [question], [answer], [tags])
FROM [SQL2005_552664_gsow].[dbo].[faq]
I am not very fluent with MSSQL yet, so any and all help is appreciated.
How "remote" are we talking? If it's another database on the same server you can just prepend the database name, as your initial example showed:
INSERT INTO [mytable] SELECT * FROM [database].dbo.[table]
If it's on a completely different server, you need to set them up as linked servers. Then you can do this:
INSERT INTO [mytable] SELECT * FROM [server].[database].dbo.[table]
You must, of course, fully specificy the database and schema within that server as well, and expect performance to suffer because it will need to move data across the network.
You can use OPENDATASOURCE or OPENROWSET to access tables on other servers. You'll need to provide SQL credentials in the query or have both databases setup to allow access to your windows id for this to work. You can also use the import/export wizard in SQL Server Management Studio -- right click on the database and choose Tasks-->Import or Tasks-->Export.
USE master
GO
EXEC sp_addlinkedserver
#server = 'RemoteSer',
#srvproduct = '',
#provider = 'MSDASQL',
#provstr = 'DRIVER={SQL Server};SERVER=000.000.000.000\SQLEXPRESS;'
GO
SELECT * INTO bnkWeb.dbo.test1 FROM [RemoteSer].[bnk].[dbo].[test1]
You might want to look into DTS or (2005+) Integration Services. Both of these will take data from one DB (Table) and copy the data into another DB (Table).
Link to Integration Services: http://msdn.microsoft.com/en-us/library/ms141091.aspx

Resources