How to use sqlcmd to execute many queries in one table?

时间:2022-07-22 01:05:08

I have many SQL scripts; I want to execute them in one table using sqlcmd.

我有很多SQL脚本;我想使用sqlcmd在一个表中执行它们。

I have created a new database and a new table. How can I read several files from my PC and execute them in one table?

我创建了一个新数据库和一个新表。如何从PC上读取多个文件并在一个表中执行?

1 个解决方案

#1


0  

Refer this

1.Click Start, point to All Programs, point to Accessories, and then click Notepad.

2.Copy and paste the following Transact-SQL code into Notepad:

USE <DBName>;
GO
select *from table1
select *from table2
select *from table3
select *from table4
select *from table5
--give all your select query here
GO

Save the file as myScript.sql in the C drive.

3.To run the script file

4.Open a command prompt window.

In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql

Press ENTER.

The result of the sql file is written to the command prompt window.
To save this output to a text file

5.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.

#1


0  

Refer this

1.Click Start, point to All Programs, point to Accessories, and then click Notepad.

2.Copy and paste the following Transact-SQL code into Notepad:

USE <DBName>;
GO
select *from table1
select *from table2
select *from table3
select *from table4
select *from table5
--give all your select query here
GO

Save the file as myScript.sql in the C drive.

3.To run the script file

4.Open a command prompt window.

In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql

Press ENTER.

The result of the sql file is written to the command prompt window.
To save this output to a text file

5.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.