I got
CONFIG statement cannot be used inside a user transaction
when running procedure 2 below. Any resolution? Thanks.
Procedure #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
Procedure #2:
create proc [dbo].[test_nz_tb3]
as
create table #t (a varchar(2))
insert into #t
exec nz_test1
Apparently (to me, anyway), if you are told that you can't do what you are trying to do, you need to change your code to avoid doing what you aren't allowed to. In particular, you likely need to move the configuration/reconfiguration code outside the procedure you are calling in the INSERT statement (the nz_test1 one), to another stored procedure, for instance. Run that code separately, probably before the insert (that may depend on what you are trying to achieve with that reconfiguration, which you aren't revealing).
So, something like this, perhaps:
CREATE PROCEDURE dbo.my_config
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;
GO
CREATE PROCEDURE dbo.nz_test1
AS
SELECT 1 AS Value;
GO
CREATE PROCEDURE dbo.test_nz_tb3
AS
EXEC dbo.my_config;
CREATE TABLE #t (a varchar(10));
INSERT INTO #t
EXEC dbo.nz_test1;
Make sure you do not call test_nz_tb3 within a transaction either. Otherwise you'll need to call my_config outside test_nz_tb3, likely before the transaction where the latter is called.
Related
Can you please help me to fix this query?
SELECT
FROM OPENROWSET('SQLNCLI',
'Server=.;Trusted_Connection=Yes;',
'SET FMTONLY OFF;EXEC master.sys.xp_readerrorlog')
I get this error:
Msg 11519, Level 16, State 1, Procedure sys.sp_describe_first_result_set, Line 1 [Batch Start Line 12]
The metadata could not be determined because statement 'EXEC master.sys.xp_readerrorlog' invokes an extended stored procedure.
I have enabled Ad-Hoc Distributed Queries as well by using below query but getting same error.
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
You cannot use OPENROWSET for an extended stored procedure. You could create a linked server
EXEC {Your Linked Server Name}.master.sys.xp_readerrorlog;
But you say you have 120 instances. (Quite why you don't have management software for so many, is another question...) So you are better off using Powershell to do this. For example
Get-SqlErrorLog
-Since LastMonth
-ServerInstance "your","Server","Instances","Here"
You would probably want other code to filter and group the results, but you haven't shown what you want.
For example you could do something like this
Get-Content "ServerInstances.txt"
| Get-SqlErrorLog -After "2022-09-16 10:00:00"
| Where Source -eq "Backup"
I need to change degree of parallelism, but when i run that i am getting error
Could not find stored procedure 'sp_configure'.
Here is my script for that
USE test;
GO
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
EXEC sp_configure 'max degree of parallelism', 8;
GO
RECONFIGURE WITH OVERRIDE;
GO
It seems you are using Azure SQL Database where sp_configure is not available. Use ALTER DATABASE SCOPED CCONFIGURATION instead to configure MAXDOP.
I am using xp_cmdshell and i want the output to the text file to be semi-colon separated. I have tested the following:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1; -- 1 for at enable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
-- Extracting information from the database
EXEC xp_cmdshell 'bcp "SELECT TcpIpAddress FROM [SIT-DVH].[dbo].[Preb_Idera]" queryout "C:\Output\Ip_outputSemi.txt" -T -c -t;'
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To disable the feature.
EXEC sp_configure 'xp_cmdshell', 0; -- 0 for at disable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
I have seen several places saying that setting -t; should make the output to be semi-colon separated, however the output is still:
xxxx
yyyy
zzzz
I found the solution, since the output is as followed:
xxx
zzz
yyy
i needed to change -t; to -r; since i only have one field per row and [-r row_term], the output is as followed:
xxx;zzz;yyy
How can i resolve a hostname in t-sql? a 2000 compatible method is preferred. Although something that works on 2005/2008 would also be helpful.
eg. If i have the hostname stackoverflow.com i want to return 69.59.196.211
Well, I suppose you could use xp_cmdshell to execute nslookup and parse the results. Seems like a really awkward thing for SQL Server to be doing, though.
exec master..xp_cmdshell 'nslookup intel.com'
.. then you'll probably want to stuff that in a temp table and walk through the results.
You could also, if you can get access to SQL Server 2005 or 2008, build a stored procedure or function in .NET and do a simple call to Dns.GetHostAddresses().
SQL Server may block access to procedure 'sys.xp_cmdshell' as part of the security configuration. As a system administrator you can enable the use of 'xp_cmdshell' as follows:
-- Allow advanced options.
EXEC sp_configure 'show advanced options', 1;
GO
-- Update the currently configured value for advanced options.
RECONFIGURE;
GO
-- Then, enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- Update the currently configured value for this feature.
RECONFIGURE;
GO
-- Then you can go ahead and run ...
exec master..xp_cmdshell 'nslookup microsoft.com'
GO
How is it possible to run a stored procedure when SQL Server Express Edition starts?
Use the system stored procedure sp_procoption to define the stored procedure you wish to be executed at SQL Server Service startup.
exec sp_procoption
#ProcName = 'procedureName',
#OptionName = 'startup',
#OptionValue = 'true'
USE master;
GO
-- first set the server to show advanced options
EXEC sp_configure 'show advanced option', '1';
RECONFIGURE
-- then set the scan for startup procs to 1
EXEC sp_configure 'scan for startup procs', '1';
RECONFIGURE
IF OBJECT_ID('spTest') IS NOT NULL
DROP PROC spTest
GO
-- crate a test stored procedure
CREATE PROC spTest
AS
-- just create a sample database
EXEC('CREATE database db1')
GO
-- set it to run at sql server start-up
exec sp_procoption N'spTest', 'startup', 'on'