title: how do a merge of sql server with mule?
body: <p>I have done this with a foreach and execute dll operation, but when try insert o uptdate a string  throw a exeption, however with a int values works.</p>
  
  <p>The exeption:`Exception stack is:</p>
  
  <pre><code>1. Invalid column name 'Carlos'. (com.microsoft.sqlserver.jdbc.SQLServerException)
    com.microsoft.sqlserver.jdbc.SQLServerException:217 (null)
  2. Invalid column name 'Carlos'. (com.microsoft.sqlserver.jdbc.SQLServerException). Message payload is of type: LinkedHashMap (org.mule.api.MessagingException)
    org.mule.module.db.internal.processor.AbstractDbMessageProcessor:93 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
  ********************************************************************************
  Root Exception stack trace:
  com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'Carlos'.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:865)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:762)
      at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
      at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:2198)
      at org.mule.module.db.internal.domain.autogeneratedkey.NoAutoGeneratedKeyStrategy.executeUpdate(NoAutoGeneratedKeyStrategy.java:59)
      at org.mule.module.db.internal.domain.executor.UpdateExecutor.doExecuteQuery(UpdateExecutor.java:43)
      at org.mule.module.db.internal.domain.executor.UpdateExecutor.doExecuteQuery(UpdateExecutor.java:37)
      at org.mule.module.db.internal.domain.executor.AbstractSingleQueryExecutor.execute(AbstractSingleQueryExecutor.java:38)
      at org.mule.module.db.internal.processor.ExecuteDdlMessageProcessor.doExecuteQuery(ExecuteDdlMessageProcessor.java:53)
      at org.mule.module.db.internal.processor.AbstractSingleQueryDbMessageProcessor.executeQuery(AbstractSingleQueryDbMessageProcessor.java:42)
      at org.mule.module.db.internal.processor.AbstractDbMessageProcessor.process(AbstractDbMessageProcessor.java:66)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
      at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:107)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.routing.outbound.AbstractMessageSequenceSplitter.processParts(AbstractMessageSequenceSplitter.java:129)
      at org.mule.routing.outbound.AbstractMessageSequenceSplitter.process(AbstractMessageSequenceSplitter.java:59)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
      at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:107)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
      at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:107)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.routing.Foreach.process(Foreach.java:94)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
      at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:107)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.interceptor.AbstractEnvelopeInterceptor.processBlocking(AbstractEnvelopeInterceptor.java:58)
      at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
      at org.mule.processor.AsyncInterceptingMessageProcessor.processNextTimed(AsyncInterceptingMessageProcessor.java:123)
      at o...
  ********************************************************************************
  `
  </code></pre>
  
  <p>'Carlos' is a value from the payload.</p>
  
  <p>This is the DLL</p>
  
  <pre><code>BEGIN MERGE [FP].[Profile] AS DESTINO USING (SELECT #[payload.ID]) AS
  FUENTE (ID) ON (DESTINO.ID=FUENTE.ID) WHEN MATCHED THEN UPDATE SET 
  [UserId]= #[payload.UserId] WHEN NOT MATCHED THEN INSERT 
  (ID,UserId,ExternalId,FirstName) VALUES (#[payload.ID],#
  [payload.UserId],#[payload.ExternalId],#[payload.FirstName]); END
  </code></pre>
  
  <p>The XML Flow:</p>
  
  <pre><code>&lt;flow name="insertprofilesindwFlow"&gt;
      &lt;file:inbound-endpoint path="C:\Users\LBonaventura\Documents\MuleFilesFromDB" responseTimeout="10000" doc:name="Read the file with the profiles" moveToDirectory="C:\Users\LBonaventura\Documents\MuleFilesFromDB" pollingFrequency="864000000"/&gt;
       &lt;dw:transform-message metadata:id="57e39ab0-6c79-4118-a57e-fe6561a04e9b" doc:name="Transform Message  to map"&gt;
          &lt;dw:input-payload doc:sample="list_csv_1.csv"/&gt;
          &lt;dw:set-payload&gt;&lt;![CDATA[%dw 1.0 %output application/java ---payload map ((payload01 , indexOfPayload01) -&gt; {
  
  
  ID: payload01.ID,
  
  ExternalId: payload01.ID as :string,
  UserId: payload01.UserId,
  
  FirstName: payload01.FirstName,
  
  
  LastName: payload01.LastName,
  
  
  
  (DateOfBirth: payload01.DateOfBirth as :string) when payload01.DateOfBirth != "",
  
  
  (DateOfBirth: null) when payload01.DateOfBirth=="",
  
  
  
  (Gender:"M") when  payload01.Gender=="1",
  
  
  (Gender:"F") when  payload01.Gender=="0",
  
  
  (Gender:null) when payload01.Gender=="",
  
  
  
  
  AllowTracking: payload01.AllowTracking,
  
  
  Email: payload01.AlertEmail, 
  
  MainProfile: payload01.Main,
  
  
  Active: payload01.Active as :boolean,
  CreatedOn: payload01.DateAdded as :string,
  UpdatedOn: payload01.DateUpdated as :string,
  
  
  LanguageIso: "ESP",
  
  
  Deleted: false})]]&gt;&lt;/dw:set-payload&gt;
      &lt;/dw:transform-message&gt;
      &lt;foreach doc:name="For Each Profile"&gt;
          &lt;db:execute-ddl config-ref="FOX_DW_DATABASE_CONFIGURATION" doc:name="Upsert in the Database"&gt;
              &lt;db:dynamic-query&gt;&lt;![CDATA[BEGIN MERGE [FP].[Profile] AS DESTINO
  USING (SELECT #[payload.ID]) AS FUENTE (ID)
  ON (DESTINO.ID=FUENTE.ID)
  WHEN MATCHED THEN
      UPDATE SET 
  
          [UserId]= #[payload.UserId],
          [FirstName]=#[payload.FirstName]
  
  
  WHEN NOT MATCHED THEN
  
      INSERT 
          (ID,UserId,ExternalId,[FirstName])
      VALUES
         (#[payload.ID],#[payload.UserId],#[payload.ExternalId],#[payload.FirstName]); END]]&gt;&lt;/db:dynamic-query&gt;
          &lt;/db:execute-ddl&gt;
      &lt;/foreach&gt;
  
  &lt;/flow&gt;
  </code></pre>