0

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.

1
  • Can you run that query interactively? If so, you might have to qualify the table name with the server, schema, and/or owner information. Commented Sep 26, 2013 at 15:24

2 Answers 2

4

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. e.g.

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.