Listing 1: Stored Procedure That Converts a .csv File to an Excel File CREATE PROCEDURE Util_CSV_to_Excel @UserName varchar(35) , @TargetName AS varchar(255), @SaveAsName AS varchar(255), @TargetTable AS varchar(255), @JobName AS varchar(255) AS /* This code inserts a header row into the CSV file and saves it as XLS for linkedserver attached variables (you could store the script code in a table). */ DECLARE @NewJobID AS uniqueidentifier DECLARE @NewJobName AS sysname DECLARE @NewDescription AS nvarchar(512) DECLARE @ScriptCode AS nvarchar(3200) DECLARE @FieldCount AS integer /* Note that the Initial Catalog in the script could come in as a parameter if this procedure is talking to multiple databases. */ SET @NewJobName = @JobName + '_' + @UserName SET @NewDescription = 'Excel Temp Job FOR ' + @UserName -- Get table schema (for header construction). SELECT @FieldCount = count(*) FROM information_schema.columns WHERE table_name = @TargetTable /* The whole script goes into this nvarchar(3200): VBScript requires these carriage returns. Don't use single quotes in body of script unless you use them in pairs. The best place to test and debug a script is in a SQL Server Agent job step. After testing the script, paste it here and add the necessary variables and quotes. Set up the script code. */ SET @ScriptCode = 'Dim xlApp, xlBook, xlSheet dim cn, rs, cmdText, t, FldName SET xlApp = CreateObject("excel.application") SET xlBook = xlApp.Workbooks.Open("' + @TargetName + '", 0, False, 2) SET xlSheet = xlBook.Worksheets(1) xlApp.DisplayAlerts = False xlSheet.Name = "' + @TargetTable + '" xlApp.ActiveCell.EntireRow.Insert SET cn = CreateObject("ADODB.Connection") SET rs = CreateObject("ADODB.Recordset") cn.Provider = "sqloledb" cn.Properties("Data Source").Value = "." cn.Properties("Initial Catalog").Value = "MYDATA" cn.Properties("Integrated Security").Value = "SSPI" cn.Open cmdText = "SELECT table_name, column_name, ordinal_position FROM information_schema.columns WHERE table_name = ''' + @TargetTable + ''' ORDER BY ordinal_position" rs.Open cmdText, cn FOR t = 1 TO ' + cast(@FieldCount AS Varchar(4)) + ' FldName = rs.Fields("column_name") xlSheet.Cells(1, t).Value = FldName rs.MoveNext NEXT rs.Close cn.Close xlBook.SaveAs "' + @SaveAsName + '", 43 xlBook.Close xlApp.quit SET rs = Nothing SET cn = Nothing SET xlSheet = Nothing SET xlBook = Nothing SET xlApp = Nothing' -- Create job. -- The @delete_level variable determines whether the job is permanent or temporary. EXEC msdb..sp_add_job @job_name=@NewJobName, @enabled=0, @description=@NewDescription, @start_step_id=1, @category_name=NULL, @owner_login_name='sa', @notify_level_eventlog=2, @delete_level=1, @job_id=@NewJobId output -- Add job server. EXEC msdb..sp_add_jobserver @job_name=@NewJobName -- Add job step. EXEC msdb..sp_add_jobstep @job_name=@NewJobName, @step_id=1, @step_name='Create Excel File', @subsystem='ActiveScripting', @command=@ScriptCode -- Set the scripting language. UPDATE msdb.dbo.sysjobsteps SET database_name = 'VBScript' WHERE job_id = @NewJobId -- Run job. EXEC msdb..sp_start_job @job_name=@NewJobName