title: Getting pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@Id".')
body: <p>I am trying to execute .sql file from Python3.
  Below is the python code I am trying</p>
  
  <pre><code>import time
  userdate=time.strftime("%m_%d_%H_%M%S")
  import pypyodbc as pyodbc
  db_host='hostname\DBTEST'
  db_name='dbname'
  conn='Driver={SQL Server};Server=' + db_host + ';Database=' +db_name + 
  ';Trusted_Connection=yes;'
  db=pyodbc.connect(conn)
  cursor=db.cursor()
  file=open('C:\\abc\\xyz.sql','r')
  line=file.read()
  sql_cmd=line.split('\n')
  for x in sql_cmd:
     cursor.execute(x)
  </code></pre>
  
  <p>Below is the xyz.sql script</p>
  
  <pre><code>DECLARE @XML XML;
  DECLARE @FileName VARCHAR(1000);
  DECLARE @Id UNIQUEIDENTIFIER 
  SELECT @Id = NEWID()
  SELECT @FileName = 'ggg.xml' 
  SELECT @XML = '&lt;Model&gt;
      ....xml tags here...
      ....        
  
  &lt;/Model&gt;'
  
  IF EXISTS (SELECT * FROM tablename CM WHERE CM.columnname = 'test') BEGIN
      UPDATE CM
          SET CM.pn = '01-00001',
              CM.rev= '06',
              CM.Model = @XML,
              CM.ModifiedOn = GETUTCDATE()
         FROM cm.tablename CM
         WHERE CM.columnname= 'test'
  
      PRINT 'Updated ' + @FileName
  END ELSE BEGIN
      INSERT INTO cm.tablename(cmID, MN, CMType, Description, PN, Rev, CM, 
      RowStatus, ModifiedBy, ModifiedOn)
      SELECT @Id, 'test123', 'abc.1', '', '01-00011', '01', @XML, 'A', 
  '74E8A3E0-E5CA-4563-BD49-12DFD210ED92', GETUTCDATE()
      PRINT 'Inserted ' + @FileName
  END
  </code></pre>
  
  <p>I get below error when I run the python code.</p>
  
  <pre><code>pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL 
  Server Driver][SQL Server]Must declare the scalar variable "@Id".')
  DECLARE @XML XML;
  DECLARE @FileName VARCHAR(1000);
  DECLARE @Id UNIQUEIDENTIFIER 
  SELECT @Id = NEWID()
  </code></pre>
  
  <p>Process finished with exit code 1</p>
  
  <p>Note: If I run the sql query from M/S SQL Management studio (sql server 2016), it runs successfully.
  any help on this would be appreciated.</p>