I want to get list of all table names where a specific value is matched within all tables and all its columns. I already tried something like bellow but seems sql query is not correct. Any idea how I can do this?
select * from table where value='john#example.com'
I need this query for MS SQL server. Maybe its a bad idea to run such big query but this will be helpful to understand a development version of non relational database.
Related
I'm using VBA to display in an Excel sheet all customers filling some business related criteria.
The server I'm using uses 1 database per customer, I have no way around it, that's what it is. Thoses databases have the same structure though (same tables and same columns by table).
Through coding, I found a way to to this, which works, but I'd like/need to speed it up.
I first get all customers-related databases names through one query, and then execute a query on each result to decide if I should display it or not.
Since SQL treatments are considered to be faster than pretty much everything else, I think I could get my program running faster using a single query, with an "EXISTS" clause inside it, and display all results of this single query.
Here is roughly one thing I tried which might be close to what I need, but doesnt work as it is :
SELECT name FROM dbo.sysdatabases
WHERE (name LIKE 'DBC%')
AND EXISTS (SELECT TRUE FROM name.dbo.Purchases WHERE product_category = '012')
I just changed some parameters so it's not specific to my company, like what comes after LIKE and the condition after exists, the main point being that I need (do I ? ) to use name in my EXISTS clause to run through all relevant databases.
My problem is that SQL tells me that 'name.dbo.Purchases' is not a valid name (while, when I loop on a query so I can send a SQL query with a real name, say DBC01.dbo.Purchases, it works fine). How can I get name to be considered as what I'm already querying on, as opposed to "name" itself, in a single query ?
I'm not 100% sure that SELECT TRUE works fine but if I need to I'll take a simple field from my table, that's not a primary concern.
I wanted to reach out to ask if there is a practical way of finding out a given table's structure/schema e.g.,the column names and example row data inserted into the table(like the head function in python) if you only have the table name. I have access to several tables in my current role, however, a person who developed the tables left the team I am on. I was interested in examining the tables closer via SQL Assistant in Teradata (these tables often contain often hundreds of thousands of rows hence there are issues of hitting CPU exception criteria errors).
I have tried the following select statement, but there is an issue of hitting internal CPU exception criteria limits.
SELECT top10 * FROM dbc.table1
Thank you in advance for any tips/advice!
You can use one of these commands to get table's structure details in teradata
SHOW TABLE Database_Name.Table_Name;
or
HELP TABLE Database_Name.Table_Name;
It shows the table structure details
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 multiple tables which all have the same structure --except a couple of them have one column misnamed. I would like a sql statement that would allow the user to select that misnamed column using the correct name (there are only 2 possible names for the column-the correct one and the wrong one). I was thinking I could have the query first look at the all_tab_columns view to look up the table and decide which spelling of that column the table has to retrieve the data...
I understand the difficulty of renaming/altering existing production tables, but it seems like the best solution to this problem is simply to update the misnamed tables with the correct column name. Is there a reason (beyond extra work) that this is infeasible?
When you need to compare two tables to see what the differences are, are there any tools or shortcuts you use, or do you handcode the SQL to compare the two tables?
Basically the core features of a product like Red Gate SQL Data Compare (schemas for my tables typically always match).
Background: In my SQL Server environment, I created a stored procedure which inspects the metadata of the two tables/views, creates a query (as dynamic sql) which joins the two tables on the specified key columns, and compares data in the compare columns, reporting key differences and data differences. The query can either be printed and modified/copied or just excecuted as is. We are not allowed to create stored procedures in our Teradata environment, unfortunately.
Sounds like a data profiling tool such as Talend's Open Profiler would make the most sense at that point.
You could write a BTEQ statement that builds the query similar to your SQL Server stored procedure and then export the dynamically built SQL. You can then in turn run that inside of your BTEQ. It might get cumbersome, but with enough determination you could probably mock something up.
I dont know if this is the right answer you are searching for.
sel * from database_name1.table_name1
minus
sel * from database_name2.table_name2;
you can do the same by selecting specific columns. This will basically give the non existent rows from table2 which are in table1.
If you were not looking for this type of answer, please ignore this and continue.
Also you can select like below.
select
table1.keycol1,
table2.keycol2,
(table1.factcol1 - table2.factcol2) as diff
from table1
inner join
table2
on table1.keycol1 = table2.keycol1
and table1.keycol2 = table2.keycol2
where diff <> 0
This was just an analysis which can give an idea. Please ignore any syntactical and programmatical errors.
Hope this helps.