Skip to content

Instantly share code, notes, and snippets.

@h3xagn
Last active August 21, 2022 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3xagn/96e58b02db118616f22aed745d942544 to your computer and use it in GitHub Desktop.
Save h3xagn/96e58b02db118616f22aed745d942544 to your computer and use it in GitHub Desktop.
Extract data from SQL Server: https://h3xagn.com
# get list of history DBs
# note: check permission of the user to access 'master' database
query = "set nocount on; print 'Database';select name from sys.Databases where name like 'History%'"
sqlcmd = f'sqlcmd -S {server} -d ProcessDataDB -U {username} -P {password} -Q "{query}" -s "," -h -1 -W -o "./data/{site}_dbs.csv"'
logger.info("-- Getting ProcessDataDB History databases...")
os.system(sqlcmd)
# get list of historic databases
df = pd.read_csv(f"./data/{site}_dbs.csv")
logger.info(f"Found {len(df.index)} history DBs.\n")
# create list of DBs
list_of_dbs = list(df.Database)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment