Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Export data from a SQLserver DB to a text file

Author  Topic 

uggers2k
Starting Member

6 Posts

Posted - 2010-10-07 : 12:59:38
Hi All,

I am a absolute newbie to SQL Server, with many years experience in Linux/Oracle.

I have decided to join the SQL Server bandwagon and am producing a simple script to select data from a table and then dump this data to a text file. This script also needs to run automatically at 12pm each day and the daily script that is produced needs to be timestamped. e.g file.07102010.

Using BCP, I have managed to create the text file with the relevant data.

I would like information on:

a) Automatically running the script at a certain time (In Linux, I would just create a Cron job)

b) How I could name the daily created file with a timestamp

Many Thanks





tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-07 : 13:03:29
a) You can schedule it to run via a SQL Agent job. You could also use Windows Task Scheduler, but from the SQL world, we use SQL Agent.
b) I'll work up an example.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-07 : 13:21:51
DECLARE @s varchar(500)

SET @s = 'FileName_'
SET @s = @s + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 120), '-', ''), ' ', ''), ':', '')
SET @s = @s + '.txt'

PRINT @s

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-07 : 13:22:58
I grabbed that REPLACE... code from my backup stored procedure.

Here's the comment I've got in that portion of the code:
-- Get the current date using style 120, remove all dashes, spaces, and colons

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

uggers2k
Starting Member

6 Posts

Posted - 2010-10-07 : 13:54:36
Tara, you are a legend thanks. Will post the full script once tested.

regards
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-07 : 14:05:18
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

uggers2k
Starting Member

6 Posts

Posted - 2010-10-08 : 06:20:25
Hi all,

1 more issue

Enclosed is the procedure I have created to satisy my requirement:

create Procedure myProcedure
as
Begin
Declare @str varchar(1000),
@s varchar(30),
@table varchar(30)
set @table = 'audit'
SET @s = 'c:\temp\File_'
SET @s = @s + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120), '-', ''), ' ', ''), ':', '')
SET @s = @s + '.log'
set @str='Exec Master..xp_Cmdshell ''bcp "Select * from '+db_name()+'..'+@table+'" queryout "'+@s+'" -T -S "'+@@servername+'" -c'''
Exec(@str)
end

My issue is that I need the returned SQL to be in single quotes and each returned attribute to be seperated by a ,
i.e. 'column1','column2','colum3'

Is this possible? Was thinking about installing active perl and creating a script to parse the created file and edit?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-08 : 13:28:10
You should instead use SSIS for this if you want single quotes.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

uggers2k
Starting Member

6 Posts

Posted - 2010-10-10 : 15:15:30
Can you use SSIS to run automatically on a daily basis?

regards
Go to Top of Page

uggers2k
Starting Member

6 Posts

Posted - 2010-10-10 : 16:21:07
Also, this works in sql server

select name +', '+age from details

but changing the procedure (as above) to include this statement, when calling the procedure, it fails with:

Procedure expects parameter 'no_output' of type 'varchar'.

Any reason???

Go to Top of Page

uggers2k
Starting Member

6 Posts

Posted - 2010-10-11 : 11:45:35
All sorted. Installed active Perl, and wrote a Perl script to sort it out. Nice and simple.

I guess creating a procedure was not the way to go to sort this out.

Cheers all
Go to Top of Page
   

- Advertisement -