0

I have different sql statements(create, insert,delete, update,...) in python that I want to execute them for different databases,and I want to pass database name as a parameter to sql statements. example: query = """ SELECT * FROM [mainDB].[dbo].[customer] """

2
  • 1
    'SELECT * FROM ' + databaseName Commented Aug 3, 2018 at 15:46
  • Replace [mainDB] with a variable name => query = "SELECT * FROM "+ dataBaseName +".[dbo].[customer] " Commented Aug 3, 2018 at 15:51

1 Answer 1

1

you should replace [mainDB] with a variable name in your query => "SELECT * FROM "+ dataBaseName +".[dbo].[customer] "

You can set a list of database name and loop on this list to perform your query

for i, dbName in enumerate(['mydb', 'otherdb', 'thirddb']):
     print("SELECT * FROM ["+ dbName +"].[dbo].[customer];")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.