View Single Post
Old 22-Oct-2007, 12:51 AM   #2 (permalink)
Anilrgowda
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Re: Invalid column name or Syntax error?

you need to use two single quotes to enclose what will eventually be a literal...

ie ''S51'' not "S51"

the 3 quotes in the
AND CONVERT(VARCHAR(12), crcalldatetime, 110) between "' + @startdate + '" and "' + @enddate + '"

are actually (intended to be) 3 single quotes ... because you are using dynamic sql (A bad idea usually)
you need to contruct the sql string with embeded quotes around your literal strings...

when doing this you double up / escape the quote character so that the sql parser recognisez that you want to actually use a quote within the string rather than end the current string...

you probably want this

SELECT @SQL = 'SELECT ''' + @NAME + ''', CRAGENTID, count(DISTINCT L.I3_ROWID) '
+ ' FROM [' + @NAME + '_CALLLIST] as L '
+ ' WHERE CRCallResultCode like ''S51'''
+ ' AND CONVERT(CHAR(12), crcalldatetime, 110) between '''
+ @startdate + ''' and ''' + @enddate + ''''
+ ' GROUP BY CRAGENTID'


------------------


Anilrgowda is offline   Reply With Quote