?
Solved

Dynamic SQL Error - Incorrect syntax near ')'.

Posted on 2011-09-05
4
Medium Priority
?
895 Views
Last Modified: 2012-05-12
Hi All,

Can anyone suggest why I am receiving the following error within the attached SQL. The error is on the line 4 'SET @query = '..

Error:

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.

Thanks,

Rit
DECLARE @cols NVARCHAR(2000), @query NVARCHAR(4000)

	SELECT @cols = COALESCE(@cols + ',' + QUOTENAME(Car_Name), QUOTENAME(Car_Name)) FROM lookup_cars ORDER BY Car_Name, OrderSeq

	SET @query = 
	'SELECT *  FROM 
	(
		view_new_cars
	)t

	PIVOT (COUNT(t.CarID) FOR T.Car_Name IN (' + @cols + ')) as PVT'

	EXEC(@query)

Open in new window

0
Comment
Question by:rito1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
x-men earned 2000 total points
ID: 36484478
use a "print @query" to check it the string is ok
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 36484496
Good thinking! :-)
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36484823
And the error is? You should post that usually, as the accepted post might have helped, but isn't the solution. In such cases please post the solution, accept it and optionally award assist points to experts' posts.

The error is: You need to cut off the first comma from @cols, and make sure there is always a value, even if the select to construct @cols does not return any rows.
Instead, I would insert a dummy value into the final SQL:
SET @query = 
        'SELECT *  FROM 
        (
                view_new_cars
        )t

        PIVOT (COUNT(t.CarID) FOR T.Car_Name IN ( '''' ' + @cols + ')) as PVT'

Open in new window

0
 
LVL 1

Author Comment

by:rito1
ID: 36487029
Thanks for the advice Qlemo.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question