Using sp_ExecuteSQL

  • Hello,

    If I create some dynamic SQL and execute it using sp_ExecuteSQL, and the SQL I created generates an error message, is there any way I can trap this?

    Thanks.

  • Use Output parameter:

    DECLARE @SQL NVARCHAR(4000);

    DECLARE @ParameterDefinition NVARCHAR(4000);

    -- Set value for Parameter variable

    SELECT@ParameterDefinition = '

    @top INT,

    @acctnum INT,

    @ErrorOutput INT OUTPUT,

    @ROWCOUNTOutput INT OUTPUT'

    -- Set value for the Sql String

    SELECT @SQL = 'Select top (@top) * from acct where acctnum > @acctnum; SELECT @ErrorOutput = @@Error,@ROWCOUNTOutput = @@ROWCOUNT'

    EXEC sp_executeSQL

    @SQL,

    @ParameterDefinition,

    @top = 10,

    @acctnum = 400975,

    @ErrorOutput = 0 ,

    @ROWCOUNTOutput = 0

  • Use TRY.. CATCH

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Great stuff chaps

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply