Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sql server CONFIG statement cannot be used inside a user transaction Expand / Collapse
Author
Message
Posted Friday, August 24, 2012 9:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 9:00 AM
Points: 48, Visits: 235
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
Post #1349778
Posted Friday, August 24, 2012 10:30 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:15 AM
Points: 1,056, Visits: 2,687
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
Post #1349802
Posted Friday, August 24, 2012 3:51 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 PM
Points: 7,097, Visits: 12,598
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
Post #1349961
Posted Friday, August 24, 2012 4:02 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 42,756, Visits: 35,850
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 2008, MVP
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

Post #1349964
Posted Tuesday, August 28, 2012 4:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 9:00 AM
Points: 48, Visits: 235
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.

Post #1350786
Posted Tuesday, August 28, 2012 3:19 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 PM
Points: 7,097, Visits: 12,598
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
Post #1351293
Posted Wednesday, August 29, 2012 2:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 9:00 AM
Points: 48, Visits: 235
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.
Post #1351467
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse