• Hi

    I think there is no direct command to print the data to a file from t-sql. you have an alternate approach which is little time consuming if you don't mind go ahead with the below steps and reply to me if you fail.

    1) Create a procedure to write the data to a file either .txt and .doc file using the below code and make sure you need to enable the cdmshell as well as OLEAutomation also from the Surface Configuration Area tool in 2005 or use sp_configure procedure to enable the cmdshell to tru and type reconfigure and run both at the same time.Here is the procedure which create a .txt file.

    -------------------------

    CREATE Proc [dbo].[UCreateOrAppendTextFile](@Filename VarChar(100),@Text nVarchar(4000))

    AS

    DECLARE @FileSystem int

    DECLARE @FileHandle int

    DECLARE @RetCode int

    DECLARE @RetVal int

    DECLARE @CreateOrAppend int

    EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject' , @FileSystem OUTPUT

    IF (@@ERROR|@RetCode > 0 Or @FileSystem < 0)

    RAISERROR ('could not create FileSystemObject',16,1)

    EXECUTE @RetCode = sp_OAMethod @FileSystem , 'FileExists', @RetVal out, @FileName

    IF (@@ERROR|@RetCode > 0)

    RAISERROR ('could not check file existence',16,1)

    -- If file exists then append else create

    SET @CreateOrAppend = case @RetVal when 1 then 8 else 2 end

    EXECUTE @RetCode = sp_OAMethod @FileSystem , 'OpenTextFile' , @FileHandle OUTPUT , @Filename, @CreateOrAppend, 1

    IF (@@ERROR|@RetCode > 0 Or @FileHandle < 0)

    RAISERROR ('could not create File',16,1)

    EXECUTE @RetCode = sp_OAMethod @FileHandle , 'WriteLine' , NULL , @text

    IF (@@ERROR|@RetCode > 0 )

    RAISERROR ('could not write to File',16,1)

    EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Close'

    IF (@@ERROR|@RetCode > 0)

    RAISERROR ('Could not close file ',16,1)

    EXEC sp_OADestroy @filehandle

    IF (@@ERROR|@RetCode > 0)

    RAISERROR ('Could not destroy file object',16,1)

    EXEC sp_OADestroy @FileSystem

    ----------------------------------------

    2) run the procedure as below

    exec UCreateOrAppendTextFile 'd:\test.doc','hello'

    3) if you are using the sql 2005 then do som coding using CLR programming to create a procedure which reads the data from the file you created and prints. If you want the legacy COM technology look at the below code to print a file from the sql

    http://www.codeproject.com/KB/reporting-services/Print_from_SQL.aspx

    I think it will make some sense to you.

    Regards

    Srinivas Chilakapati