3

My code is as below, somehow there is always an error near @Name

DECLARE @Name nvarchar(MAX)  =  '(mm.dll, ben and jerry.exe)'
DECLARE @sql nvarchar(MAX)= 'SELECT OrderName,
   customer.version,
   count(DISTINCT company.CID) as Counts
FROM [CompanyData] company
  INNER JOIN [vendor] mav on company.CID = mav.CID
  LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId AND company.DId = customer.DId
WHERE OrderName in' + @Name+ '
  GROUP BY  
         customer.version, OrderName'

EXEC sp_executesql @sql

5 Answers 5

5

Put a single quote in your declaration of @Name and just remove the hashes(#) in it:

DECLARE @Name nvarchar(MAX)='(''mm.dll'', ''ben and jerry.exe'')'
DECLARE @sql nvarchar(MAX)= 
       'SELECT 
              OrderName,
              customer.version,
              count(DISTINCT company.CID) as Counts
        FROM [CompanyData] company
        INNER JOIN [vendor] mav on company.CID = mav.CID
        LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId    
             AND company.DId = customer.DId
        WHERE OrderName in ' + @Name+ '
        GROUP BY customer.version, OrderName'

EXEC sp_executesql @sql
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @Rigel1121 thx for the prompt reply. But I don't think it is due to the ' since the data is actually faked and even without ' just like '(apple and pear, orange)' won't work as well
I think the problem might be the way I concatenate the sql string?
I've edited my answer. Since your values in your WHERE clause are strings, Don't forget to enclosed them in single quotes. See and try my edited answer.
Thanks! The error changes this time since my actual data has a . in each value, such as 'mm.dll' so the error seems to be the . do I have to escape . in sql as well? How could I do that in this case?
I have changed my values. Sorry for so much confusion
|
0

Please add double quotes in @Name variable like..

DECLARE @Name nvarchar(MAX)  =  '("mm.dll", "ben and jerry.exe")'

Comments

0

it would probably save you a lot of grief if you store your names into a table value;

DECLARE @a varchar(max) = 'Jerry.ede';    
DECLARE @b varchar(max) = 'Sa''ra';    
DECLARE @c varchar(max) =  '(''mm.dll''),(''ben and jerry.exe'')';

DECLARE @Names TABLE (name nvarchar(MAX));

INSERT INTO names values (@a),(@b);
EXEC sp_executesql N'INSERT INTO names values ' + @c;

SELECT OrderName,
   customer.version,
   count(DISTINCT company.CID) as Counts
FROM [CompanyData] company
  INNER JOIN [vendor] mav on company.CID = mav.CID
  LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId AND company.DId = customer.DId
WHERE OrderName in (SELECT name from @names)
  GROUP BY  
         customer.version, OrderName;

Comments

0

EXEC(@sql) or EXEC sys.sp_executesql @sql

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

Please add double single qoute in the string @name .

 DECLARE @Name nvarchar(MAX)  =  '(sally''s order, ben and jerry)'
 DECLARE @sql nvarchar(MAX)= 'SELECT OrderName,
 customer.version,
  count(DISTINCT company.CID) as Counts
 FROM [CompanyData] company
 INNER JOIN [vendor] mav on company.CID = mav.CID
 LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId       
 AND company.DId = customer.DId
 WHERE OrderName in' + @Name+ '
 GROUP BY  
 customer.version, OrderName'

 EXEC sp_executesql @sql

1 Comment

thx! but I think my actual problem is not because of that.. see the corresponding changes above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.