How to use TAB as column separator in SQLCMD

后端 未结 11 1216
后悔当初
后悔当初 2020-12-16 17:36

SQLCMD supports the -s parameter to specify the column separator, but I couldn\'t figure how how to represent the tab (CHAR(9)) character. I have tried the following but bo

相关标签:
11条回答
  • 2020-12-16 18:10

    I had this problem while trying to run sqlcmd on terminal. I got it working by entering a tab character (copying from text editor didn't work for me).

    Press cntrl + v then tab.

    How to enter a tab char on command line?

    0 讨论(0)
  • 2020-12-16 18:13

    A similar answer to one posted above, but it's simpler in a way that I think is significant.

    1. Open your text editor
    2. Press Tab
    3. Highlight the chunk of whitespace (the tab) created
    4. Copy and paste that into the spot in your SQL command

    Even though this tab is represented as a wide chunk of whitespace, it is a single character.

    The other answer had some unnecessary stuff about pasting the whole command with "<TAB>" in it. I think that throws people off (it certainly threw me off).

    0 讨论(0)
  • 2020-12-16 18:18

    Found a good answer here: SQLCMD outfile as tab delimited text file

    1. Open Notepad
    2. Paste this: sqlcmd -S (local) -E -s"<TAB>" -Q "select * from sys.dm_exec_query_stats" -o MyOutput.txt -h-1 -W
    3. Highlight <TAB>, then hit the Tab key
    4. Save the file as MyBatch.bat
    5. Run MyBatch.bat
    0 讨论(0)
  • 2020-12-16 18:22

    In a batch file, putting a tab between the double quotes works.

    sqlcmd -S ServerName -E -Q"select * from mytable" -s"   " -o results.txt
    

    to do the same in a PowerShell file use escaped double quotes wrapped around an escaped tab

    sqlcmd -S ServerName -E -Q"select * from mytable" -s `"`t`" -o results.txt
    
    0 讨论(0)
  • 2020-12-16 18:22

    tldr: use ALT+009 the ascii tab code for the separator character

    In the example, replace {ALTCHAR} with ALT+009 (hold the ALT key and enter the digits 009)

    sqlcmd -E -d tempdb -W -s "{ALTCHAR}" -o junk.txt -Q "select 1 c1,2 c2,3 c3"
    

    Edit junk.txt. Tabs will be between columns.

    For other command line options:

    sqlcmd -?
    

    Note: The shell converts the ALT char to ^I, but if you try the command by typing -s "^I", you won't get the same results.

    0 讨论(0)
  • 2020-12-16 18:23

    Try using horizontal scroll bars with cmd.exe or powershell. Right click shortcut and click properties for repeated use, or right click title bar and click properties after opening then click layout tab. In screen buffer size set width and height to 8000 and then unselect wrap text output on resize (important). Click ok. Then restore down by clicking button next to minimize. You should see horizontal and vertical scroll bars. You can maximize window now and scroll in any direction. Now you can see all records in database.

    0 讨论(0)
提交回复
热议问题