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

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.

Related

SQL Server - access local file from SSMS

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

Saving SQL Queries in SAP

Hey guys I've written some SQL statements in SAP, I want to be able to save the code and run it when ever I need to. The SQL code either locks or unlocks specified users based on the UFLAG value entered. I'm fairly new to SAP/SQL and have no idea on how to actually save this small/simple script.
I used TCODE: db13 then navigated to the Diagnostics tab -> SQL Command Editor.
Any help would be appreciated, thanks!
As far as I know, sql scripts cannot be saved in the system. However, you can export to txt and import from txt file.
In my opinion, it is not a proper way to handle this at db layer. You should write a program and should use open sql statements.
In Eclipse(ADT) you can save them as favourites in SQL console. There is a downward arrow in RUN, click on the same will give you options to 'add to favourites', manage favourites etc.

How to create multiple stored procedures from .sql files

I have about 100 stored procedures files that I want to add to a new database that I created. Is there any way I can just drop them in a folder to import them? When I tried that, like how you would with windows explorer, it just opened them all in query windows.
I am using SQL Server Management Studio.
Right now you did not respond to the question in the comments to make your question clearer, but I have these two suggestions
Assuming you already have a database that uses these procedures:
Right click the database in Management Studio and click on Task
Click on Generate Scripts
In the dialog that comes up, click next as you specify the right options
Select the checkbox for "Stored Procedures"
Select All the required stored procedures
Select option to generate script to a new window or file
You are done. Run you script on the other database
If you have access to Visual Studio
Create a new SQL Server Database project
Select the connection string of your new database
Create a stored procedure folder and add all the stored procedure files from your system to it
Right click the project and click Deploy/Publish
Hope you have the tables referenced in the stored procedures

Visual Foxpro SQL Server - Can't find the Call to SQL server in Foxpro

My DBA's are saying my foxpro application or .DBC (Database container) are hitting SQL server but searching all the code can't find the SQL call (FMTONLY ON/OFF).
This is the SQL command being sent:
FMTONLY ON/OFF
Getting called 16260 times every few minuets?
Any ideas how to find this or what could be causing it, maybe my DBC file?
If you can't find it embedded in the .DBC, but not entirely sure its NOT in there, you can use a VFP tool to dump its contents to a .prg file... GENDBC which is in your installation folder of {VFP}\Tools\GenDBC\GenDBC.prg
Open your database, then run that program, it will cycle through all the tables, indexes, relations, connections, etc and generate the code corresponding to everything in it... You could then look at the output .prg file and see if something in there might be triggering what you can't see otherwise.

How to get script of SQL Server data? [duplicate]

This question already has answers here:
What is the best way to auto-generate INSERT statements for a SQL Server table?
(24 answers)
Closed 6 years ago.
I'm looking for a way to do something analogous to the MySQL dump from SQL Server. I need to be able to pick the tables and export the schema and the data (or I can export the schema via SQL Server Management Studio and export the data separately somehow).
I need this data to be able to turn around and go back into SQL Server so it needs to maintain GUIDs/uniqueidentifiers and other column types.
Does anyone know of a good tool for this?
From the SQL Server Management Studio you can right click on your database and select:
Tasks -> Generate Scripts
Then simply proceed through the wizard. Make sure to set 'Script Data' to TRUE when prompted to choose the script options.
SQL Server 2008 R2
Further reading:
Robert Burke: SQL Server 2005 - Scripting your Database
SQL Server Management Studio
This is your best tool for performing this task. You can generate a script that will build whichever tables you wish from a database as well as insert the data in those tables (as far as I know you have to export all of the data in the selected tables however).
To do this follow these steps:
Right-click on your database and select Tasks > Generate Scripts
In the Generate and Publish Scripts wizard, select the "Select specific database objects" option
Expand the "Tables" tree and select all of the tables you wish to export the scheme and data for, then click Next
In the next screen choose how you wish to save the script (the Output Type must remain set as "Save scripts to a specific location"), then click the Advanced button in the top right corner
In the newly opened window, under the General section is a setting called "Types of data to script", set this to "Scheme and data" and click OK
Click Next, review the export summary and click Next again. This will generate the script to your selected destination.
To restore your database, simply create a new database and change the first line of your generated script to USE [Your.New.Database.Name], then execute. Your new database will now have all of the tables and data you selected from the original database.
I had a hell of a time finding this option in SQL Management Studio 2012, but I finally found it. The option is hiding in the Advanced button in the screen below.
I always assumed this contained just assumed advanced options for File generation, since that's what it's next to, but it turns out someone at MS is just really bad at UI design in this case. HTH somebody who comes to this thread like I did.
If you want to script all table rows then
Go with Generate Scripts as described by Daniel Vassallo. You can’t go wrong here
Else
Use third party tools such as ApexSQL Script or SSMS Toolpack for more advanced scripting that includes some preprocessing, selective scripting and more.
Check out SSMS Tool Pack. It works in Management Studio 2005 and 2008. There is an option to generate insert statements which I've found helpful moving small amounts of data from one system to another.
With this option you will have to script out the DDL separately.
SqlPubWiz.exe (for me, it's in C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2>)
Run it with no arguments for a wizard. Give it arguments to run on commandline.
SqlPubWiz.exe script -C "<ConnectionString>" <OutputFile>
BCP can dump your data to a file and in SQL Server Management Studio, right click on the table, and select "script table as" then "create to", then "file..." and it will produce a complete table script.
BCP info
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=319
http://msdn.microsoft.com/en-us/library/aa174646%28SQL.80%29.aspx
I know this has been answered already, but I am here to offer a word of warning.
We recently received a database from a client that has a cyclical foreign key reference. The SQL Server script generator refuses to generate the data for databases with cyclical references.