writing result to a file

  • execution of a query in sql server express and saving the result/output in a doc /pdf file.

  • Anu,

    If you are using SSMSE then you can do the following

    1.) Open SSMSE, connect to SQL express

    2.) Press ctrl+shift+f

    3.) enter the query and execute , then a window will pop-up to save the results to a file

    If you are using command prompt (i.e sqlcmd)

    1.) sqlcmd -Sservername -E -Q"Your query" -o"output file path"

    Pls note, change the servername,query and output path and use appropriate authentication to connect to the server and then press enter.

    You can't directly convert the output to a pdf instead you can save it to a txt file and the convert to pdf.

  • hi Mr. Vidya Sagar,

    Thanks for ur reply .

    But i tried the one what is give by u but i dint get the result.

    can u be more detailed about that .

    Actual requirement is that i want to get the result of a trigger when the particular table is effected to be oppened in .net application for that first iam trying to save the result in the text file the i can try for .net application.

    waiting for the reply

    thanks in advance.

  • Anu,

    You can write the query output to an output file like .txt or .doc etc. by using OSQL or SQLCMD utilities. These are command line utilities used to execute sql queries at command prompt. Try the below one and let me know.

    c:/> SQLCMD -E(trusted connection) –S(servername) -i(iputfile) -o(outputfile)

    eg: c:/> sqlcmd -E -Sxx\abc -id:/query.txt -oresult.txt

    In the above example -i(query.txt) should consists of the query you want to execute and the result will be copied to -o(result.txt) file.

    Instead of using -i(inputfile) you can also write the query here.

    For more options of SQLCMD or OSQL check at command prompt by giving the name. eg: c:/>osql ( provides the entire options used in osql )

    Same process can be followed for OSQL utility also. If any error occurs, please post the error.

    Hope this may help u.

    Cheers,

    Naveen

  • Hi Naveen,

    Thank You for ur reply .

    But what i want is not that.

    My actual requiremtent is to save the result of the query to a text file not from command promt but it must be from sql server express itself.

    Iam having a table on which i created a trigger.if any record is inserted in that table my trigger will fire , through this trigger i have to open the text file and want to save the result in that text file. this must be done when the trigger is fired.

    If u can plzz help me out .

    Thank you ,

    Anujahanvi S.

  • You must first create the textfile with the column headers in it. Below is an example of openrowset.

    Use AdventureWorks

    go

    INSERT OPENROWSET('Microsoft.Jet.OLEDB.4.0',

    'Text;Database=C:\',test#txt)

    --query goes here

    SELECT FirstName

    FROM Person.Contact

    Where ContactID < 50

  • Hi Naveen,

    Can you please help me in this issue..

    How to use the OSQL -b ..

    i didnot got any answer from goole..

    so please help .

  • Google search links to http://msdn2.microsoft.com/en-us/library/aa214012(SQL.80).aspx

    -b

    Specifies that osql exits and returns a DOS ERRORLEVEL value when an error occurs. The value returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity of 10 or greater; otherwise, the value returned is 0. Microsoft MS-DOS® batch files can test the value of DOS ERRORLEVEL and handle the error appropriately.

    Paul Randal
    CEO, SQLskills.com: Check out SQLskills online training!
    Blog:www.SQLskills.com/blogs/paul Twitter: @PaulRandal
    SQL MVP, Microsoft RD, Contributing Editor of TechNet Magazine
    Author of DBCC CHECKDB/repair (and other Storage Engine) code of SQL Server 2005

  • Hi Anu,

    Please follow the instructions below.

    1) open the SQL server,

    2) Connect to the particular DB

    3) Opne the Query Analyzer inSQL 2000 and in 2005 Click on New query tab,

    4) Write your query into query pann,

    5) Press Ctrl + T (This is for result in .txt)

    6) Execute your query,

    7) You will find your query result in txt format.

    8) select all (Ctrl + A)

    9) Press Ctrl + C for copy

    10) Open notepad file and paste (Ctrl + V) and

    11) save it in .txt or .sql for SQL query format.

    Manoj

    MCP,MCTS

    Manoj

    MCP, MCTS (GDBA/EDA)

  • I like that there is always another way of doing things in SQL. and that people are happy to SHARE their knowledge. thanks!

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply