|
Save the following
SQL Server SQLCMD script in a designated folder as "selecttable.sql":
USE AdventureWorks
GO
SELECT TOP 100 *
FROM $(TableName)
ORDER BY 1
GO
Execute the following
T-SQL script in Query Editor or the content of @CmdString at the command
line starting with SQLCMD to see the head (TOP 100 rows) of Person.Contact and Sales.SalesOrderHeader tables:
DECLARE @CmdString CHAR(2000)
SET @CmdString = 'sqlcmd -v TableName ="Person.Contact" -i
e:\data\script\selecttable.sql -S"ServerName"'
PRINT @CmdString -- test & debug
EXEC xp_cmdshell @CmdString
SET @CmdString = 'sqlcmd -v TableName ="Sales.SalesOrderHeader" -i
e:\data\script\selecttable.sql -S"ServerName"'
EXEC xp_cmdshell @CmdString
/* Partial results
SalesOrderID RevisionNumber OrderDate
43659 1 2001-07-01 00:00:00.000
43660 1 2001-07-01 00:00:00.000
43661 1 2001-07-01 00:00:00.000
43662 1 2001-07-01 00:00:00.000
43663 1 2001-07-01 00:00:00.000
*/
Execute a stored procedure with parameters via SQLCMD:
DECLARE @CmdString VARCHAR(2000)
SET @CmdString = 'sqlcmd -S"YOURSERVER" -Q"EXECUTE [AdventureWorks2008].[dbo].[uspGetBillOfMaterials] 800, ''20040401''"'
PRINT @CmdString -- test & debug
EXEC xp_cmdshell @CmdString
|