SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



writing result to a file Expand / Collapse
Author
Message
Posted Tuesday, November 13, 2007 5:53 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, January 06, 2008 9:01 PM
Points: 33, Visits: 89
execution of a query in sql server express and saving the result/output in a doc /pdf file.
Post #421519
Posted Tuesday, November 13, 2007 6:05 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, November 11, 2009 6:26 AM
Points: 391, Visits: 425
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.


Regards..Vidhya Sagar
Post #421526
Posted Tuesday, November 13, 2007 11:01 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, January 06, 2008 9:01 PM
Points: 33, Visits: 89
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.
Post #421904
Posted Wednesday, November 21, 2007 3:39 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, August 25, 2008 1:32 PM
Points: 52, Visits: 34
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
Post #424505
Posted Wednesday, November 21, 2007 9:43 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, January 06, 2008 9:01 PM
Points: 33, Visits: 89
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.
Post #424813
Posted Wednesday, November 28, 2007 12:18 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, November 17, 2009 3:58 PM
Points: 2,091, Visits: 2,460
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





My blog: http://jahaines.blogspot.com
Post #427031
Posted Monday, March 31, 2008 12:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 11, 2008 7:33 AM
Points: 1, Visits: 6
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 .
Post #476792
Posted Monday, March 31, 2008 12:47 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:21 PM
Points: 1,425, Visits: 1,029
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
Managing Director, www.SQLskills.com
Blog:www.SQLskills.com/blogs/paul Twitter: @PaulRandal
SQL MVP, Microsoft RD, Contributing Editor of TechNet Magazine
Author of DBCC CHECKDB/repair for SQL Server 2005
Post #476796
Posted Wednesday, April 09, 2008 8:19 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, February 04, 2009 7:34 AM
Points: 129, Visits: 130
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)
Post #482369
Posted Wednesday, August 26, 2009 5:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 05, 2009 9:51 PM
Points: 1, Visits: 12
I like that there is always another way of doing things in SQL. and that people are happy to SHARE their knowledge. thanks!
Post #777972
« Prev Topic | Next Topic »


Permissions Expand / Collapse