2

I have the following T-SQL codes configured to run on a daily basis using SQL Server Agent job. My database is running on SQL Server 2012.

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=C:\;HDR=YES;FMT=Delimited','SELECT * FROM [myfile.csv]')

SELECT ReservationStayID,NameTitle,FirstName,LastName,ArrivalDate,DepartureDate FROM [GuestNameInfo]

My issue is that the output of this query is being appended to the existing records in the csv file. I want the output to overwrite the existing content each time the SQL Server Agent job is run.

How do I modify my query to acheive this?

1
  • If you use BCP, it will create the file for you each time and you can add the date stamp to the file name as @Jen R has mentioned below.
    – SS_DBA
    Apr 27, 2017 at 13:54

1 Answer 1

5

I would recommend first renaming your existing myfile.csv to something else (like myfile_[DateOfLastRun].csv). Then start fresh with a new myfile.csv. That way if something goes wrong outside this process and you need whatever was in myfile.csv the day/week/month before, you have it.

You could use BCP for this in a BAT file:

set vardate=%DATE:~4,10%
set varDateWithoutSlashes=%vardate:/=-%
bcp "SELECT someColumns FROM aTable" queryout myFile_%varDateWithoutSlashes%.csv -t, -c -T

The example above creates your CSV with the date already in the name. You could also rename the existing file, then create your new myfile.csv without the date:

set vardate=%DATE:~4,10%
set varDateWithoutSlashes=%vardate:/=-%
ren myFile.csv myFile_%varDateWithoutSlashes%.csv
bcp "SELECT someColumns FROM aTable" queryout myFile.csv -t, -c -T

Be sure to build in cleanup of old files somewhere - that can even be done in the same batch process as this one.

You can add DB name and server name to the bcp line - by default it connects to the local server and the user's default DB (See the BCP documentation link for even more options)

bcp databaseName "SELECT someColumns FROM aTable" queryout myFile.csv -t, -c -T -S serverName
9
  • But not just rename it.. keep X occurrences (30 days worth, 30 runs) of the file that way you don't keep putting new files out there without ever cleaning up.
    – xQbert
    Apr 27, 2017 at 13:58
  • Very true @xQbert - we build in cleanup steps as well and I forgot to mention that. Thanks
    – Jen R
    Apr 27, 2017 at 14:02
  • @JenR Thanks for helping out! I understand your suggestion to have the file renamed for each run but I need the file to be overwritten and with the same name as I will have an FTP task in SSIS which will upload this file on an FTP Server on a daily basis. Apr 27, 2017 at 14:56
  • 1
    Look at the second block of code in my example - it has your newest file with the same name always, and the older ones dated. That way if your FTP breaks and you don't immediately notice (maybe you're on vacation), the missed files are still there and you can send them.
    – Jen R
    Apr 27, 2017 at 15:06
  • 1
    I added the syntax for server name and DB name above
    – Jen R
    Apr 28, 2017 at 17:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.