Tuesday, January 21, 2014

How to Write to a Text File from SQL Server

I just recently posted a video to YouTube discussing how you can actually write to a text file directly from SQL Server.  This is nice because otherwise you would have it do it straight from VB.Net or perhaps C#.
The only caveat to that is when you are tying to automate a process completely from SQ Server and do not wish to involve any other programs.

It covers some tips and tricks but also goes over how you must turn on the permissions in SQL Server for this to work. You must be an admin.

Here is the code to turn on permissions for Ole Automation:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO


SQL SERVER COURSE - 70% OFF FOR OUR READERS

For those of you that would like to broaden your understanding of SQL Server, or if you work with someone who needs a little (or a lot of) help :), We are offering a huge discount to one of our most popular online courses. It is the lead off course which discusses tables - what to do and not do. It is very instructional, practical and straightforward.

Normally this course is $59.00, but for a limited time it is available through this offer for only $18.00!   

SQL SERVER COURSE - SAVE 70% !!

SQL Server FAST TRACK for Novices - Tables

Quickly master intelligent table design and management via SSMS, Queries, TSQL, Scripting and Stored Procedures.   Get Started



Here is a procedure that I wrote to reuse the functionality without having to kep re-writing all of the code:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[WriteToFile]

@File        VARCHAR(2000), 
@Text        VARCHAR(2000)

AS  
  
BEGIN  

DECLARE @OLE INT  
DECLARE @FileID INT  
  

EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT  
        
EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, @File, 8, 1  
      
EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, @Text 
  
EXECUTE sp_OADestroy @FileID  
EXECUTE sp_OADestroy @OLE  
  
END  

       Of course, this does you absolutely no good unless you can invoke the functionality in an easily accessible way. This next three code snippets will go over how to harness the functionality both quickly and easily.

The first example is about as dumbed down as you could make it.

The second and third code snippets will be progressively more sophisticated and robust.


So, here are the three different code examples that you can use to invoke this SQL functionality:

EXEC WriteToFile 'C:\Users\Mertman\Documents\ACME\Log Files\Log.txt','Did it work?'


Or...

DECLARE @Path VARCHAR(2000)
DECLARE @Txt VARCHAR(2000)

SET @Path = 'C:\Users\Mertman\Documents\ACME\Log Files\Log.txt'
SET @Txt = 'Did it work the next time?'

EXEC WriteToFile @Path, @Txt


Or...

DECLARE @Path VARCHAR(2000)
DECLARE @Txt VARCHAR(2000)

DECLARE @YYYY INT
DECLARE @MM INT
DECLARE @DD INT

DECLARE @YYYY_Txt VARCHAR(2000)
DECLARE @MM_Txt VARCHAR(2000)
DECLARE @DD_Txt VARCHAR(2000)

DECLARE @DateStamp VARCHAR(2000)

SET @YYYY = DATEPART(yy,GETDATE())
SET @YYYY_Txt = CAST(@YYYY AS VARCHAR(2000))

SET @MM = DATEPART(mm,GETDATE())
SET @MM_Txt = CAST(@MM AS VARCHAR(2000))

SET @DD = DATEPART(dd,GETDATE())
SET @DD_Txt = CAST(@DD AS VARCHAR(2000))

SET @DateStamp = @YYYY_Txt + '_' + @MM_Txt + '_' + @DD_Txt

SET @Path = 'C:\Users\Mertman\Documents\ACME\Log Files\Log_' + @DateStamp + '.txt'
SET @Txt = 'Did it work?'


EXEC WriteToFile @Path, @Txt


The video is here:



More questions? Check out the following related article How To Write To a Text File from SQL Server


SQL SERVER COURSE - 70% OFF FOR OUR READERS

For those of you that would like to broaden your understanding of SQL Server, or if you work with someone who needs a little (or a lot of) help :), We are offering a huge discount to one of our most popular online courses. It is the lead off course which discusses tables - what to do and not do. It is very instructional, practical and straightforward.

Normally this course is $59.00, but for a limited time it is available through this offer for only $18.00!   

SQL SERVER COURSE - SAVE 70% !!

SQL Server FAST TRACK for Novices - Tables

Quickly master intelligent table design and management via SSMS, Queries, TSQL, Scripting and Stored Procedures.   Get Started


Thursday, January 16, 2014

Table Theory

Tables are the backbone of every database.  This is because data is stored in tables. Tables are made of records, which are made of fields. Each record will have the same 'footprint' meaning that it will have placeholders for each bucket of data rt each 'field'.

Imagine a paper phone book. Each page will have dozens of records usually a name, a phone number and an address, at the very least. Each record has the same 'shape' or 'layout', but the information is different:

Name Address Phone
---------- ----------------- ----------
Joe Shmoe 10 Apple Lane 555-1212
Jack Smith 22 Blue St 555-3456
Sam Sneer 35 Hidden Trail 555-9898

In this case you might have a table named TelephoneNumbers. There would be three fields, a Name field, an Address field and a Phone field.

      As you look at this information, each column of a specific type of data, vertically, would be referred to as a 'field', like the Name field or the address field. Each set of data going across would be an individual record.

In databases it is a good idea to give each table a unique field. This unique field is an id that is really useful to the database but primarily useless to a human being. It helps the database keep track of records just in case two records were identical in every way. This should never happen in real life in a phone book, unless everybody's name is 'Earl', but it can definitely happen in a database.



With a unique identifier the table would be structured something like this:

ID       Name Address Phone
---      ---------- ----------------- ----------
1        Joe Shmoe 10 Apple Lane 555-1212
2        Jack Smith 22 Blue St 555-3456
3        Sam Sneer 35 Hidden Trail 555-9898

By using this new 'key' field the database will not only be able to track specific records but it will also be able to 'relate' the data in this table to records in other tables.

SQL SERVER COURSE - 95% OFF FOR OUR READERS

For those of you that would like to broaden your understanding of SQL Server, or if you work with someone who needs a little (or a lot of) help :), We are offering a huge discount to one of our most popular online courses. It is the lead off course which discusses tables - what to do and not do. It is very instructional, practical and straightforward.

Normally this course is $199.00, but for a limited time it is available through this offer for only $3.00!   Click the link below to get started:



Wednesday, January 15, 2014

Announcing New Upcoming Material

This is to let you all know about the new posts that will be available in the coming weeks. Any kind of blog on TSQL would have to cover the basics of table creation. In addition, we will be covering simple TSQL queries as well as related TSQL queries and then finally stored procedures and functions:

1. Table Creation
2. Related Data
3. Simple TSQL queries
4. Related TSQL queries
5. Stored Procedures
6. Functions

The first four sections will really be in anticipation of the last two, Stored Procedures and Functions.

Stored Procedures are the most important part of database programming in my opinion. The reason why is this: If you have an app (phone/web/EXE) that is compiled and references a stored procedure in a database, you can always altered the stored procedure without having to re-compile your application. This is a huge benefit, but the import can be lost on even the sharpest of developers if they have not had a lot of experience with using stored procedures.

SQL SERVER COURSE - 95% OFF FOR OUR READERS

For those of you that would like to broaden your understanding of SQL Server, or if you work with someone who needs a little (or a lot of) help :), We are offering a huge discount to one of our most popular online courses. It is the lead off course which discusses tables - what to do and not do. It is very instructional, practical and straightforward.

Normally this course is $199.00, but for a limited time it is available through this offer for only $3.00!   Click the link below to get started: