Using sqlcmd with FOR XML returns Invalid Object name 'dbo.Table' yet Query runs in SSMS

…衆ロ難τιáo~ 提交于 2019-12-06 03:29:24

As you've discovered and I confirmed, that you need to specify the -d MyDatabaseName option.

Otherwise, you'll get the invalid object name "Table" error as soon as you use FOR XML PATH, even if you add the database name in your script FROM clause (viz. even FROM Database.dbo.Table won't circumvent the need for -d with FOR XML AUTO). Bizarre.

Re: it does not kick out a properly formatted xml file:

You need to add the :XML ON as the first line in your input (-i) file to sqlcmd:

:XML ON
SELECT title1,
 ...

A bit late but in the SQL Script just add the following before your query:

USE dbname;

SELECT ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!