Is there a relatively easy way to find an instance of a character, say a "|" or a "~" or a "&", in a database?
It could be in any field...in any table.
There are around 400 to 500 tables in the database in total.
the easiest way is to bulk export all the data for all of the tables and then search/grep for the string in question.
http://msdn.microsoft.com/en-us/library/ms187042.aspx
You could export the database to one big SQL script using the SQL Management Console. Once you have the text file you can use notepad/grep/etc. to find the characters you're looking for.
This code will identify each table and varchar column in your database. It could be used to generate a select statement for each table and column to search for rows where that column contains the characters you're looking for.
select o.id, o.name, c.name, t.name, t.*
from sysobjects o
join syscolumns c on c.id = o.id
join systypes t on t.xtype = c.xtype
where o.xtype = 'U'
and t.status = 0
and t.name like '%varchar'
order by o.name, c.name
Depending on the size and indexing of your tables this may or may not be a good idea.
Related
My database name is CARE_DynamicsAX and I want to find a column name workerStatus
select * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'workerStatus'
If I'm not wrong, you are trying to find the Table where you have a column name as workerStatus. If that is the case, you might run this query to find the same.
This works for the column names from TABLES for SQL Server.
This query would run under the assumption that you know that the column name that you are searching starts with workerStat
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 'workerStat%'
I understand creating a join and comparing the values of specific columns from two tables. In this case, I am only interested in comparing the columns between two different tables, not the values.
I want to check to see if I have the correct number of columns in my new table, and that the spelling of each column in my new table matches the other. Essentially, a way to QC the schema of the new table.
Any suggestions for a SQL query to execute this?
You can utilize the table below:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = SOME DB
AND TABLE_NAME = SOME TBL
Or you can use sys.columns similarly.
You want to check whether the Data Definition is correct. You cannot do so using a Data Query on those tables.
However, SQL Server stores the information about the tables in the database in other tables. See: https://msdn.microsoft.com/en-us/library/ms186778.aspx
In this case you might get what you want using a query on the INFORMATION_SCHEMA.COLUMNS table.
;WITH cteTable1Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('A')
)
, cteTable2Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('B')
)
SELECT
OBJECT_NAME(c1.object_id) as Table1Name
,c1.name as Table1ColName
,OBJECT_NAME(c2.object_id) as Table2name
,c2.name as Table2ColName
FROM
cteTable1Columns c1
FULL JOIN cteTable2Columns c2
ON c1.name = c2.name
WHERE
c1.object_id is NULL
OR c2.object_id IS NULL
If you use a method like this to identify the columns in each table then do a full outer join on name it will show you all of your problems if any exist. (note you can also use INFORMATION_SCHEMA.COLUMNS)
I have many tables in my database. I want to create a general query which will search for duplicate records in all columns of all the tables in a database in SQL server.
Something like this,
select
T.NAME as TABLE_NAME,
C.NAME as COLUMN_NAME
from
SYS.TABLES as T
inner join
SYS.COLUMNS C on T.OBJECT_ID = C.OBJECT_ID
group by
T.NAME, C.NAME
having
count(*) > 1
I do not know how to do it or if there is any way to do it.
You can find duplicate column names with something like this:
select column_name
from information_schema.columns
group by column_name
having count(*) > 1;
You can join back to column to get the table names. You might also want to limit the search to base tables (it is unclear what you mean by "duplicates").
If you want a quick way to see if two tables are the same you can start with CHECKSUM_AGG and compare the results against two tables. If they have the same columns types and the results of checksum_agg are equal the contents are the same regardless of the row order.
I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
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
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy
I have a huge database with some 100 tables and some 250 stored procedures. I want to know the list of tables affected by a subset of stored procedures. For example, I have a list of 50 stored procedures, out of 250, and I want to know the list of tables that will be affected by these 50 stored procedures. Is there any easy way for doing this, other than reading all the stored procedures and finding the list of tables manually?
PS: I am using SQL Server 2000 and SQL Server 2005 clients for this.
This would be your SQL Server query:
SELECT
[NAME]
FROM
sysobjects
WHERE
xType = 'U' AND --specifies a user table object
id in
(
SELECT
sd.depid
FROM
sysobjects so,
sysdepends sd
WHERE
so.name = 'NameOfStoredProcedure' AND
sd.id = so.id
)
Hope this helps someone.
sp_depends 'StoredProcName' will return the object name and object type that the stored proc depends on.
EDIT: I like #KG's answer better. More flexible IMHO.
I'd do it this way in SQL 2005 (uncomment the "AND" line if you only want it for a particular proc):
SELECT
[Proc] = SCHEMA_NAME(p.schema_id) + '.' + p.name,
[Table] = SCHEMA_NAME(t.schema_id) + '.' + t.name,
[Column] = c.name,
d.is_selected,
d.is_updated
FROM sys.procedures p
INNER JOIN sys.sql_dependencies d
ON d.object_id = p.object_id
AND d.class IN (0,1)
INNER JOIN sys.tables t
ON t.object_id = d.referenced_major_id
INNER JOIN sys.columns c
ON c.object_id = t.object_id
AND c.column_id = d.referenced_minor_id
WHERE p.type IN ('P')
-- AND p.object_id = OBJECT_ID('MyProc')
ORDER BY
1, 2, 3
One very invasive option would be to get a duplicate database and set a trigger on every table that logs that something happened. Then run all the SP's. If you can't do lots of mods to the DB that wont work
Also, be sure to add the logging to existing triggers rather than replace them with logging if you also want tables that the SP's effect via triggers.