I am working on a team trying to phase out a legacy system. As this is a rather large system with multiple integrations, the database will live on, even after the legacy system is replaced.
Now, the problem is that all the table names in the database have numerical names: "RT001", "RT002", "RT003", etc. With well over 100 tables it gets harder to know what each table is, and how can be joined to get hold of specific data.
Is there a way to define a global table alias in sybase so that sybase knows that the SQL"select * from Order, OrderItems where ..." is referring to tables RT035 and RT036 ? This way I can keep the original table names as RT035, while having an alias like "Order", or even "RT035_Order" refer to it.
As far as I know there is no such thing as a "synonym" (that's what it's called in an Oracle database) in Sybase ASE. But you could still use simple view to do basically the same thing:
CREATE VIEW Order AS SELECT * FROM RT035;
Related
Are there any functions / keywords / syntax in SOQL queries that does NOT have an equivalent operation in SQL?
Basically, does there exist a SOQL query that you couldn't convert directly into a SQL query?
Weird question, why do you ask? And which SQL you mean exactly, Oracle, SQL Server flavour, Maria DB or what?
I'd say you'll have hard time
mapping SELECT Account.Owner.Manager.Profile.Name FROM Opportunity into "normal" joins
replicating TOLABEL() (translate picklist values on the fly)
replicating anything SF-specific like WITH (say knowledge base's data categories) or USING SCOPE (you can pull "my accounts" but can you pull "my team's accounts"? "My territory's accounts"? Without an orgy of joins)
doing joins over mutant polymorphic fields like Task.WhatId or ContentDocumentLink.LinkedEntityId
doing any kind of SOSL, especially if org uses synonyms
converting currencies on the fly
doing things like FISCAL_YEAR() without orgy of joins to Period table
replicating any geolocation-related queries (accounts up to 10 km away from me) without knowing exactly what GIS (or whatever) SF uses
doing soft deletes (or however Recycle Bin really works) without impact on performance. I mean if records go to another table - columns are identical and join/view happens magically when you query ALL ROWS?
doing any Person Account stuff, silently querying and updating effectively 2 tables (as materialised view maybe?)
Some differences of SOQL:
No SELECT *
No views
SOQL read-only
Limited indexes
Object-relational mapping is automatic
Schema changes protected
Using Delphi I need to create a class which will contain specific table structure (without data), including all fields, constraints, foreign keys, indexes. The goal is having "standard" table, compare them and find differences. This thing ought to be included into my big project so I can't use any "outer" comparators. Furthermore, this functionality might be extended, so I need to have my own realization. The question is how can I retrieve this information, having connection string and knowing specific table name. SQL Server 2008 is being used.
If you look at Delphi source, it's done this way :
Select * from Table where 1=2
Update:
Metadata can be retrieved with Information Schema Views , for example constraints :
SELECT * FROM databaseName.INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
Where TABLE_NAME='tableName'
Been a very long time since I touched Delphi, but I do remember a couple things I used to do. Like
select top 0 * from table
returns 0 records but the TQuery is 'populated' with the meta data. Or I think on a TClientDataSet you can set the rows to -1, which has the same effect.
As I said, it's been a long time since I tinkered in Delphi and I was using the BDE and not native clients so this could all be useless info.
Hope this helps a bit though.
I've a database which contains several tables for various tables of different products. These products have unique part numbers across all tables.
To search across all tables, I've created a view which uses UNION ALL across all common fields in the tables.
Once a part has been identified, I need to select all the columns depending on the table the data resides in. The view includes a field that specifies the table the data was found in.
I'm not sure of the way to accomplish the last part:
CASE statement (I'm leaning towards this one at the moment)
Dynamic SQL (prefer not to use this, would involve SELECT * and other nasties)
SELECT in client side (client needs to select from arbitrary tables, require additional privileges, bad design?)
Alternative solution?
EDIT: Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.
(I'd mark the question as answered, but I don't have enough reputation for that)
I am not sure whether i understood your question correctly.. my understanding is you have views which is selecting data from diffrent tables using union all.. you can give table name while creating view only
select "table1",table1.a,table1.b.. from table1
union all
select "table2", table2.a,table2.b ..... from table2
Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.
I have lately learned what is dynamic sql and one of the most interesting features of it to me is that we can use dynamic columns names and tables. But I cannot think about useful real life examples. The only one that came into my mind is statistical table.
Let`s say that we have table with name, type and created_data. Then we want to have a table that in columns are years from created_data column and in row type and number of names created in years. (sorry for my English)
What can be other useful real life examples of using dynamic sql with column and table as parameters? How do you use it?
Thanks for any suggestions and help :)
regards
Gabe
/edit
Thx for replies, I am particulary interested in examples that do not contain administrative things or database convertion or something like that, I am looking for examples where the code in example java is more complicated than using a dynamic sql in for example stored procedure.
An example of dynamic SQL is to fix a broken schema and make it more usable.
For example if you have hundreds of users and someone originally decided to create a new table for each user, you might want to redesign the database to have only one table. Then you'd need to migrate all the existing data to this new system.
You can query the information schema for table names with a certain naming pattern or containing certain columns then use dynamic SQL to select all the data from each of those tables then put it into a single table.
INSERT INTO users (name, col1, col2)
SELECT 'foo', col1, col2 FROM user_foo
UNION ALL
SELECT 'bar', col1, col2 FROM user_bar
UNION ALL
...
Then hopefully after doing this once you will never need to touch dynamic SQL again.
Long-long ago I have worked with appliaction where users uses their own tables in common database.
Imagine, each user can create their own table in database from UI. To get the access to data from these tables, developer needs to use the dynamic SQL.
I once had to write an Excel import where the excel sheet was not like a csv file but layed out like a matrix. So I had to deal with a unknown number of columns for 3 temporary tables (columns, rows, "infield"). The rows were also a short form of tree. Sounds weird, but was a fun to do.
In SQL Server there was no chance to handle this without dynamic SQL.
Another example from a situation I recently came up against. A MySQL database of about 250 tables, all in MyISAM engine and no database design schema, chart or other explanation at all - well, except the not so helpful table and column names.
To plan for conversion to InnoDB and find possible foreign keys, we either had to manually check all queries (and the conditions used in JOIN and WHERE clauses) created from the web frontend code or make a script that uses dynamic SQL and checks all combinations of columns with compatible datatype and compares the data stored in those columns combinations (and then manually accept or reject these possibilities).
I have a UDF in SQL 2005 that I would need to schemabind as it is used in a view that I need an index on.
This UDF gets information from a table that is located in a different db (same server) then the one where the UDF is located.
Since it is invalid to specify table as [DBName].dbo.[Tablename], is there a way I can get the info from the table in the other db?
Schema binding is supposed to guarantee consistency. However consistency can not be guaranteed across two different databases, therefore schema-binding cannot be made across two different databases. In other words it's impossible to achieve.
Imagine that, for example, one database is restored to an earlier point in time - the index on the indexed view would become corrupt and queries would be returning wrong results.
If your UDF is in Database1, and it needs to access data from a table in Database2, all you have to do is create a view in Database1 that grabs the data you need from the table(s) in Database2. Then use this view in your UDF.
Works just fine, I used this approach many times.
Hope it helps.