问题
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