I am using SQL Server 2005. I am having a peculiar need now.
I am having a Database and it in turn have several table(>50). All of these table have a similar named column with a condition. I want to query this column across all the tables in the DB.
I am not finding a proper solution in Googling. Please help me.
Thanks a lot in Advance :)
Maybe this can help you?
SELECT * FROM information_schema.tables
Afterwards, you can loop through all tables and query your column.
Using Kevin's suggestion you do something like this, then execute it's output.
DECLARE #ColumnName AS VARCHAR(20)
SET #ColumnName = 'MyColumn'
SELECT 'SELECT ' + #ColumnName + ' FROM ' + TABLE_NAME FROM information_schema.tables
sp_msforeachtable #command1=N'SELECT ''?'', * from ? WHERE yourcol=''whatever'''
Above will run query against every table in selected database, and print tablename + every column in the table for conditions met. If it reaches a table that has an invalid column, it'll just keep going (but print an error message).
Related
Say, I have 100 columns in a table. I do not know in which columns a particular value could exist. So, I would like to check across all columns, if it exists in any of the 100 columns, I would like to select it.
I searched around a bit, and in most places the solution seems to be something like the following
select *
from tablename
where col1='myval'
or col2='myval'
or col3='myval'
or .. or col100='myval'
I also read a few forums where having to do this is said to be a bad case of database design, I agree, but I'm working on an already existing table in a database.
Is there a more intelligent way to do this?
One way is by reversing the In operator
select *
from yourtable
where 'Myval' in (col1,col2,col3,...)
If you don't want to manually type the columns use dynamic sql to generate the query
declare #sql varchar(max)='select *
from yourtable
where ''Myval'' in ('
select #sql+=quotename(column_name)+',' from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='yourtable'
select #sql =left(#sql,len(#sql)-1)+')'
--print #sql
exec sp_executesql #sql
I have imported a table to my database, and to get the number of columns and assign it to a variable I do
SELECT #HowManyColumns = COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = #table_name )
But it keeps telling that the count is 0!
If I do the same for other tables it works!
I have found that the column count of the table that is not working is more than 40 columns, Why is it not working...
The data is this
Since tables w/o columns don't exists, that can only mean that the WHERE clause is not satisfied. In other words, the table named as the value of #table_name does not exists. Since you say 'sometimes it work, and some does not' that would immediately point toward case sensitive deployments. Make sure you always use the correct name for the table, with the proper case, so your code work correctly on servers which are deployed with a case sensitive collation.
Try issuing
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA
and check table name for case.
Try having the table name as a text string instead:
DECLARE
#table_name varchar(50),
#noOfColumns int
SET
#table_name = 'table_name'
SET
#noOfColumns =
(SELECT count(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #table_name)
PRINT #noOfColumns
The answer you get is the number of columns in the #noOfColumns variable
I'm debating whether or not to try running through a list of tables and truncating them with a stored procedure. Would it be that easy with MySql and how would I do it?
The main piece of info you need is the list of tables. Most platforms support this:
select table_name from information_schema.tables
However, before you code the sproc, do a select * from information_schema.tables and examine the entries, there may be some you do not expect -- system tables and such, so you may need to craft a filter to get the set you want.
Since I don't do mySQL that much, I can't show you the code, but if you can translate this from MS SQL, and fill in some blanks you can make it work:
declare #table_name varchar(200)
while 1=1 begin
select top 1 #table_name = table_name
from information_schema.tables
where ....possible filter...
if #table_name is null break
-- for this line you may need dynamic sql
truncate table #table_name
end
So i'm a pretty good systems admin and can manage mysql servers with no issue. My problem is in actually coding sql, and more specifically coding sql I feel safe to automate in nightly scripts. Thats where you awesome stack overflow guys come in.
I currently have my production wordpress site sync to my dev server so we have as close to live for testing on before a push to production. Right after I update the mysql to be a copy of production I need to find and replace about 2,000 strings.
All I need to do is find a way to execute SQL to find and replace http://DrunkOnJudgement.com to http://dev.DrunkOnJudgement.com any place in any table.
Help me Obi wan Kenobis your my only hope.
You want to do something like this
update table_name
set column_name = replace(column_name, 'http://dev.DrunkOnJudgement.com', 'http://DrunkOnJudgement.com');
this will ensure that you simply replace the text you are looking for in a specific column with the text you want it to be without changing any text around it.
so for example you could just shorten it to something like: replace(column_name, 'dev.DrunkOnJudgment.com', 'DrunkOnJudgment.com')
You can also specify a where clause so that you only replace items that contain that text so something like this:
where column_name like '%dev.DrunkOnJudgement.com%'
Ok to do something like this for all columns in all tables, basically search the entire db. You can use a statement like this:
SELECT Concat('UPDATE ', TABLE_NAME, ' SET ', COLUMN_NAME, ' = REPLACE(', COLUMN_NAME, ',''dev.DrunkOnJudgment.com'',''DrunkOnJudgment.com'')', ' WHERE ', COLUMN_NAME, ' like ''%dev.DrunkOnJudgment.com%''' ) FROM INFORMATION_SCHEMA.COLUMNS
it will output a sql statement like the one above for each column and table in the database and because you are using a replace statement if it does not find the text it does not replace anything it also ensure you only update records that actually contain that text.
So to automate this you would use a cursor, I have not tested the following code but it would look something like this:
DECLARE done BOOLEAN DEFAULT 0;
DECLARE sql VARCHAR(2000);
DECLARE cmds CURSOR
FOR
SELECT Concat('UPDATE ', TABLE_NAME, ' SET ', COLUMN_NAME, ' = REPLACE(', COLUMN_NAME, ',''dev.DrunkOnJudgment.com'',''DrunkOnJudgment.com'')', ' WHERE ', COLUMN_NAME, ' like ''%dev.DrunkOnJudgment.com%''' ) FROM INFORMATION_SCHEMA.COLUMN;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN cmds;
REPEAT
FETCH cmds INTO sql;
PREPARE stmt FROM sql;
EXECUTE stmt;
DROP PREPARE stmt;
UNTIL done END REPEAT;
CLOSE cmds;
Is this as simple as an update statement with a where clause?
update myTable
set myCol = 'http://dev.DrunkOnJudgement.com'
where myCol = 'http://DrunkOnJudgement.com'
At this point in your product's life cycle I guess it is too late to suggest working out a solution that takes away this issue entirely?
For example, not using absolute URLs, or replacing the urls with some variable that is evaluated at runtime?
How can I Select all columns from all tables from the DB, like:
Select * From *
in SQL Server 2008???
The table list it´s very very big, and have so many columns, is it possible to do it without writing the column names?
Or maybe make a select that returns the name of the tables.
This SQL will do this...
DECLARE #SQL AS VarChar(MAX)
SET #SQL = ''
SELECT #SQL = #SQL + 'SELECT * FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES
EXEC (#SQL)
Try this, works fine
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
then you could add
WHERE TABLE_NAME LIKE '' AND COLUMN_NAME LIKE ''
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where t.name = 'ProductItem' AND C.name like '%retail%'
ORDER BY schema_name, table_name
It is possible to retrieve the name of all columns from sys.columns
It is possible to retrieve the name of all table from sys.tables
But is impossible to retrieve all the data from all the tables. As soon as more than one table is involved in a query, a JOIN is necessary. Unless join conditions are provided, tables are joined as full Cartesian product, meaning each row from each table is matched with each row from ll other tables. Such a query as you request would produce for 10 tables with 10 records each no less than 10e10 records, ie. 100 billion records. I'm sure you don't want this.
Perhaps if you explain what you what to achieve, not how, we can help better.
To select * from each table, one after another, you can use the undocumented but well known sp_msforeachtable:
sp_msforeachtable 'select * from ?'
If you are going to send to Excel, I would suggest you use the export wizard and simply select all the tables there. In the object browser, put your cursor on the database name and right click. Chose Tasks - Export Data and follow the wizard. WHy anyone would want an entire database in Excel is beyond me, but that's the best way. If you need to do it more than once you can save the export in an SSIS package.
In SQL Server 2016 Management Studio ( Version: 13.0.15900.1), to get all column names in a specified table, below is the syntax:
**Select name from [YourDatabaseName].[sys].[all_columns]
where object_id=(Select object_id from [YourDatabaseName].[sys].[tables]
where name='YourTableName')**