xp_cmdshell semi-colon separation - sql

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

Related

Error while executing xp_readerrorlog using OPENROWSET in SQL Server - querying remote data sources

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"

Getting error when Executing sp_configure 'max server memory'

When I want to set max server memory, I am getting the bellow error message:
The configuration option 'max server memory' does not exist, or it may
be an advanced option
sql query is as follow:
exec sp_configure 'max server memory', 1024
reconfigure
The error message is pretty clear, it's telling you that's an advanced option
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 1024;
GO
RECONFIGURE;
GO
You may want to read Server Memory Configuration Options
The error is telling you the problem here, it's an advanced setting and you haven't enabled them. A search would have led you to the documentation: Example A. Set the max server memory option to 4 GB.
:
The following example sets the max server memory option to 4 GB. Note
that although sp_configure specifies the name of the option as max
server memory (MB), the example demonstrates omitting the (MB).
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 4096;
GO
RECONFIGURE;
GO
Do you really want to set the Memory to 1GB though? That's nothing for SQL Server to "play" with; you're very likely going to have performance issues.

getting error Could not find stored procedure 'sp_configure'. in sql server

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.

SQL Server CONFIG statement cannot be used inside a user transaction

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.

Resolve hostnames with t-sql

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