<?xml version="1.0" encoding="utf-8" ?> 
<TemplateProc>

<Script>
  <![CDATA[
/*
'+-----------------------------------------------------------------------------
'| Procedure: %%ProcName%%
'| Descrição: Adiciona registros para a tabela %%TableName%%
'| Data Criação: %%Date%%
'| Data Últ. Modificação: 
'| Responsável: Forms 2005
'+-----------------------------------------------------------------------------
*/

CREATE PROCEDURE %%ProcName%% 
(
	@%%CampoChave%% INT,
	@%%CampoUsuario%% INT,
	@%%CampoGUID%% VARCHAR(36),
	@%%CampoParentGUID%% VARCHAR(36) = NULL%%CamposDeclare%%
)
AS
  IF @%%CampoChave%% = 0
  BEGIN
    SELECT @%%CampoChave%% = %%CampoChave%% FROM %%TableName%% WHERE GUID = @GUID
    SET @%%CampoChave%% = ISNULL(@%%CampoChave%%, 0)
  END
  
  IF @%%CampoChave%% = 0
  BEGIN
    SET NOCOUNT ON
    IF (SELECT COUNT(*) FROM %%TableName%% WHERE GUID = @GUID) > 0
    BEGIN
      SET @GUID = NEWID()
      INSERT INTO [dbo].[WFLOG_LOGMESSAGE] (LOG_C_CATEGORY,LOG_D_TIMESTAMP,LOG_C_MESSAGE,LOG_N_TYPE,LOG_N_EVENTCODE,LOG_N_EVENTDETAIL,LOG_C_USER,LOG_C_URL,LOG_C_USERADDRESS,LOG_C_AUTHTYPE,LOG_C_SERVERNAME) 
      VALUES ('Duplicated GUID', GETDATE(), '%%TableName%% - Duplicated GUID found', 2, 101000, 0, 'SYSTEM', '', '', '', NULL) 
    END
    INSERT INTO %%TableName%%
      (%%Campos%%)
    VALUES
      (%%Valores%%)
    SET NOCOUNT OFF
    SELECT * FROM %%TableName%% WHERE %%CampoChave%% = @@IDENTITY
  END
	ELSE
  BEGIN
    SET NOCOUNT ON
    UPDATE %%TableName%%
    SET %%ValoresUpdate%% 
    WHERE %%Where%%
    SET NOCOUNT OFF
    SELECT * FROM %%TableName%% WHERE %%CampoChave%% = @%%CampoChave%%
  END
]]></Script>

</TemplateProc>