T-SQL: issue with string concat

不打扰是莪最后的温柔 提交于 2019-11-29 13:18:26

You can't parametrise or concatenate the parameters of OPENROWSET. It is constant values only.

You'll have to use dynamic SQL and a temp table, or consider using SSIS for example

This article pointed me in the right direction when I had the same issue with OPENQUERY:

https://web.archive.org/web/20120724073530/http://consultingblogs.emc.com/jamespipe/archive/2007/06/28/SQL-Server-2005_3A00_-Passing-variables-into-an-OPENQUERY-argument.aspx

Basically, you can wrap the entire statement in a variable (nvarchar), including the openrowset, and run exec sp_executesql @sql. It gets a little ugly to read around the 's though, because you'll have to escape them with ''.

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