Find a value anywhere in a read-only database - sql-server

A customer's 96GB SQL Server 2014 accounting database has about 1,000 tables, none of which has constraints or fkeys applied or is otherwise documented. I have read only access, with basically other no rights.
A user has sent me a screenshot showing a value that is stored somewhere in the database. The value is "51210000", which might also be stored as a pointer to its entry in the ACCOUNTS table, 323.
I have seen various solutions to searching an entire db, but they invariably use temporary tables, procedures, or other solutions that require write access. Can anyone offer a way to do this read-only?

It's going to be a bit slow, but you can run these a few at a time to see where it may exist
SELECT CONCAT('select * from ', TABLE_NAME, ' where ', COLUMN_NAME, ' = ''51210000''')
FROM INFORMATION_SCHEMA.COLUMNS

Perhaps sp_MSforeachtable
This is certainly NOT fast and I would suggest testing on a smaller database.
Example
Declare #Results table (TableName varchar(500),RowData varchar(max))
Insert Into #Results
EXEC sp_MSforeachtable 'SELECT TableName=''?'' ,RowData = (Select A.* for XML Raw) FROM ? A Where (Select A.* for XML Raw) like ''%51210000%'''
Select *
From #Results
Note:
If 2016+, you may get a little boost by using the JSON alternative.
Replace (Select A.* for XML Raw)
With (Select A.* for JSON Path)
Just for fun
I ran a test looking for "Consulting" on a 17GB database (214 tables). It took 1 minute 30 seconds to return
EDIT - Dynamic SQL Approach
Declare #SQL varchar(max)
Set #SQL=Stuff((Select 'Union All ' +Expr
From (
Select Expr = 'Select Table_Schema='''+Table_Schema+''',Table_Name='''+Table_Name+''',Column_Name='''+Column_Name+''',Value=cast('+quotename(Column_Name)+' as varchar(max)) From '+quotename(Table_Schema)+'.'+quoteName(Table_Name)+' Where '+quotename(Column_Name)+' like ''%Cappe%''||'
From INFORMATION_SCHEMA.COLUMNS
Where Data_Type in ('varchar','int','float','bigint') -- << Set Your Desired Filter
and Table_Name not like 'vw_%' -- << I'd tend to exclude views my prefix vw_
) A
For XML Path ('')),1,10,'')
Set #SQL = replace(#SQL,'||',char(13))
Print #SQL
Exec(#SQL)
Returns

Related

How to properly form an array loop to use in an SQL statement

I am trying to use an array to pull table information. My array build seems to be fine and it is created with the correct number of rows.
THis is what I have so far:
DECLARE #myTableVariable TABLE (name varchar(30))
insert into #myTableVariable SELECT [NAME] FROM SYS.DATABASES
SELECT
TABLE_CATALOG AS 'DATABASE',
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
FROM #myTableVariable.information_schema.columns
I hope my question makes sense! Thanks in advance!
I know there needs to be some sort of WHILE loop around the SQL statement portion, but I am unsure how to properly form it. I have been looking around with no luck.
You can achieve what you're trying to do with a little bit of Dynamic SQL.
In SQL Server I would prefer to use the system DMVs instead of Information_Schema (which really only exist for compatability) however, assuming you have permission to access the database(s) and don't have conflicting collations, the following should help:
declare #Sql nvarchar(max);
with db as (select name from master.sys.databases where database_id > 4)
select #Sql = string_agg(Convert(nvarchar(max), Concat(N'
select
TABLE_CATALOG as [DATABASE],
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
from ', QuoteName(db.name), N'.information_schema.columns')), N' union all ')
from db;
select #Sql;
exec (#Sql);

How can I join tables specified in a separate list of tables in SQL Server? [duplicate]

I have been researching this for a couple of days and feel like I am going around in circles. I have basic knowledge of SQL but there are many areas I do not understand.
I have a table that stores the names and fields of all the other tables in my database.
tblFields
===================================================
TableName FieldName BookmarkName
---------------------------------------------------
Customer FirstName CustomerFirstName
Customer LastName CustomerLastName
Customer DOB CustomerDOB
I want to write a SELECT statement like the following but i am unable to get it work:
SELECT (SELECT [FieldName] FROM [TableName]) FROM tblFields
Is this possible? The application I have developed requires this for user customization of reports.
If i understand what you are trying to do, i think this will help you. It is not pretty and it works for SQL Server 2005 and above, but maybe this is what you are looking for:
declare #tableName nvarchar(100)
declare #sqlQuery nvarchar(max)
declare #fields varchar(500)
set #tableName = 'YourTableName'
set #fields = ''
select #fields = #fields + QUOTENAME(t.fieldname) + ',' from (
select distinct fieldname from tblfields where tablename = #tableName)t
set #sqlQuery = 'select ' + left(#fields, LEN(#fields)-1) + ' from ' + QUOTENAME(#tableName)
execute sp_executesql #sqlQuery
Edit: As Martin suggested, i edited so that the columns and tablename are using QUOTENAME
If I understand correctly what you are trying to do, you are probably better off doing this as two separate queries from your program. One which gets the fields you want to select which you then use in your program to build up the second query which actually gets the data.
If it must be done entirely in SQL, then you will need to tell us what database you are using. If it is SQL Server, you might be able to user a cursor over the first query to build up the second query which you then execute with the sp_executesql stored procedure. But doing doing it outside of SQL would be recommended.

Query on multiple databases (SQL server)

I have multi databases with same structure its name like that "Client1234" the different in numbers beside "client" i have table called "Transactions" inside each database and i want to run query to get count all raws in "transactions" table in all databases.
also when i select database i need to check it has the client word and it has numbers beside the word.
Try to use sp_msforeachdb stored procedure like so:
create table #temp ([rows] int, [client] varchar(100))
exec sp_msforeachdb '
if ''?'' like ''Client%'' and exists(select * from ?.sys.tables t where t.name = ''Transactions'')
begin
insert into #temp select count(*), ''?'' from ?..Transactions
end
'
select * from #temp
drop table #temp
You can use dynamic SQL to create these queries:
select 'select count(*) from ' + name + '.dbo.transactions'
from master..sysdatabases
where name like 'Client%'
and isnumeric(substring(name,6,1))
This would return a result set with each row being a SQL query to count a specific database. It could be consumed by a programming language, used as a cursor, etc.. If you provide more detail I may be able to provide a better example.
When using Fosco's method, it is a good idea to put in brackets [] around the database name:
SELECT 'SELECT count(*) FROM ' + '[' + name + ']' + '.dbo.transactions'
FROM master..sysdatabases
WHERE name like 'Client%' and isnumeric(substring(name,6,1))
If the name and number of the databases you wish to query is not known beforehand then you can only do this by using a dynamic query. You'll need to generate a script like
SELECT COUNT(*) FROM Client1.dbo.Transactions
SELECT COUNT(*) FROM Client2.dbo.Transactions
...
Of course you need to have your appropriate permissions in place for each database.

In sql server, is there any way to check whether the schema change will impact on the stored procs?

In SQL Server, is there any way to check whether the changes in the schema will impact Stored Procedures (and/or Views)?
For example a change of the column name in one table, may break some Stored Procedures; how to check the impacted stored procs?
try using:
EXEC sp_depends 'YourTableName'
and/or
DECLARE #Search nvarchar(500)
SET #Search='YourTableName' --or anything else
SELECT DISTINCT
LEFT(o.name, 100) AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%'+#Search+'%'
ORDER BY 2,1
Use Visual Studio Database Edition for your T-SQL development. It will catch such problems during build, as it creates the deployment .dbschema file.
In SSMS (SQL Server Management Studio) right click on the object you are changing and click on View Dependencies. I don't think this will find references from another database.
You can also look for references in stored procedures if they are not encrypted. You would have to do this in each database you suspect might reference the object you are changing.
select objects.name
,sql_modules.definition
from sys.sql_modules sql_modules
join sys.objects objects on sql_modules.object_id = objects.object_id
where definition like '%some column name%';
I have found nothing that is 100.0000% accurate 100.000000% of the time.
Best way I can think to do this is to abstract your stored procedures from your actual tables using views, and to create those views with a "WITH SCHEMABINDING" clause which should prevent changes that will break your views...
Commercial tools such as Red Gate's SQL Refactor can do this.
I think that recent version of Visual Studio also include this kind of features, but I haven't tried.
To my knowledge, there are no built-in features of Microsoft SQL Server per-se which will do this. Correction: I just read about sp_depends in KM's answer to this post... Note that sp_depends's usage is deprecated; it is replaced by sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities
Also, if the underlying stored procedures use dynamic SQL, the task of detecting dependencies becomes more difficult and prone to "misses".
If you want to change the name of an object or column, then the Smart Rename feature of Red Gate Software's SQL Prompt 5 will generate a script that both performs the rename and updates references to the old name in other objects.
If you're just interested in what depends on a column name, then SQL Prompt 5 also has a Column Dependencies function, where hovering over the column name in a script pops up a window containing a list of objects that refer to the column.
You can download a 14-day trial for free, to see if either of these features works for you.
Paul Stephenson
SQL Prompt Project Manager
Red Gate Software
Have a look at these answers:
Refreshing metadata on user functions t-SQL
SQL Server relationships buried in stored procedures rather than schema
In SQL Server, how can I find everywhere a column is referenced?
How do I find all stored procedures that insert, update, or delete records?
Other than dynamic SQL, using SCHEMABINDING where possible and sp_refreshsqlmodule and sql_dependencies for everything else is very accurate.
If you use SQL Server
You can use this query after your change and find Stored Procedure Or View Or ...
that after your change might get error
USE <Your_DataBase_Name>;
SET NOCOUNT ON;
DECLARE #name NVARCHAR(MAX)
DECLARE #sql NVARCHAR(MAX)
DECLARE #type CHAR(2)
DECLARE #type_desc NVARCHAR(60)
DECLARE #params NVARCHAR(MAX)
DECLARE #tblInvalid TABLE
(
[type_desc] NVARCHAR(60) ,
[name] NVARCHAR(MAX) ,
[error_number] INT ,
[error_message] NVARCHAR(MAX) ,
[type] CHAR(2)
);
DECLARE testSPs CURSOR FAST_FORWARD
FOR
SELECT [name] = OBJECT_NAME(SM.[object_id]) ,
[type] = SO.[type] ,
SO.[type_desc] ,
[params] = ( SELECT (
SELECT CONVERT(XML, ( SELECT STUFF(( SELECT
', ' + [name]
+ '=NULL' AS [text()]
FROM
sys.parameters
WHERE
[object_id] = SM.[object_id]
FOR
XML
PATH('')
), 1, 1, '')
))
FOR XML RAW ,
TYPE
).value('/row[1]', 'varchar(max)')
)
FROM sys.sql_modules SM
JOIN sys.objects SO ON SO.[object_id] = SM.[object_id]
WHERE SO.[is_ms_shipped] = 0
AND SO.[type] = 'P'
OPEN testSPs
FETCH NEXT FROM testSPs INTO #name, #type, #type_desc, #params
WHILE ( ##FETCH_STATUS = 0 )
BEGIN
BEGIN TRY
SET #sql = 'SET FMTONLY ON; exec ' + #name + ' ' + #params
+ '; SET FMTONLY OFF;'
--PRINT #sql;
EXEC (#sql);
END TRY
BEGIN CATCH
PRINT #type_desc + ', ' + #name + ', Error: '
+ CAST(ERROR_NUMBER() AS VARCHAR) + ', ' + ERROR_MESSAGE();
INSERT INTO #tblInvalid
SELECT #type_desc ,
#name ,
ERROR_NUMBER() ,
ERROR_MESSAGE() ,
#type;
END CATCH
FETCH NEXT FROM testSPs INTO #name, #type, #type_desc, #params
END
CLOSE testSPs
DEALLOCATE testSPs
SELECT [type_desc] ,
[name] ,
[error_number] ,
[error_message]
FROM #tblInvalid
ORDER BY CHARINDEX([type], ' U V PK UQ F TR FN TF P SQ ') ,
[name];

OBJECT_ID of object in another database - how to find database ID or name/fully qualified object name?

Example:
USE AnotherDB
-- This works - same ID as from other DB
SELECT OBJECT_ID('AnotherDB.ASchema.ATable')
-- This works
SELECT OBJECT_NAME(OBJECT_ID('AnotherDB.ASchema.ATable'))
USE ThisDB
-- This works - same ID as from other DB
SELECT OBJECT_ID('AnotherDB.ASchema.ATable')
-- Gives NULL
SELECT OBJECT_NAME(OBJECT_ID('AnotherDB.ASchema.ATable'))
Obviously the metadata functions expect a current database. The BOL entries typically have language like this for functions like OBJECT_NAME etc.:
The Microsoft SQL Server 2005 Database
Engine assumes that object_id is in
the context of the current database. A
query that references an object_id in
another database returns NULL or
incorrect results.
The reasons I need to be able to do this:
I can't USE the other database from within an SP
I can't create a proxy UDF stub (or alter anything) in the other databases or in master (or any other database besides my own) to help me out.
So how can I get the database from OBJECT_ID('AnotherDB.ASchema.ATable') when I'm in ThisDB?
My goal is to take a possibly partially qualified name from a configuration table, resolving it in the current context to a fully qualified name, use PARSENAME to get the database name and then dynamic SQL to build a script to be able to get to the meta data tables directly with database.sys.* or USE db; sys.*
You should be able to do this:
SELECT
name
FROM
AnotherDB.sys.objects --changes context
WHERE
object_id = OBJECT_ID('AnotherDB.ASchema.ATable')
This is what you effectively do with OBJECT_ID('AnotherDB.ASchema.ATable')
This means that you could rely on dbname.sys.objects and avoid confusion with metadata functions.
Note: the new Catalog views are designed to be used and not change from version to version, as per the link. In the old days, it was consider bad practice to use system tables but the stigma still remains.
So, you can safely rely on sys.objects rather that the metadata functions.
Do I understand it correctly that you want the db id of AnotherDB?
SELECT *
FROM master..sysdatabases
WHERE name = 'AnotherDB'
Otherwise, you can USE other db's in dynamic SQL if it helps:
DECLARE #SQL NVARCHAR(MAX)
, #objId INT
SET #SQL = N'
USE AnotherDB
SELECT #id = OBJECT_ID(''customer'')
'
EXEC SP_EXECUTESQL #SQL
, N'#id INT OUTPUT'
, #id = #objId OUTPUT
SELECT #objId
OR Execute SP's in other dbs with:
EXEC AnotherDB.dbo.ProcedureName
#paramX = ...
, #paramY = ...
Take a look at the PARSENAME function in TSQL - will allow you to pull out any of the 4-part portions of a fully (or non-fully) qualified name. For the database in your example:
select parsename('AnotherDB.ASchema.ATable',3)
returns:
AnotherDB
select parsename('AnotherDB.ASchema.ATable',2)
returns:
ASchema
If non-fully qualified, you'll get null results if you ask for the portion of a name that isn't included in the string:
select parsename('ASchema.ATable',3)
returns:
NULL
I had the same issue but with OJBECT_SCHEMA_NAME as well. Following on from chadhoc's response using parsename works with OBJECT_NAME like:
DECLARE #OrigTableName NVARCHAR(MAX);
SELECT #OrigTableName = 'AnotherDB.ASchema.ATable'
SELECT OBJECT_NAME(OBJECT_ID(#OrigTableName), DB_ID(PARSENAME(#OrigTableName, 3)))
, OBJECT_SCHEMA_NAME(OBJECT_ID(#OrigTableName), DB_ID(PARSENAME(#OrigTableName, 3)))
I used the following solution:
DECLARE #SchemaName varchar(255) = 'SchemaName'
DECLARE #ObjectName varchar(255) = 'ObjectName'
SELECT o.*
FROM AnotherDB.sys.objects AS o
INNER JOIN AnotherDB.sys.schemas AS s ON o.schema_id = s.schema_id
WHERE s.name = #SchemaName
AND o.name = #ObjectName
IF NOT EXISTS (
SELECT si.name
FROM <DatabaseName>.sys.indexes si
INNER JOIN <DatabaseName>.sys.objects so
ON si.object_id = so.object_id
WHERE si.name = '<IndexName>'
AND so.name = '<TableName>'
)
BEGIN
CREATE INDEX [<IndexName>] ON [<DatabaseName>].[<Schema>].[<TableName>]
([column1])
INCLUDE ([<column2>],,,,
)
WITH (online = ON)
END

Resources