There's nothing to prevent you from using WHERE
and JOIN
clauses in the queries that you supply to SQLCMD. They're just regular SQL queries.
For example, here is the query you provided modified to include the restrictions you specified:
SELECT OITM.ItemCode, OITM.FrgnName, OITM.OnHand-OITM.IsCommited
FROM OITM
INNER JOIN OITW
ON OITW.ItemCode = OITM.ItemCode
WHERE OITM.U_Category = 'Toys'
AND OITM.Qrygroup2 = 'Y'
AND OITW.WhsCode = '01. Main'
You would just supply this query to SQLCMD, as you did with the query in your OP:
SQLCMD -S SERVER-VMSQL -d SBO_COMPANYNAME -U sa -P adminpassword -Q "SELECT OITM.ItemCode, OITM.FrgnName, OITM.OnHand-OITM.IsCommited FROM OITM INNER JOIN OITW ON OITW.ItemCode = OITM.ItemCode WHERE OITM.U_Category = 'Toys' AND OITM.Qrygroup2 = 'Y' AND OITW.WhsCode = '01. Main'" -s "," -o "\\server-address\users\rick\TestBat.csv" -h-1 -s"," -W -w 999