Created
January 17, 2018 15:41
-
-
Save orasik/65ceaf33f2a6198ff274991a948d0592 to your computer and use it in GitHub Desktop.
This script will search for a value in all columns in a MS-SQL db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will search for a value in all columns in a MS-SQL db | |
# How to use: | |
# python find_value.py VALUE | |
# It will search for VALUE in every single column in DB and print the table, column and value when found | |
import sys | |
import pymssql | |
try: | |
# Populate your DB credentials | |
conn = pymssql.connect( | |
host="", | |
user="", | |
password="", | |
database="" | |
) | |
except Exception, e: | |
print("Error in connecting to db {}").format(e) | |
exit(0) | |
cursor = conn.cursor() | |
cursor.execute("SELECT t1.name as table_name, t2.name as column_name FROM sys.tables t1 JOIN sys.columns t2 ON t1.object_id = t2.object_id;") | |
rows = cursor.fetchall() | |
for row in rows: | |
try: | |
query = 'SELECT * FROM "{}" WHERE "{}" = \'{}\''.format(row[0], row[1], sys.argv[1]) | |
cursor.execute(query) | |
val = cursor.fetchone() | |
if val != None: | |
print 'Table: {}, Field: {}, Val: {}'.format(row[0], row[1], val) | |
except: | |
continue | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment