Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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()
Add Comment
Please, Sign In to add comment