I am running SQL express on a windows 2008 R2 server and trying to write a script to periodically query a database and save the results to a file. I have used:
sqlcmd -Q SELECT DISTINCT Date from MMD_Scale ORDER BY Date ASC -o testresults.txt
and
bcp "select distinct Date from MMD_Scale ORDER BY Date ASC" queryout testresults.txt -c -T
The problem is that both times it comes back saying the table MMD_Scale cant be resolved. I have verified the query in the Server management studio. There was also some online sources that say to specify the database with -D but when I add it it says -D is obsolete and is ignored. Any help is appreciated.
The issue is that when sqlcmd or bcp connects, they will connect to your default database. In this instance, it appears that is not the database that your table resides in.
Try putting "USE [database_name]" in your query. eg
sqlcmd -Q "USE MyDatabase; SELECT DISTINCT Date from MMD_Scale ORDER BY Date ASC" -o testresults.txt
Or fully-qualify the table name:
sqlcmd -Q "SELECT DISTINCT Date from MyDatabase.dbo.MMD_Scale ORDER BY Date ASC" -o testresults.txt
The -D
flag is not the same as -d
. Try sqlcmd -d YourDatabase -Q SELECT DISTINCT Date from MMD_Scale ORDER BY Date ASC -o testresults.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.