Create a script file
To create a simple Transact-SQL script file using Notepad, follow these steps:
Click Start , select All Programs , go to Accessories , and then click Notepad .
Copy and paste the following Transact-SQL code into Notepad:
Save the file as myScript.sql on drive C.
Run the script file
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S myServer\InstanceName -i C:\myScript.sql
Press ENTER.
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S myServer\InstanceName -i C:\myScript.sql
Press ENTER.
A list of Adventure Works employee names and addresses is written in the command prompt window.
Save the output to a text file
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S myServer\InstanceName -i C:\myScript.sql -o C:\EmpAdds.txt
Press ENTER.
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S myServer\InstanceName -i C:\myScript.sql -o C:\EmpAdds.txt
Press ENTER.
No output is returned in the command prompt window. Instead, the output is sent to the EmpAdds.txt file. You can verify this output by opening the EmpAdds.txt file.
If you want to specify Username/Password for access
sqlcmd -S myServer\InstanceName -U username -P username -i C:\myScript.sql -o C:\EmpAdds.txt
SOURCE: https://docs.microsoft.com/en-us/sql/ssms/scripting/sqlcmd-run-transact-sql-script-files?view=sql-server-ver15
Comments
Post a Comment