วันอังคารที่ 20 เมษายน พ.ศ. 2553

sqlcmd Utility

sqlcmd.exe is one of msdn utility, that let you enter sql statements
sqlcmd.exe เป็นเครื่องมือหนึ่งของ MSDN ที่ไว้สำหรับสั่งคำสั่ง sql ผ่าน command line

sqlcmd.exe is installed in
โดยปกติแล้วจะถูกติดตั้งไว้ที่ ลองตรวจสอบดูว่าได้ลงไว้แล้วหรือยัง
C:\Program Files\Microsoft SQL Server\90\Tools\binn

you can download and install from microsoft
ถ้ายังไม่มีสามารถดาวน์โหลดมาติดตั้งเฉพาะโปรแกรมนี้ได้ดังนี้

Download
http://www.microsoft.com/downloads/details.aspx?FamilyID=d09c1d60-a13c-4479-9b91-9e8b9d835cdc&displaylang=en
install sqlncli.exe and follow with sqlncli.exe

เข้าไปที่เวบสำหรับดาวน์โหลดของ ไมโครซอฟท์ แล้วเลือกดาวน์โหลด เฉพาะ sqlncli.exe และ sqlncli.exe

Howto use for backup
การใช้งาน
1. run sqlcmd.exe at command prompt
สั่ง sqlcmd จะเจอ 1> เพื่อรอชุดคำสั่ง

2. use the following command
สามารถสั่งคำสั่งเป็นชุดได้ เมื่อต้องการให้ทำงานใช้คำสั่ง go
1> backup database YOURDATABASENAME
2> to disk=’C:\BACKUPNAME’
3> go

3. when command complete will return result like this
เมื่อทำงานเสร็จแล้วจะมีข้อความมาแจ้ง และไฟล์ที่เราได้ก็จะอยู่ตามที่เราตั้งไว้ในบรรทัดที่ 2
Processed 35464 pages for database ‘YOURDATABASENAME’, file ‘db’ on file 1.
Processed 1 pages for database ‘YOURDATABASENAME’, file ‘db_Log’ on file 1.
BACKUP DATABASE successfully processed 35465 pages in 55.686 seconds (5.217 MB/s
ec).

4. working with batch script
คำสั่งในการ backup สามารถนำไปใส่ใน batch ได้ ดังนี้
4.1 use sqlcmd togather with sql text file
เขียน query เซฟไว้เป็น text file แล้วสั่ง run ผ่าน sqlcmd

query.txt
backup database YOURDATABASENAME to disk=’C:\BACKUPNAME’

use following command at command prompt
เวลาเรียกใช้งานก็ใช้ดังนี้

sqlcmd -i query.txt

4.2 Write All script with one Command
เขียนทั้งหมดผ่าน batch script เลย
sqlcmd -Q "backup database YOURDATABASENAME to disk=’C:\BACKUPNAME’

For more Information please follow these Links
รายละเอียดเพิ่มเติมดูได้จากเวบของไมโครซอฟ์ทเอง

Remark
sqlcmd
[{ { -U login_id [ -P password ] } | –E trusted connection }]
[ -z new password ] [ -Z new password and exit]
[ -S server_name [ \ instance_name ] ] [ -H wksta_name ] [ -d db_name ]
[ -l login time_out ] [ -A dedicated admin connection ]
[ -i input_file ] [ -o output_file ]
[ -f <> | i: <> [ < , o: <> ] ]
[ -u unicode output ] [ -r [ 0 | 1 ] msgs to stderr ]
[ -R use client regional settings ]
[ -q "cmdline query" ] [ -Q "cmdline query" and exit ]
[ -e echo input ] [ -t query time_out ]
[ -I enable Quoted Identifiers ]
[ -v var = "value"...] [ -x disable variable substitution ]
[ -h headers ][ -s col_separator ] [ -w column_width ]
[ -W remove trailing spaces ]
[ -k [ 1 | 2 ] remove[replace] control characters ]
[ -y display_width ] [-Y display_width ]
[ -b on error batch abort ] [ -V severitylevel ] [ -m error_level ]
[ -a packet_size ][ -c cmd_end ]
[ -L [ c ] list servers[clean output] ]
[ -p [ 1 ] print statistics[colon format]]
[ -X [ 1 ] ] disable commands, startup script, enviroment variables [and exit]
[ -? show syntax summary ]

ไม่มีความคิดเห็น:

แสดงความคิดเห็น