标题:CTE错误

时间:2010-06-23 13:55:41

标签: sql sql-server sql-server-2005 tsql

我在执行以下SQL时遇到这些错误请帮帮我

;WITH myCTE AS
(Select mcisi.* from
coke_packaged_item AS spi
JOIN coke_item AS si
  ON si.coke_id = spi.coke_id
  AND si.coke_item_id = spi.coke_item_id
  AND si.shipper_flag = 'n'
JOIN merch_cat_import_coke_item AS mcisi
  ON mcisi.coke_id = si.coke_id
  AND mcisi.resolved_coke_item_id = si.coke_item_id
  AND mcisi.cat_import_event_id = @cat_import_event_id
  AND mcisi.accept_flag = 'y')


UPDATE coke_packaged_item
SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id) 
  priced_in_uom_id = COALESCE((select resolved_weight_unit_of_measure_idfrom myCTE.coke_id = coke_id), @each_uom_id), 
  name = (select packaged_item_name from myCTE where myCTE.coke_id = coke_id), 
  package_weight = (select package_weight from myCTE where myCTE.coke_id = coke_id) ,
  status_code = (select status_code from myCTE where myCTE.coke_id = coke_id) ,
  last_modified_user_id = (select CASE WHEN last_modified_user_id = 42 THEN @posting_user_id ELSE last_modified_user_id END from myCTE where myCTE.coke_id = coke_id),
  last_modified_timestamp = CURRENT_TIMESTAMP
and exists (select coke_id from coke_item AS si
  where si.coke_id = spi.coke_id
  AND si.coke_item_id = spi.coke_item_id
  AND si.shipper_flag = 'n')
and exists (select coke_id from merch_cat_import_coke_item AS mcisi)
  where mcisi.coke_id = si.coke_id
  AND mcisi.resolved_coke_item_id = si.coke_item_id
  AND mcisi.cat_import_event_id = @cat_import_event_id
  AND mcisi.accept_flag = 'y'

错误消息是:

Msg 102, Level 15, State 1, Procedure Sp_Name, Line 44
Incorrect syntax near 'priced_in_uom_id'.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 44
Incorrect syntax near '.'.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 45
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 46
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 47
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 48
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Procedure Sp_Name, Line 54
Incorrect syntax near the keyword 'and'.
Msg 156, Level 15, State 1, Procedure Sp_Name, Line 55
Incorrect syntax near the keyword 'where'.

4 个答案:

答案 0 :(得分:2)

我的朋友,我一直在寻找@你的代码,到目前为止,我真正找到的只是输入错误。

例如, *在set子句中,packaged_in_uom_id和pricing_in_uom_id的集合之间没有逗号。 *在pricing_in_uom_id的子查询中,from子句前面没有空格。

我的建议是采用一种程式化的模式来编写你的sql。我使用的模式说明了这一点 *每个条款都有自己的行 - 选择,从哪里,按顺序,分组 *每列都有自己的一行 *列表逗号前面的项目

当我开始使用这些模式格式化代码时,我开始看到问题。

我使用RedGate的SQL Refactor强制执行这些模式。这是我发现的最好的SQL代码格式化程序。

答案 1 :(得分:0)

看起来你在第一个SET行的末尾错过了一个逗号

SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id),

答案 2 :(得分:0)

您在myCTE.coke_id = coke_id)

之后的第44行错过了一个逗号

答案 3 :(得分:0)

你在这里错过了一个逗号

SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id) ,

同样在下一行中,您在fromwhere

之前错过了一个空格
select resolved_weight_unit_of_measure_idfrom myCTE.coke_id = coke_id

我可能会有其他错误让您找到它们

@SmartestVEGA您今天已经发布了大量这类问题。我建议通过语法检查获取SQL2008 Management Studio的副本。我粘贴了您的查询,可以很快看到问题。