I am having complications adding a guid type to a sql string and getting the query to go. I have to write the sql this because I need to pass the name database as a parameter.
The following code is what I’m trying to do but it doesnt work. No matter what I do it doesn’t read the GUID correctly. It gives me the following error when executing the stored proc. Probably because guid has “-” in them
Incorrect syntax near ‘-‘.
Here’s the code:
ALTER PROCEDURE [dbo].[templatesAndBaskets_Select]
@guid uniqueidentifier,
@DatabaseName varchar(50)
AS
BEGIN
Declare @sql varchar(max)
Set @sql=
'select soldtoid
from ' + @DatabaseName + '.templatesAndBaskets' +
' where ordergroupid = ' + CONVERT(varchar(max),@guid)
print @sql
EXECUTE SP_EXECUTESQL @sql
END
You probably just simply need to put single quotes around the GUID :
That should result in a SQL statement something like this:
which is valid – yours without the single quotes around the GUID isn’t valid…
Update: you also need to change your
@sqlvariable toNVARCHARforsp_Executesql– try this:Does that work??