Export table data from a particular data mart? - export

To obtain a list of all tables from all data marts in a data warehouse using Sybase, we use
select * from sysobjects where type = 'U'
However, I want to grab and export all tables from a particular data mart ("DM"). I'm relatively new to Sybase, so my gut's telling me to do something along the following lines:
select * from DM.sysobjects where type = 'U'
That's obviously not working, or else I would not be here :). Can someone point me in the right direction?

Based on the tables you mention, it looks like you are using Sybase ASE.
Sybase ASE doesn't support exporting all tables at once automatically through any method besides doing a dump of the database. To get all the tables in a database, you will need to create a script that will loop through all the tables in a database (what you are calling data mart).
There are many different ways to go about this, and it can vary based on if you are on a *nix or windows platform, but this will at least give you an idea.
set nocount on
select "bcp "+ name + " out " + name + ".bcp -Uusername -Sservername -Ppassword -c
from sysobjects where type = 'U'
go
This will give you a script file that should be easy to run against the server. -c denotes that it's a character mode or plain text file. There are other options for changing delimiters, and more but this should get you started.
This answer has more details on using bcp

Related

Is it possible to return query results as an Excel/CSV file formatted as a BLOB or base64 encoded string?

Limitations on some software I'm using require me to think outside the box, so can you have a query that automatically returns a "file" with the results in it? I imagine it would be as BLOB or a base 64 encoded string or something similar.
Using Microsoft SQL Server
You can use two methods to achieve this
1. Use SQLcmd
SQLCMD -S SERVERNAME -E -Q "SELECT Col1,Col2,Col3 FROM MyDatabase.dbo.MyTable"
-s "," -o "D:\MyData.csv"
Run this above command in a cmd and achieve the expected result.
2. Use OpenRawset
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
SELECT Field1, Field2, Field3 FROM DatabaseName
You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.
limitations:
The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.
If you can create objects in the target database or another database on the same server, you could use the stored procedures and functions from my answer to this question: SSMS: Automatically save multiple result sets from same SQL script into separate tabs in Excel?
You would just need to exclude the #OutputFileName parameter to force output as a single column, single row varbinary(max).
select * into #systables from sys.tables;
select * into #syscolumns from sys.columns;
select * into #systypes from sys.types;
exec dbo.GetExcelSpreadsheetData
#Worksheets = 'sys.tables/#systables/autofilter|sys.columns/#syscolumns/autofilter|sys.types/#systypes'
drop table #systables;
drop table #syscolumns;
drop table #systypes;
Output (truncated):
ExcelSpreadsheetData
-------------------------------------
0x504B0304140000000800DB78295479400A1

How can I compare tables in two different databases using SQL?

I'm trying to compare the schemas of two tables that exist in different databases. So far, I have this query
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('table1')
The only thing is that I don't know how to use the sys.columns to reference a database other than the one that the query is connected to. I tried this
SELECT * FROM db.sys.columns WHERE object_id = OBJECT_ID('table1')
but it didn't find anything.
I'm using SQL Server 2005
Any suggestions? thanks!
Take a look at redgate's SQL Compare.
To answer your specific question, you need to fully qualify the table reference.
SELECT * FROM db.sys.columns WHERE object_id = OBJECT_ID('db.SchemaName.table1')
Late one but hopefully useful.
Even though chama asked for SQL solutions I’d still recommend using a third party tools such as ApexSQL Diff or tools from Red Gate Joe already mentioned (I’ve used both and they worked great).
Reason is that query for comparing two tables using information schema has to be quite complex in order to catch all differences.
Note that all of the examples mentioned here only cover columns but none of the queries shown here will show the difference between nvarchar(20) and nvarchar(50) or difference in foreign keys or indexes or….
Short answer is yes – this is possible using information schema views but it can be rather complex if you want to compare every detail in those two tables.
all you need is to specify the DB name and shcema when calling OBJECT_ID function, like:
SELECT *
FROM DB_NAME.sys.columns
WHERE object_id = OBJECT_ID('DB_NAME.SHCEMA_NAME.table1')
Try the information_schema. eg:
select *
from
db1.information_schema.columns col1
join db2.information_schema.columns col2
on col1.table_catalog = col2.table_catalog
and col1.table_schema = col2.table_schema
and col1.column_name = col2.column_name
...
The information_schema simplifies sticking together the information from all the sys.columns,sys.objects etc. It exists automatically in your DB. I think its actually an ISO standard thing, so should work on various DB systems.
More information about the information_schema can be found here
Comparing whether the object or columns exists in both schemas is only a tiny bit of the solution. What if they exist in both databases but are different?
For my bsn ModuleStore project, I implemented a scripting routine which actually scripts most DB objects including table and view columns, indexes, namespaces etc. as XML using T-SQL code only. This may be a good place to start. You can find it on Google code, and the file in question (which generates the SQL query for dumping the object schema to XML) is here.
Code -
drop table #a
drop table #b
select *
into #a
from [databasename1].information_schema.columns a
--where table_name = 'aaa'
select *
into #b
from [databasename2].information_schema.columns b -- add linked server name and db as needed
--where table_name = 'bbb'
select distinct( a.table_name), b.TABLE_SCHEMA+ '.' + (b.table_name) TableName,b.TABLE_CATALOG DatabaseName
from #a a
right join #b b
on a.TABLE_NAME = b.TABLE_NAME and a.TABLE_SCHEMA = b.TABLE_SCHEMA
where a.table_name is null-- and a.table_name not like '%sync%'
Just in case you are using MS VS 2015 (Community is a free download). The SOL Server tools includes a Schema Comparison tool. "SQL Server Data Tools (SSDT) includes a Schema Compare utility that you can use to compare two database definitions".
This is a GPL Java program I wrote for comparing data in any two tables, with a common key and common columns, across any two heterogeneous databases using JDBC: https://sourceforge.net/projects/metaqa/
It intelligently forgives (numeric, string and date) data type differences by reducing them to a common format. The output is a sparse tab delimited file with .xls extension for use in a spreadsheet.
The program ingests SQL that is used to produce a source table that can be compared with the target table. The target table SQL can be generated automatically. The target table is read one row at a time and therefore should be indexed on the common key.
It detects missing rows on either side and common keyed rows with other optional column differences. Obviously the meta data can be accessed by SQL so whether your concern is with the data, or with the meta-data, it will still work.
This is very powerful in a data migration or System migration project, and also for auditing interfaces. You will be astounded at the number of errors it detects. Minimal false positives still do occur.
Informix, Oracle and SQL-Server were the first JDBC targets and you can extend that list if desired.

How to programmatically generate DDL from Oracle database?

I've got a monumentally tedious task that is to find several tables from a huge schema, and generate the DDL for these tables.
Say, I've got a schemaA has 1000 tables, I need to find if a tableA existed in this schemaA, if it does, generate the DDL and save it to filesystem, if don't, print it's name out or write it to a file. Any ideas?
The DBMS_METADATA package (assuming you are on a reasonably recent version of Oracle) will generate the DDL for any object in the database. So
SELECT dbms_metadata.get_ddl( 'TABLE', 'TABLEA', 'SCHEMAA' )
FROM dual;
will return a CLOB with the DDL for SchemaA.TableA. You could catch the exception that is thrown that the object doesn't exist, but I would tend to suggest that you query the data dictionary (i.e. DBA_OBJECTS) to verify that there is a table named TableA in SchemaA, i.e.
SELECT COUNT(*)
FROM dba_objects
WHERE owner = 'SCHEMAA'
AND object_name = 'TABLEA'
AND object_type = 'TABLE'
Note that if you don't have access to DBA_OBJECTS, you could use ALL_OBJECTS instead. The concern there, however, is that there may be a TableA in SchemaA that you don't have SELECT access on. That table would not appear in ALL_OBJECTS (which has all the objects that you have access to) but it would appear in DBA_OBJECTS (which has all the objects in the database regardless of your ability to access them).
You can then either write the DDL to a file or, if the count is 0, indicate that the object doesn't exist. From a stored procedure, you can use the UTL_FILE package to write to a file on the database server. If you are trying to write to a file on the client file system, you would need to use a language that has access to the client operating system's resources. A small C/ Java/ Perl/ etc. program should be able to select a CLOB and write that data to a file on the client operating system.

How to get the schema informataion/dump (without data) for an oracle database instance in xml format?

Well.. the question is descriptive enough I guess. What I am looking for is an exact equivalent of the below MySQL command in oracle-
mysqldump --xml --no-data -u[username] -p[pass] [db_instance] > [someXMLfile]
Where on a linux box do I have to run the oracle command? Straight inside the shell would do?
You can get an XML representation of any given table using the GET_XML function in the DBMS_METADATA package. The DBMS_METADATA documentation has an example of generating the XML for all tables in a schema (this excludes the storage clauses, though you can obviously eliminate that call)
set pagesize 0
set long 90000
execute DBMS_METADATA.SET_TRANSFORM_PARAM(
DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_ALL_TABLES u
WHERE u.nested='NO'
AND (u.iot_type is null or u.iot_type='IOT');
execute DBMS_METADATA.SET_TRANSFORM_PARAM(
DBMS_METADATA.SESSION_TRANSFORM,'DEFAULT');

What is the sql to query SqlServer system databases to find an DB object?

I have a large db with many tables and sprocs, and I want to find and see, for example, if there is a table with a name that has "setting" as part of it. I'm not very familiar with SqlServer's System Databases like master, msdb etc., I know there is a way to query one of those dbs to get what I need back, does someone know how to do it?
Thank you,
Ray.
SQL Server also supports the standard information schema views. Probably better to use them, since this query should also work across different database engines if you ever need to do a migration:
SELECT * FROM INFORMATION_SCHEMA.tables where table_name LIKE '%Settings%'
the table you want is sys.objects
SELECT *
FROM sys.objects
The table with the info you seek is called sysobjects. Here's a query for what you describe:
SELECT * FROM sysobjects WHERE xtype = 'U' AND NAME LIKE '%setting%'
(U is the type for user tables)
For Sql Server 2005
SELECT * FROM sys.objects where type in ('U') and name like '%setting%'

Resources