sql server CONFIG statement cannot be used inside a user transaction

  • I got 'CONFIG statement cannot be used inside a user transaction' when running proc 2 below. Any resolution? Thanks. (sql server 2005)

    proc 1:

    CREATE PROC [dbo].[nz_test1]

    as

    EXEC sp_configure 'show advanced option', 1

    RECONFIGURE WITH OVERRIDE

    EXEC sp_configure 'xp_cmdshell', 1

    EXEC sp_configure 'ad hoc distributed queries', 1

    RECONFIGURE WITH OVERRIDE

    select 1

    proc 2:

    create proc [dbo].[test_nz_tb3]

    as

    create table #t (a varchar(2))

    insert into #t

    exec nz_test1

  • what are you trying to do, are you trying to save the output of configuration sp's in the table and do some manipulations?

    Regards
    Durai Nagarajan

  • Use a return code.

    Zero typically means 'no error' and non-zero typically means 'problem occurred'.

    -- proc 1:

    ALTER PROC [dbo].[nz_test1]

    AS

    BEGIN TRY

    EXEC sp_configure

    'show advanced option',

    1

    RECONFIGURE WITH OVERRIDE

    EXEC sp_configure

    'xp_cmdshell',

    0

    EXEC sp_configure

    'ad hoc distributed queries',

    0

    RECONFIGURE WITH OVERRIDE

    RETURN 0

    END TRY

    BEGIN CATCH

    RETURN 1

    END CATCH

    GO

    --proc 2:

    ALTER PROC [dbo].[test_nz_tb3]

    AS

    DECLARE @return_value INT;

    EXEC @return_value = dbo.nz_test1

    SELECT CASE @return_value

    WHEN 0 THEN 'Success'

    ELSE 'Fail'

    END

    GO

    EXEC [dbo].[test_nz_tb3]

    GO

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Just one point.

    Reconfigure With Override tells SQL not to check that the values you've specified are sensible and valid values. It allows you to set options to values that will cause problems.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for the replies, I was on holiday so didn't come here.

    Actually in my first proc (nz_test1) I just want to make sure to turn on 'xp_cmdshell' and 'ad hoc distributed queries'. Then following queries will generate a result set.

    The second proc (test_nz_tb3) willl then used the result set. So there has to be the

    insert into #t

    exec nz_test1

    to get the result set from the first proc.

    I know if the insert statement is removed then everything is fine.

    So maybe I should put the 'xp_cmdshell' and 'ad hoc distributed queries' code in test_nz_tb3? Or maybe just set these separately (E.g. run it only once as a administration task), since these seem not relevant to the main logic of the proc. But if these are accidently turned off by someone else then the proc will break. I am just wondering what is the best practice. Thanks.

  • nzhang6666 (8/28/2012)


    Thanks for the replies, I was on holiday so didn't come here.

    Actually in my first proc (nz_test1) I just want to make sure to turn on 'xp_cmdshell' and 'ad hoc distributed queries'. Then following queries will generate a result set.

    The second proc (test_nz_tb3) willl then used the result set. So there has to be the

    insert into #t

    exec nz_test1

    to get the result set from the first proc.

    I know if the insert statement is removed then everything is fine.

    You do not need to return a resultset. Did you look at the code I provided showig how to use a return code?

    So maybe I should put the 'xp_cmdshell' and 'ad hoc distributed queries' code in test_nz_tb3? Or maybe just set these separately (E.g. run it only once as a administration task), since these seem not relevant to the main logic of the proc. But if these are accidently turned off by someone else then the proc will break. I am just wondering what is the best practice. Thanks.

    If you're worried and are guarding from someone disabling xp_cmdshell then you have bigger issues than just your proc failing. It means you do not have control over your instance, and that you have little faith in the ones that do have control to check to see if anyone is using the feature before disabling it.

    If you;re that concerned maybe we should discuss the need a bit more. Security Best Practices say not to use xp_cmdshell unless it is absolutely needed. Are you sure you need it? Can what you're trying to do be done some other way without enabling xp_cmdshell, for example with a Windows batch script or PowerShell script?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Hi opc, I saw your code. But actually I was not just seeing the results of the config statements. There are more logic after the config statements, which I didn't put it here because they are not relevant to this problem. And I am just a developer new to my current company, not a DBA, so that's why I want to make sure my code won't break without relying on other people. I ended up putting the config statements in a separate proc (say usp_config). So in my 'outer ' proc, the usp_config is called first then I can run the

    insert into #temp exec 'inner proc'

    Thanks.

Viewing 7 posts - 1 through 6 (of 6 total)

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