2

I want to retrieve the latest date from a SQL Server table.

In an "Execute SQL task" I have the following SQL Statement:

SELECT ? = MAX(MYDATE) --SQL data type of this column is datetime
FROM TBLLOG
WHERE COMPLETED = 1

Under the parameter mapping section I have added 1 output parameter:

  • Variable Name: User:var_testdt (note: this is of type datetime)
  • Direction: Output
  • Date Type: Date
  • Parameter Name: 0
  • Parameter Size: -1

The ResultSet property on the "Execute SQL" task is set to None.

I get this error when executing the package:

... failed with the following error:
Error HRESULT E_FAIL has been returned from a call to a COM component.
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Hadi
  • 36,233
  • 13
  • 65
  • 124
variable
  • 8,262
  • 9
  • 95
  • 215

3 Answers3

1

This is a data type issue.

Based on the SQL Server official documentation, DT_DBTIMESTAMP data type in SSIS is mapped to the datetime and smalldatetime data types in SQL Server. While, DT_DATE is not mapped to any data type.

To solve this problem, just change the output parameter's data type from DATE to DBTIMESTAMP. Besides, make sure that the User:var_testdt variable is also DateTime.

enter image description here

Hadi
  • 36,233
  • 13
  • 65
  • 124
-1

you should do something like below:

DECLARE @ExString NVARCHAR(500) 
SET @ExString = N'SELECT MAX([MYDATE]) AS MaxOfDate FROM TBLLOG
WHERE COMPLETED = 1 '
EXECUTE(@ExString)

Result . . .

AliNajafZadeh
  • 1,216
  • 2
  • 13
  • 22
-1

I use this. Note: @RETORNOERRO is an output parameter.

DECLARE @SQLString nvarchar(max), @ParmDefinition nvarchar(2000)
SET @SQLString = 'IF NOT EXISTS (SELECT 1 FROM DBO.ITEM_PEDIDO ITE (NOLOCK) WHERE COD_PEDIDO = @COD_PED_AVS_RETORNO ) ' +
    'BEGIN ' +
        'SET @RETORNOERRO = ''Dado não inserido. Houve algum erro no insert dos itens, Verifique. Pedido_app: '' + @P02_CODIGO_PEDIDO 
    END';
SET @ParmDefinition = N'@COD_PED_AVS_RETORNO int, @P02_CODIGO_PEDIDO varchar(10), @RETORNOERRO varchar(200) OUTPUT';
EXECUTE sp_executesql @SQLString, @ParmDefinition, @COD_PED_AVS_RETORNO, @P02_CODIGO_PEDIDO, @RETORNOERRO OUTPUT;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77