title: SQL Server 2005 / XML Stored Proc - Unicode to ascii? (Exception 0xc00ce508)
body: <p>I have an MSSQL2005 stored procedure here, which is supposed to take an XML message as input, and store it's content into a table.
  The table fields are varchars, because our delphi backend application could not handle unicode.
  Now, the messages that come in, are encoded ISO-8859-1. All is fine until characters over the > 128 standard set are included (in this case, ÄÖäö, which are an integral part of finnish). This causes the DB server to raise exception 0xc00ce508.
  The database's default, as well as the table's and field's, collation is set to latin1, which should be the same as ISO-8859-1.</p>
  
  <p>The XML message is parsed using the XML subsystem, like so:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[parse] @XmlIn NVARCHAR(1000) AS
  SET NOCOUNT ON
  DECLARE @XmlDocumentHandle INT
  DECLARE @XmlDocument VARCHAR(1000)
  BEGIN
  SET @XmlDocument = @XmlIn
  EXECUTE sp_xml_preparedocument @XmlDocumentHandle OUTPUT, @XmlDocument
  BEGIN TRANSACTION
  //the xml message's fields are looped through here, and rows added or modified in two tables accordingly
  // like ...
  DECLARE TempCursor CURSOR FOR
  SELECT AM_WORK_ID,CUSTNO,STYPE,REFE,VIN_NUMBER,REG_NO,VEHICLE_CONNO,READY_FOR_INVOICE,IS_SP,SMANID,INVOICENO,SUB_STATUS,TOTAL,TOTAL0,VAT,WRKORDNO
  FROM OPENXML (@XmlDocumentHandle, '/ORDER_NEW_CP_REQ/ORDER_NEW_CUSTOMER_REQ',8)
  WITH (AM_WORK_ID int '@EXIDNO',CUSTNO int '@CUSTNO',STYPE VARCHAR(1) '@STYPE',REFE VARCHAR(50) '@REFE',VIN_NUMBER VARCHAR(30) '@VEHICLE_VINNO',
  REG_NO VARCHAR(20) '@VEHICLE_LICNO',VEHICLE_CONNO VARCHAR(30) '@VEHICLE_CONNO',READY_FOR_INVOICE INT '@READY_FOR_INVOICE',IS_SP INT '@IS_SP',
  SMANID INT '@SMANID',INVOICENO INT '@INVOICENO',SUB_STATUS VARCHAR(1) '@SUB_STATUS',TOTAL NUMERIC(12,2) '@TOTAL',TOTAL0 NUMERIC(12,2) '@TOTAL0',VAT NUMERIC(12,2) '@VAT',WRKORDNO INT '@WRKORDNO')
  OPEN TempCursor
  FETCH NEXT FROM TempCursor
  INTO @wAmWork,@wCustNo,@wType,@wRefe,@wVIN,@wReg,@wConNo,@wRdy,@wIsSp,@wSMan,@wInvoNo,@wSubStatus,@wTot,@wTot0,@wVat,@wWrkOrdNo
  // ... etc
  COMMIT TRANSACTION
  EXECUTE sp_xml_removedocument @XmlDocumentHandle
  END
  </code></pre>
  
  <p>Previously, the stored procedure used to use nvarchar for input, but since that caused problems with the ancient backend application (Delphi 5 + ODBC), we had to switch the fields to varchars, at which point everything broke.</p>
  
  <p>I also tried taking in nvarchar and converting that to varchar at the start, but the result is the same.</p>