title: Trying to execute SQL Server command in pyodbc but getting error
body: <p>I have been trying to execute some SQL commands  thought python script using pyodbc but getting the following error.</p>
  <p>Script code for Reference</p>
  <pre><code>conn_info = f&quot;DRIVER={driver};SERVER={server2};DATABASE=master;Trusted_Connection=yes;UID={uname2};PWD={passwd2}&quot;
          cnct_str = pyodbc.connect(conn_info, autocommit=True)
          query = &quot;&quot;&quot;CREATE USER {%s} WITH PASSWORD = '{%s}';&quot;&quot;&quot;% ((user_create,user_pass))
          print(&quot;&lt;-----random generated passowrd is-----&gt;&quot;)
          print(user_pass)
          cur = cnct_str.cursor().execute(query)
          cur.commit()
          while cur.nextset():
              pass
      def post_restore2():
          conn_info = f&quot;DRIVER={driver};SERVER={server2};DATABASE=master;Trusted_Connection=yes;UID={uname2};PWD={passwd2}&quot;
          cnct_str = pyodbc.connect(conn_info, autocommit=True)
          query2 = &quot;&quot;&quot;USE [{%s}] 
          CREATE USER {%s} WITH PASSWORD = '{%s}';
          GO
          EXEC sp_addrolemember N'db_owner', N'{%s}'
          GO&quot;&quot;&quot;% ((dba_name,user_create,user_pass,user_create))
          cur = cnct_str.cursor().execute(query2)
          while cur.nextset():
              pass
  </code></pre>
  <p>error output:</p>
  <pre><code>  Traceback (most recent call last):
    File &quot;C:\Users\piyus\OneDrive\Desktop\sql_server_backup_restore_script.py&quot;, 
    line 129, in &lt;module&gt;
    post_restore1()
    File &quot;C:\Users\piyus\OneDrive\Desktop\sql_server_backup_restore_script.py&quot;, 
    line 112, in post_restore1
    cur = cnct_str.cursor().execute(query)
     pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native 
    Client 11.0]Syntax error, permission violation, or other nonspecific error 
    (0) (SQLExecDirectW)')
  </code></pre>