T-SQL查询中的语法不正确

时间:2012-08-13 10:37:58

标签: sql sql-server-2008 tsql

我的存储过程中有T-SQL语句,这给了我错误。但是在查询窗口中相同的查询运行得很好。 它给出了错误说法

  

Incorret Syntax Near'。'

Set @SQL='Select ''<a href="javascript:editTicketByIDAction('' + 
          CONVERT(VarChar(Max), Ticket) + '')">'' + 
          CONVERT(VarChar(Max),t1.Ticket) + 
          ''</a>'' t1.Ticket,VendorTicket[Vendor Ticket], Comments 
From VendorTickets t1 
WHERE NotifyOn <= GetDate() And NotifyOn Is Not Null AND 
NOT EXISTS(SELECT * FROM VendorTickets t2 
WHERE t1.Ticket = t2.Ticket AND t1.InitiatedOn < t2.InitiatedOn)'

Exec(@SQL)

没有T-SQL,效果很好

    SELECT t1.Ticket,t1.VendorTicket,t1.Comments 
FROM VendorTickets t1 WHERE 
NOT EXISTS(SELECT * FROM VendorTickets t2 
WHERE t1.Ticket = t2.Ticket AND t1.InitiatedOn < t2.InitiatedOn)
AND  NotifyOn <= GetDate() And NotifyOn Is Not Null

2 个答案:

答案 0 :(得分:6)

您错过了+

... + '</a>' +  t1.Ticket ...
             ^------------------------here

答案 1 :(得分:0)

你在两个地方错过了+。试试这个:

Set @SQL='Select + ''<a href="javascript:editTicketByIDAction('' + 
          CONVERT(VarChar(Max), Ticket) + '')">'' + 
          CONVERT(VarChar(Max),t1.Ticket) + 
          ''</a>'' + t1.Ticket,VendorTicket[Vendor Ticket], Comments 
From VendorTickets t1 
WHERE NotifyOn <= GetDate() And NotifyOn Is Not Null AND 
NOT EXISTS(SELECT * FROM VendorTickets t2 
WHERE t1.Ticket = t2.Ticket AND t1.InitiatedOn < t2.InitiatedOn)'

Exec(@SQL)