how to concatenate uniqueidentifier in a dynamic query

送分小仙女□ 提交于 2019-12-10 12:39:02

问题


I have a dynamic query in which I want to concatenate uniqueidentifier, but + and & operators are not supporting this, is there a way I can concatenate uniqueidentifier to a dynamic string. Any sample or any help in this regard will be highly appricated.


回答1:


Have you tried casting or converting to a string first, then concating?

CAST(NEWID() AS NVARCHAR(36))



回答2:


I know this is old but I ran into this post trying to figure out the same thing and the problem was that I didn't have enough 's around my unique identifier. I basically had:

'SELECT * FROM Interface WHERE ID = '' + CAST(@InterfaceID AS NVARCHAR(36)) + '' AND 1 = 1'

I got an error saying incorrect syntax near 00A (the first part of the GUID). The problem is that this was parsing to :

SELECT * FROM Interface WHERE ID = 00A3F5B5-C7B3-4128-B03A-EADE79129F40 AND 1 = 1

By changing my query to:

'SELECT * FROM Interface WHERE ID = ''' + CAST(@InterfaceID AS NVARCHAR(36)) + ''' AND 1 = 1'

with three apostrophes I got:

SELECT * FROM Interface WHERE ID = '00A3F5B5-C7B3-4128-B03A-EADE79129F40' AND 1 = 1

Which is correct.

Hope that helps.



来源:https://stackoverflow.com/questions/1036572/how-to-concatenate-uniqueidentifier-in-a-dynamic-query

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