Update data if matching value and column in all table - sql

I have a problem on updating value in a column named 'ST_CODE' exists in many tables throughout the database.
I am able to find out the table that contain the column "ST_CODE" with the following codes.
SELECT c.name As ColNames, t.name as TableNames
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name='ST_CODE'
Is it possible to update the value from '00000' to '11000' for all the columns named 'ST_CODE' within all the tables?
Many thanks!

Yes.. create a stored procedure and insert your tablename and column name in a temporary table.
Insert into #temp(SELECT c.name As ColNames, t.name as TableNames
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name='ST_CODE')
Then loop your temp table and write update statement as you needed. Please follow this link for assistance. http://ask.sqlservercentral.com/questions/24259/how-to-update-a-dynamic-sql-table-with-a-set-varia.html

Related

Pass a column field row wise as a table name inside the subquery

select B.name, B.id, count(select * from B.name)
from B
group by name;
How to access the parent query column value inside the sub query as a table name? The table name will be different for all the rows in the table B.
Looking for the syntax if its possible, or any workarounds for this scenario
Assuming B.name is the name of a table you want the count of rows from, you could try the following that gets the rowcount from the sys.partitions DMV
select b.[name], b.Id, p.rows
from b
join sys.tables t on t.[name] = b.[name]
and t.schema_id = Schema_Id('dbo') /* or column with the schema name */
join sys.partitions p on p.object_id = t.object_id
and p.index_id <= 1;

SQL query for finding number of columns

I want to find the number of columns in a table but I want to find them using the LIKE condition.
Is there anyway I can do that?
Thank you very much
Assuming you are using sql server, try the following query:
SELECT COUNT(*)
FROM SYS.COLUMNS c
INNER JOIN sys.tables t ON t.object_id = c.object_id
WHERE t.name like 'TABLE_NAME'
Fill in your like condition instead of "TABLE_NAME"

How do I dynamically join one table to multiple other tables in SQL Server?

I am currently searching my database for all column name's that have a particular substring using the below code.
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
INTO
#Tables
FROM
sys.tables AS t
INNER JOIN
sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%foo%'
ORDER BY
schema_name, table_name;
I want to take the table and column names this query returns and join each of them to another table like below.
SELECT *
FROM #ReferenceTable rt
INNER JOIN #Tables.table_name t ON rt.ReferenceColumn = t.ColumnName
I believe this can be done through a dynamic query, but I haven't been able to figure out how. My initial thought was to pass a table into sp_executesql, but I'm not sure that's possible. What is the correct way to join multiple table names to another table?

Need to find specific column name from database

I have one database on my server I want to filter one column name from the whole database where that column is used.
E.g: If column name is "AlternativeID" is exist in 5 tables and than I want the query that find this column name exists in which tables.?
I find the following query to find specific column name from database.
SELECT * FROM sys.columns WHERE name LIKE '%AlternativeID%'
I hope this is make sense to everyone. Any help will appreciated. Thanks!!
Try this:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%AlternativeID%'
OR...
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%AlternativeID%'
Source : Find all tables containing column with specified name
Hope this helps...
Try:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('AlternativeID')
AND TABLE_SCHEMA='YourDatabaseName';

Locating two columns within a table in an SQL database

I use this query frequently to retrieve all columns matching a string in a database. I am trying to link two tables and to do so I need matching columns to connect the two.
What I want to be able to search for is a table that has a column with the string "CUSTOMER_ID" AND another column with "WORKORDER_BASE_ID"
Is there an easy way to do this, working from this sqript? I thought some type of subquery might be necessary, but I have yet to make anything work.
SELECT
t.name AS table_name
,SCHEMA_NAME(schema_id) AS schema_name
,c.name AS column_name
,T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%CUSTOMER_ID%'
ORDER BY modify_date
Something like this should work:
SELECT t.name AS table_name ,
SCHEMA_NAME(schema_id) AS schema_name ,
c.name AS column_name ,
T.modify_date
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'CUSTOMER_ID' )
AND EXISTS ( SELECT *
FROM sys.columns c1
WHERE c1.object_id = c.object_id
AND c1.name = 'WORKORDER_BASE_ID' )
ORDER BY modify_date