SQL Server - access local file from SSMS - sql-server-2012

Is it even possible to access a file in a local drive, copy it, and place the copy in another location? If it is, can anyone show me how?
I am trying to create a job via the SQL Server Agent that copies a file and store it in another location.

Yes, it is possible by using the system procedure xp_cmdshell.
You can use any DOS commands using this procedure!
As an example you can use below command:
/* build copy command */
SET #Cmd = 'COPY "C:\temp\1.bmp" "D:\1.bmp"';
/* execute copy command */
EXEC master.dbo.xp_cmdshell #Cmd;
Note: Because of security risk, this procedure is disabled by default since a few version ago. You need to enable it, but note that this procedure is a security hole if your network is not secure.
Also note that this is just a utility procedure, it might not be good idea to use it in applications except for very special scenarios. If you are going to use it in your application, you may need to rethink about your design.
To enable the xp_cmdshell use below commands:
-- To allow advanced options to be changed.
EXECUTE sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXECUTE sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO

Related

How to write string to a text file inside an ms sql statement

I am trying to write some log data about my queries to a text file. I tried lots of code scripts from the internet but they didn't worked. How can I write strings to a text file?
I tried this one.
I am sure this was asked for couple times in the past and I saw them but could not find a proper answer for it. Thanks
Try this
First of all create the stored procedure rather than alter. see linked below which is used write string to files.
spWriteStringTofile
Now before executing we need to allow permission to execute Ole Automation Procedures. In order to do that copy the below code and execute.
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
Finally execute the below code to store string to file.
execute spWriteStringToFile 'This article describes how to fully access the
local filesystem from SQL Server. It shows a
way of reading and writing data to file, and
accessing the details of the server''s
filesystem using OLE Automation to access
the filesystem object'
, 'D:\Demo','test.txt';
I hope this will help you. If you have any problems or suggestions let me know.

How to enable direct modifications to the system catalogues in sql?

How to change the direct modification of system catalogues in sql ?
In old versions of SQL Server (pre SQL 2005), it was possible to modify system tables directly by turning on the allow updates configuration option and applying using RECONFIGURE WITH OVERRIDE. This option is obsolete in SQL Server 2005 onward. Although it still exists, it is ignored.
You can check the configured value with the command below even though the option has no meaning in modern versions:
EXEC sp_configure 'allow updates';
You "directly" modify the system catalogs by using DDL (data modification language). These are commands that start with commands such as ALTER, CREATE and DROP.
These commands are well documented.
You should not even think about directly changing system tables/views otherwise. They are owned by the database and managed by the database.

Could not find stored procedure "sys.sp_cdc_enable_db_internal"

I'm new to CDC so I started investigating the stored procedures that enable CDC in an SQL Server database. Examining sys.sp_cdc_enable_db and I get the message Could not find stored procedure sys.sp_cdc_enable_db_internal. I've looked all over my database and in the master and msdb databases. Please could anyone tell me why SSMS can't find this stored procedure?
The feature is available only in SQL Server Enterprise and Developer editions, starting with. It can be enabled only using system stored procedures. SQL Server Management Studio provides a wide range of code templates for various feature related actions
To open the templates:
In the SQL Server Management Studio menu, open View
Click Template
Open SQL Server Templates
Open the Change Data Capture sub-folder. The T-SQL templates for administration, configuration, enumeration and meta data querying are available
To set up the feature:
Make sure SQL Server Agent is running. If not, right-click it in Object Explorer and click Start
To enable the feature on the database, open the Enable Database for CDC template in the Configuration sub-folder, and replace the database name with the name of the database you want to track
USE AdventureWorks2012
GO
EXEC sys.sp_cdc_enable_db
GO
The login used must have SQL Server sysadmin privileges and must be a db_owner of the database. Otherwise, you’ll get the following error
sg 22830, Level 16, State 1, Procedure sp_cdc_enable_db_internal, Line
193 The failure occurred when executing the command
‘SetCDCTracked(Value = 1)’. The error returned was 15517: ‘Cannot
execute as the database principal because the principal “dbo” does not
exist, this type of principal cannot be impersonated, or you do not
have permission.’. Use the action and error to determine the cause of
the failure and resubmit the request.
Vist For More Information
https://solutioncenter.apexsql.com/enable-use-sql-server-change-data-capture/

Any performance hit by xp_cmdshell repeatedly enable and disable in a stored procedure?

I am using some dos commands to do file operations & bcp command in my sql server stored procedure. So I am setting xp_cmdshell on and off many times. I am doing this multiple times because, if I just enable once at the start of SP and disables at the end of SP, many times I get the error saying xp_cmdshell not enabled.
SO I am enabling and disabling at each dos command in my SP. By doing this, is there any load on sql server or any performance issues?
Also I want a way to set this xp_cmdshell enabled always. Is there any way to do this?

Receiving Email with SQL Server (SQL 2008R2)

I am trying to get SQL to receive emails, but having some difficulty. Found a blog which starts out with the use of the xp_startmail, but when I try to execute that, I am getting an error. The suggested solution for the error, didn't work...
USE MASTER
GO
SP_CONFIGURE 'show advanced options', 1;
GO
RECONFIGURE;
GO
SP_CONFIGURE 'Database Mail XPs', 1;
GO
RECONFIGURE
GO
EXEC XP_STARTMAIL
GO
Msg 15281, Level 16, State 1, Procedure xp_startmail, Line 1
SQL Server blocked access to procedure 'sys.xp_startmail' of component 'SQL Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'SQL Mail XPs' by using sp_configure. For more information about enabling 'SQL Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.
Any help to get me started would be appreciated....
[EDIT]
**
The only way for SQL Server 2008 to receive email is by using the
legacy stored procedures, such as sp_processmail, with SQL Mail.
Database Mail does not support receiving incoming messages because
there is no IMAP or POP3 support. This may have something to do with
the fact that receiving email can represent a major security risk.
Imagine what a denial-of-service attack on a database cluster could do
to an organization. Or consider the danger of an incoming email
request resulting in the execution of a query such as DROP DATABASE X.
Most SQL Server data is too precious to jeopardize in this manner.
Microsoft has also made it clear that SQL Mail will be phased out in
the next release of SQL Server. Plus, there are many better
alternatives to using this methodology, such as using native Web
services , using .NET CLR-integrated assembly code
**
That's a bit of a nuisance, but makes sense.