Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is stored procedure with pivot table to generate data like this

జిల్లా 2007-2008 2008-2009 2009-2010 2010-2011 2011-2012
Chittoor 14000 12000 15500 2376 3425
Total 14000 12000 15500 55236 436346

while executing i am getting this error
Unclosed quotation mark after the character string '2010-20'. Incorrect syntax near '2010-20'
SQL
CREATE procedure year_wise_close_bal
as
begin
DECLARE @fin_year NVARCHAR (500)
SELECT @fin_year = COALESCE (@fin_year  + ',[' + fin_year + ']', '[' + fin_year + ']')
FROM    after_audit_cc_trans
declare @sql NVARCHAR(500)
SET @sql='select * from(select insure10.dbo.Districtmst.distname as dname,
after_audit_cc_trans.fin_year as year,after_audit_cc_trans.close_bal as close_bal
from after_audit_cc_trans inner join insure10.dbo.Districtmst ON after_audit_cc_trans.distcode = insure10.dbo.Districtmst.distcode)as t1
pivot (sum(close_bal) for year in ('+@fin_year+')) as bal'
PRINT @sql
execute(@sql)
End
Posted
Comments
BulletVictim 5-Mar-14 8:35am    
try two ' around the @fin_year
(''+@fin_year+''))
CHill60 5-Mar-14 8:38am    
Your error is in @sql. Can you post the output from the PRINT @sql command. BulletVictim is correct - you appear to missing single quotes around the string value.
ZurdoDev 5-Mar-14 10:17am    
I suggest you post as solution.
Sateesh Pasupulla 5-Mar-14 11:46am    
Thank you friends for replay.
I got Solution for this query
when i was added ---> group by fin_year
to the
SELECT @fin_year = COALESCE (@fin_year + ',[' + fin_year + ']', '[' + fin_year + ']')
FROM after_audit_cc_trans group by fin_year

1 solution

As per RyanDev's suggestion I'm posting this solution ...
BulletVictim[^] said
Quote:
try two ' around the @fin_year
(''+@fin_year+''))

I responded
Quote:
Your error is in @sql. Can you post the output from the PRINT @sql command. BulletVictim is correct - you appear to missing single quotes around the string value.

OP has subsequently claimed that adding group by solved the problem
SQL
SELECT @fin_year = COALESCE (@fin_year  + ',[' + fin_year + ']', '[' + fin_year + ']')
FROM    after_audit_cc_trans group by fin_year


The reason this works could be determined by examining the contents of @fin_year as this is being inserted into the @sql string for use in an IN clause.
in ('+@fin_year+'))
so the expected format is IN('value1','value2', 'value3') etc.

The "trick" here is (as the OP has done) to print the SQL that the execute command has been provided. It is usually far easier to see the problem when you can see the characters that the error reports ... "Incorrect syntax near '2010-20'" in this instance.
 
Share this answer
 
Comments
Maciej Los 7-Mar-14 13:07pm    
+5
CHill60 7-Mar-14 14:20pm    
Thank you! And thank you for the other 5 ... OP post was deleted before I got back to you!
Maciej Los 7-Mar-14 14:23pm    
Some people doesn't want to share the answers to they questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900