Solved

Dynamic SQL Error - Incorrect syntax near ')'.

Posted on 2011-09-05
4
830 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
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
x-men earned 500 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 68

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now