개발/database

[mssql] sqlcmd를 사용하여 ddl, dml등 스크립트 파일 실행

가시죠 2022. 8. 21. 23:05
반응형

sqlcmd를 사용하여 ddl, dml등 스크립트 파일 실행

 

sqlcmd -S <myServer\instanceName> -i <C:\myScript.sql>

스크립트 파일 : C:\myScript.sql

 

명령프롬프트 창에서 아래 명령으로 실행

## 샘플1. 스크립트 실행
sqlcmd -S <myServer\instanceName> -i <c:\myScript.sql>

## 샘플2. SA 사용자로 실행 (서버정보 생략)
sqlcmd -U SA -i <c:myScript.sql>

## 샘플3. SA 사용자로 실행 (서버정보 생략, 실행결과 파일로 저장)
sqlcmd -U SA -i <c:\myScript.sql> -o c:\myScript.output.txt

## 샘플4. SA 사용자로 실행 (서버정보 생략, DB명 지정, 실행결과 파일로 저장)
sqlcmd -U SA -i <c:\myScript.sql> -d hitomisDB -o c:\myScript.output.txt

 

sqlcmd 매개변수

 

sqlcmd
   -a packet_size
   -A (dedicated administrator connection)
   -b (terminate batch job if there is an error)
   -c batch_terminator
   -C (trust the server certificate)
   -d db_name
   -D
   -e (echo input)
   -E (use trusted connection)
   -f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]
   -g (enable column encryption)
   -G (use Azure Active Directory for authentication)
   -h rows_per_header
   -H workstation_name
   -i input_file
   -I (enable quoted identifiers)
   -j (Print raw error messages)
   -k[1 | 2] (remove or replace control characters)  
   -K application_intent  
   -l login_timeout  
   -L[c] (list servers, optional clean output)  
   -m error_level  
   -M multisubnet_failover  
   -N (encrypt connection)  
   -o output_file  
   -p[1] (print statistics, optional colon format)  
   -P password  
   -q "cmdline query"  
   -Q "cmdline query" (and exit)  
   -r[0 | 1] (msgs to stderr)  
   -R (use client regional settings)  
   -s col_separator  
   -S [protocol:]server[instance_name][,port]  
   -t query_timeout  
   -u (unicode output file)  
   -U login_id  
   -v var = "value"
   -V error_severity_level
   -w screen_width
   -W (remove trailing spaces)
   -x (disable variable substitution)
   -X[1] (disable commands, startup script, environment variables, optional exit)
   -y variable_length_type_display_width
   -Y fixed_length_type_display_width
   -z new_password
   -Z new_password (and exit)
   -? (usage)

 

 

반응형