当新添加的表没有数据时,更新SQL语句不返回任何记录

时间:2014-09-09 12:51:54

标签: sql sql-server

我最近被告知要添加一个评论字段。由于这些评论是用于会计,我创建了一个新表。 CITCommentID,CITComment,DealID,CITCurrentComment是表中的字段。我必须更新一个SQL语句,从多个表中选择多个项目以显示给用户。我遇到的问题是因为该表中还没有数据,我无法检索其他信息。这是我的发言。

SELECT vl.dealid, 
       stocknumber, 
       dealnumber, 
       newdeal, 
       archived, 
       dealershipname, 
       salespersonnumber, 
       customername, 
       grossprofit, 
       dealdate, 
       make, 
       model, 
       newused, 
       amtfinanced, 
       category, 
       bankname, 
       customernumber, 
       finame, 
       status, 
       locationname, 
       datereceived, 
       comment, 
       titled, 
       changedby, 
       funded, 
       contractsreturned, 
       citcomment DateChanged 
FROM   tvehiclelog vl 
       JOIN tcustomer cu 
         ON cu.customerid = vl.customerid 
       JOIN tbank b 
         ON b.bankid = vl.bankid 
       JOIN tmake m 
         ON m.makeid = vl.makeid 
       JOIN tdealerships d 
         ON d.dealershipid = vl.dealershipid 
       JOIN tsalesperson sp 
         ON sp.salespersonid = vl.salespersonid 
       JOIN tmodel ml 
         ON ml.modelid = vl.modelid 
       JOIN tinternallocations il 
         ON il.internallocationid = vl.internallocationid 
       JOIN tvehiclecomments vcc 
         ON vl.commentid = vcc.commentid 
       JOIN tcontractsintransitcomments citc    --This is new join
         ON vl.citcommentid = citc.citcommentid --CommentID relates to the table vl 
       JOIN tfimanagers fi 
         ON fi.fimanagerid = vl.fimanagerid 
WHERE  vl.dealid = 5224 
GROUP  BY vl.dealid, 
          [stocknumber], 
          [newdeal], 
          [archived], 
          [customername], 
          [dealdate], 
          [amtfinanced], 
          [bounced], 
          [comment], 
          [locationname], 
          [daysout], 
          [datereceived], 
          [dealnumber], 
          [dealershipname], 
          [salespersonnumber], 
          [grossprofit], 
          [make], 
          [model], 
          [newused], 
          [category], 
          [bankname], 
          [customernumber], 
          [finame], 
          [status], 
          [locationname], 
          [datereceived], 
          [comment], 
          [titled], 
          [changedby], 
          [funded], 
          [contractsreturned], 
          [datechanged], 
          citcomment 
ORDER  BY [datechanged] ASC; 

即使没有与记录相关的注释,我也需要此语句来返回结果。由于这是一个没有注释的新表,它仍然需要显示其他信息。我还在学习MSSQL,但我还不知道所有的技巧。任何帮助是极大的赞赏!

2 个答案:

答案 0 :(得分:3)

将其更改为LEFT JOIN.左连接意味着即使没有与结果中的记录相关的注释,select语句也会返回结果。

LEFT JOIN tcontractsintransitcomments citc    --This is new join
ON vl.citcommentid = citc.citcommentid --CommentID relates to the table vl 

这是完整的SQL:

SELECT vl.dealid, 
       stocknumber, 
       dealnumber, 
       newdeal, 
       archived, 
       dealershipname, 
       salespersonnumber, 
       customername, 
       grossprofit, 
       dealdate, 
       make, 
       model, 
       newused, 
       amtfinanced, 
       category, 
       bankname, 
       customernumber, 
       finame, 
       status, 
       locationname, 
       datereceived, 
       comment, 
       titled, 
       changedby, 
       funded, 
       contractsreturned, 
       citcomment DateChanged 
FROM   tvehiclelog vl 
       JOIN tcustomer cu 
         ON cu.customerid = vl.customerid 
       JOIN tbank b 
         ON b.bankid = vl.bankid 
       JOIN tmake m 
         ON m.makeid = vl.makeid 
       JOIN tdealerships d 
         ON d.dealershipid = vl.dealershipid 
       JOIN tsalesperson sp 
         ON sp.salespersonid = vl.salespersonid 
       JOIN tmodel ml 
         ON ml.modelid = vl.modelid 
       JOIN tinternallocations il 
         ON il.internallocationid = vl.internallocationid 
       JOIN tvehiclecomments vcc 
         ON vl.commentid = vcc.commentid 
       LEFT JOIN tcontractsintransitcomments citc    --This is new join
         ON vl.citcommentid = citc.citcommentid --CommentID relates to the table vl 
       JOIN tfimanagers fi 
         ON fi.fimanagerid = vl.fimanagerid 
WHERE  vl.dealid = 5224 
GROUP  BY vl.dealid, 
          [stocknumber], 
          [newdeal], 
          [archived], 
          [customername], 
          [dealdate], 
          [amtfinanced], 
          [bounced], 
          [comment], 
          [locationname], 
          [daysout], 
          [datereceived], 
          [dealnumber], 
          [dealershipname], 
          [salespersonnumber], 
          [grossprofit], 
          [make], 
          [model], 
          [newused], 
          [category], 
          [bankname], 
          [customernumber], 
          [finame], 
          [status], 
          [locationname], 
          [datereceived], 
          [comment], 
          [titled], 
          [changedby], 
          [funded], 
          [contractsreturned], 
          [datechanged], 
          citcomment 
ORDER  BY [datechanged] ASC; 

答案 1 :(得分:1)

SELECT vl.dealid, 
       stocknumber, 
       dealnumber, 
       newdeal, 
       archived, 
       dealershipname, 
       salespersonnumber, 
       customername, 
       grossprofit, 
       dealdate, 
       make, 
       model, 
       newused, 
       amtfinanced, 
       category, 
       bankname, 
       customernumber, 
       finame, 
       status, 
       locationname, 
       datereceived, 
       comment, 
       titled, 
       changedby, 
       funded, 
       contractsreturned, 
       citcomment DateChanged 
FROM   tvehiclelog vl 
       JOIN tcustomer cu 
         ON cu.customerid = vl.customerid 
       JOIN tbank b 
         ON b.bankid = vl.bankid 
       JOIN tmake m 
         ON m.makeid = vl.makeid 
       JOIN tdealerships d 
         ON d.dealershipid = vl.dealershipid 
       JOIN tsalesperson sp 
         ON sp.salespersonid = vl.salespersonid 
       JOIN tmodel ml 
         ON ml.modelid = vl.modelid 
       JOIN tinternallocations il 
         ON il.internallocationid = vl.internallocationid 
       JOIN tvehiclecomments vcc 
         ON vl.commentid = vcc.commentid 
       LEFT JOIN tcontractsintransitcomments citc    
         ON vl.citcommentid = citc.citcommentid 
       JOIN tfimanagers fi 
         ON fi.fimanagerid = vl.fimanagerid 
WHERE  vl.dealid = 5224 
GROUP  BY vl.dealid, 
          [stocknumber], 
          [newdeal], 
          [archived], 
          [customername], 
          [dealdate], 
          [amtfinanced], 
          [bounced], 
          [comment], 
          [locationname], 
          [daysout], 
          [datereceived], 
          [dealnumber], 
          [dealershipname], 
          [salespersonnumber], 
          [grossprofit], 
          [make], 
          [model], 
          [newused], 
          [category], 
          [bankname], 
          [customernumber], 
          [finame], 
          [status], 
          [locationname], 
          [datereceived], 
          [comment], 
          [titled], 
          [changedby], 
          [funded], 
          [contractsreturned], 
          [datechanged], 
          citcomment 
ORDER  BY [datechanged] ASC;