execute SQL server query from Dos command prompt/ batch file.

  • Hi All,

    I need to wirte a batch file, which connects to SQL server, executes a simple Query and output the result to a text file.

    in the result if we are having 3 cloumns c1, c2, c3 insted of getting 3 vertical columns the result sud be as

    c1 item1

    c2 item1

    c3 item1

    c1 item2

    c2 item2

    c3 item2

    c1 item3

    c2 item3

    c3 item3

    Your help will be well appriciated.

    THanks & Regards,

    Sai.K.k

  • Look at the osql and isql command line utilities.

  • And since this is 2005, the sqlcmd command line utility as well.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • He is correct - use SQLCmd (it is the new version - I forgot I was in the 2005 forum).

    Books Online has this well documented.

  • Yes, use SQLCMD. However, if you don't like the output format that SQLCMD provides and/or you want to massage the data a bit before writing to the file, the next step up would be a quick VBScript (executed by CSCRIPT) using the SQL Server Provider for OLE-DB.

    I do it all the time to produce delimited (e.g., tab, pipe, etc.) flat files as well as to extract BLOBs (former TEXT and IMAGE data types) out as separate files in the file system.


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • Hi,

    I am new to SQL infact.

    Is that SQLCMD comes with sql server 2005, if so I need another solution.

    We use SQL server2000 and our clients cant shift from this. Prior to sql server 2005 and SQLCMD there might be an option of executing queries from CMD.

    IF sou can you help me in that.

    Thank & regards,

    Sai krishna.k

  • For SQL Server 2000, use OSQL, which is a replacement for ISQL. See the Books Online for details about the utility and examples.


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • I wonder why nobody has mentioned anything about BCP???

    --Ramesh


  • to run the SQL use SQL Command, but use the following to format the data the way you requested

    DROP TABLE [#testtable]

    GO

    SELECT [name],

    [object_id] AS c1,

    principal_id AS c2,

    [schema_id] AS c3

    INTO #testtable

    FROM sys.tables

    SELECT NAME,Column_name,ItemValue FROM (SELECT *

    FROM [#testtable]) AS a

    UNPIVOT (ItemValue FOR Column_name IN (c1,c2,c3)) AS unpvt

    ORDER BY 1,2

  • I had a somewhat related question.

    I want to use a SQL command to write a string to a text file using Master...xp_cmdshell.

    essentially clobbering the contents.

  • A script of how to do that can be found here

    http://www.databasejournal.com/features/mssql/archives.php

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

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