{"id":1981,"date":"2024-08-13T22:00:21","date_gmt":"2024-08-13T14:00:21","guid":{"rendered":"http:\/\/www.jh1999.cn\/?p=1981"},"modified":"2024-09-10T14:08:35","modified_gmt":"2024-09-10T06:08:35","slug":"sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96%e4%b8%be%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.jh1999.cn\/?p=1981","title":{"rendered":"SQL\u8bed\u53e5\u4f18\u5316\u4e3e\u4f8b"},"content":{"rendered":"<div class=\"gb-container gb-container-3310355e\">\n\n<p class=\"wp-block-paragraph\">\u540c\u6837\u4e00\u4e2a\u76ee\u7684\uff0c\u53ef\u4ee5\u7528\u4e0d\u540c\u7684SQL\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\u4f46\u6548\u7387\u4e0a\u4f1a\u6709\u4e0d\u540c\uff0c\u751a\u81f3\u540c\u6837\u7684\u8bed\u53e5\uff0c\u5728\u4e0d\u540c\u7684SQL Server\u7248\u672c\u4e2d\uff0c\u6548\u7387\u4e5f\u53ef\u80fd\u4e0d\u4e00\u6837\u3002\u6240\u4ee5\uff0c\u4f7f\u7528\u66f4\u4f18\u5316\u7684\u8bed\u53e5\u6765\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u662f\u6709\u5fc5\u8981\u7684\u3002\u8fd9\u91cc\u5c31\u6765\u4e3e\u4e00\u4e9b\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6\u4ee3 not in \u7684\u67e5\u8be2\u4e0e\u5220\u9664\u8bed\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select aa.* from aa left join bb on aa.id=bb.id where bb.id is null\n\nselect aa.* from aa where Not Exists (select * from bb where bb.F1=aa.F1)\n\ndelete from aa where not exists(select * from bb where aa.myID=bb.myID)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u76f8\u540c\u7684\u4e24\u4e2a\u8868\u4e4b\u95f4\u590d\u5236\u8bb0\u5f55\uff0c\u82e5\u6709IDENTITY\u680f\u4f4d\uff0c\u5148\u5173\u6389\u6807\u8bc6\u89c4\u8303\u518d\u590d\u5236\u518d\u6062\u590dIDENTITY\uff0c\u53ef\u4e8b\u534a\u529f\u500d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insert into msEvenLog select * from msEvenOld<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5220\u9664\u91cd\u590d\u7684\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delete from aa where id not in\n     (select max(id) from aa group by col1,col2,col3...)  <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6\u6700\u5927\u503c\u7684\u8bb0\u5f55 &#8212; \u62a5\u4ef7\u6392\u884c\u699c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from poItPrice aa where aa.price=\n  (select min(price) from poItPrice bb where bb.itID=aa.itID)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5de5\u827a\u8868(BOM\u7ed3\u6784\u8868)\u4e2d\u53d6\u5404\u4ea7\u54c1\u7684\u201cPJ\u201d\u5de5\u5e8f\u7684\u524d\u4e00\u9053\u5de5\u5e8f\uff0cViewOrder\u8868\u793a\u7b2c\u51e0\u9053\u5de5\u5e8f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select dA.itID,dA.LineNum from prRoute dA,\n(select d1.itID,max(d1.ViewOrder)as PreOrder from prRoute d1,prRoute d2 \n  where d1.itID=d2.itID and d1.ViewOrder&lt;d2.ViewOrder and d2.LineNum='PJ'\n  group by d1.itID)as dB \nwhere dA.itID=dB.itID and dA.ViewOrder=dB.PreOrder\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eba\u529b\u8d44\u6e90\u7ba1\u7406\u4e2d\u7684\u3010\u7528\u9910\u7edf\u8ba1\u3011\uff0c\u672c\u4f8b\u4e2dhraBrush\u662f\u5237\u5361\u8868\uff0chreEmployee\u662f\u5458\u5de5\u57fa\u7840\u8d44\u6599\u8868\uff0chreDept\u662f\u90e8\u95e8\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select d3.dpna,d1.WkNo,d1.WkNa,d2.Date0,\n  (select top 1 convert(varchar(8),d5.OnTime,108) from jhERPsu..hraBrush d5 \n      where d5.EmplID=d1.EmplID and Convert(Char(10),d5.OnTime,120)=d2.Date0 \n      and convert(varchar(8),d5.OnTime,108)&gt;='11:30:00'\n      and convert(varchar(8),d5.OnTime,108)&lt;='12:30:59')as '\u4e2d\u9910',\n  (select top 1 convert(varchar(8),d5.OnTime,108) from jhERPsu..hraBrush d5 \n      where d5.EmplID=d1.EmplID and Convert(Char(10),d5.OnTime,120)=d2.Date0 \n      and convert(varchar(8),d5.OnTime,108)&gt;='16:45:00'\n      and convert(varchar(8),d5.OnTime,108)&lt;='17:50:59')as '\u665a\u9910'\nfrom jhERPsu..hreEmployee d1 ,\n  jhERPsu..hreDept d3,\n  (select distinct EmplID,Convert(char(10),OnTime,120) as Date0 from jhERPsu..hraBrush d1\n    where d1.OnTime&gt;=@param1 and d1.OnTime&lt;@param2) d2\nwhere d1.EmplID=d2.EmplID\nand d1.Dept=d3.Dept<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u627e\u51fa\u5c0f\u6570\u4f4d\u6570\u7b2c\u4e94\u4f4d\u6709\u503c\u7684\u8bb0\u5f55\u7684\u8fc7\u6ee4\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>where cast(Price*10000 as int)&lt;Price*10000\n\nwhere convert(decimal(18,5),PostedQty) &lt; PostedQty\n\nwhere ceiling(qty*100000)&lt;&gt;qty*100000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6279\u91cf\u66f4\u65b0\u5408\u8ba1\u503c\u7684\u4e24\u79cd\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>----\u6709\u4ea4\u6613\u660e\u7ec6\u7684\u7269\u9879\u624d\u66f4\u65b0\u5408\u8ba1\u503c\nUpdate d1 set d1.OrderedQty=d2.poQty from stItSum d1,\n  (select itID,HouseID,sum(qtyIng)as poQty from mpReqLine \n    where mpReqType in('PI','WI') group by itID,HouseID) d2\n  where d1.itID=d2.itID and d1.HouseID=d2.HouseID\n\n----\u6240\u6709\u8bb0\u5f55\u90fd\u53c2\u4e0e\u66f4\u65b0\uff0c\u6ca1\u6709\u660e\u7ec6\u7684\u66f4\u65b0\u4e3aNull\nUpdate mpReqTable set VendID=(select top 1 d2.VendID from poItPrice d2,vacExchRate d3\n  where d1.itID=d2.itID and d2.Currency=d3.Currency order by d2.Price*d3.ExchRate)\n  from mpReqTable d1 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6c42\u767e\u5206\u6bd4\uff0c\u987b\u8003\u8651\u5206\u6bcd\u53ef\u80fd\u662f0\u6216\u7a7a\u7684\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select IqcId,ckErr1,AqlQty,ckItem,\nround(isNull(ckErr1,0)*100.0\/(case isNull(AqlQty,0) when 0 then 1 else AqlQty end),0) as ckPercent\n from w_IqcInsCK <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u542f\u7528raise\u529f\u80fd\uff0cif @Money &gt; @Stand \u662f\u5224\u65ad\u6761\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>begin tran\n\nif @Money &gt; @Stand\nbegin\n rollback tran\n raiserror('\u5ba2\u6237\u8d85\u51fa\u4fe1\u7528\u989d\u5ea6,\u4e0d\u5145\u8bb8\u6279\u51c6',-1,-1,'')\n return\nend\n\nupdate soSaleTable set  soStatus='2',ConfirBy='abc', ConfirDate=convert(varchar(10),getdate(),121)\n where SaleID='SO11000156'\n\ncommit tran<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u65f6\u6307\u5b9a\u7d22\u5f15\u3002\u8bf7\u6ce8\u610f\uff0c\u4e3b\u4ece\u8868\u67e5\u8be2\u65f6\u4ece\u8868\u4e0d\u80fd\u7528 with(Index=&#8230;)\u5173\u952e\u8bcd\uff0c\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u7528\u4e8e\u53ea\u8bfb\uff0c\u52a0\u4e0a\u5173\u952e\u8bcdwith(NOLOCK)\u4f1a\u5feb\u4e00\u4e9b\u3002\u67e5\u8be2\u8868\u4e2d\u6709\u54ea\u4e9b\u7d22\u5f15\uff1aEXEC sp_helpindex &#8216;ProdJournalBom&#8217;\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select * from ProdJournalBom with(INDEX=I_239LINEIDX) where DataAreaID='fin'\n\nselect * from ProdJournalBom with(NOLOCK) where DataAreaID='fin'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6\u4ee3\u67d0\u680f\u4f4d\u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update ccVoucher set itDesc=replace(itDesc,'\u6751\u5e9c','ConairCCL') where itDesc like '%\u6751\u5e9c%'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528 CharIndex \u4ee3\u66ff like\uff0c\u6548\u679c\u66f4\u597d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select d1.Number,d2.ChineseName,d1.MobileNum,d1.Destination,d3.DangerArea \n  from jh_chun2021 d1\n  left join Employees d2 on d1.Number=d2.Number\n  left join jh_danger d3 on CharIndex('\u6df1\u5733',d1.Destination)&gt;0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728A\u8868\u4e2d\u63d2\u5165\u76f8\u6bd4B\u8868\u6b20\u7f3a\u7684\u8bb0\u5f55<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Insert into MouldGoods(MouldNo,ItemID,DataAreaID,ModifiedDate)\n  Select MouldNo,ItemID,'v01',getDate() from '+tmpDB2+' d1 where not exists\n    (select * from MouldGoods where MouldNo=d1.MouldNo and ItemID=d1.ItemID )\n\nInsert into mdQcPlsTable(sono,TypeNo,bLock)\n Select  d1.SoNo,d1.ItemType,0 \n from ieSoList d1 left join mdQcPlsTable d2  on d1.sono=d2.SoNo \n  where d2.sono is null<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u8868\u4e2d\u662f\u5426\u6709\u91cd\u590d\u7684\u8138\u8c31\uff0c\u6ce8\uff1atemplate\u662f\u4fdd\u5b58\u8138\u8c31\u6570\u636e\u7684Image\u7c7b\u578b\u680f\u4f4d\uff0cEnrollNumber\u662f\u4fdd\u5b58\u5458\u5de5ID\u7684int\u7c7b\u578b\u680f\u4f4d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select EnrollNumber,count(*) from \n (select EnrollNumber,CAST(&#91;template] as varbinary(8000)) as FaceId from u500_tblzEnroll) dA\n group by EnrollNumber,FaceId having count(*)>1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5343\u4e07\u7ea7\u6570\u636e\u8868\u4e2d\u67e5\u8be2\u6570\u636e\uff0c\u4e09\u79cd in \u7684\u6548\u7387\u5bf9\u6bd4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select top 38 SPL_POCode+ltrim(PurchId) as PurchId\nfrom PurchTable where (ltrim(PurchId)=@param1 or ltrim(OrderAccount)=@param1 \nor PurchId+DataAreaId in (select distinct PurchId+DataAreaId from PurchLine where ItemID like '%'+@param1+'%'))\n----\u8017\u65f6 6 \u79d2\n\nselect top 38 SPL_POCode+ltrim(PurchId) as PurchId\nfrom PurchTable d1 where (ltrim(PurchId)=@param1 or ltrim(OrderAccount)=@param1 \nor exists (select * from PurchLine d2 where d1.PurchId=d2.PurchId\nand d1.DataAreaId=d2.DataAreaId and d2.ItemId like '%'+@param1+'%'))\n----\u8017\u65f6 1 \u79d2\n\nselect top 38 d1.SPL_POCode+ltrim(d1.PurchId) as PurchId\nfrom PurchTable d1,PurchLine d2\nwhere d1.PurchId=d2.PurchId and d1.DataAreaId=d2.DataAreaId\nand (ltrim(d1.PurchId)=@param1 or ltrim(d1.OrderAccount)=@param1 \nor d2.ItemId like '%'+@param1+'%')\n----\u8017\u65f6 1 \u79d2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b83\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b9e\u6d4b\u663e\u793a\uff0c64\u4f4d\u7684SqlServer2008\u7528\u4e0aOrder By\u540e\u901f\u5ea6\u6162\u5f88\u591a\uff0c\u5e94\u8be5\u5728\u524d\u7aef\u7684\u6570\u636e\u63a7\u4ef6\u91cc\u7528 Sort:=&#8221; \u6765\u6392\u5e8f\u3002<\/li>\n\n\n\n<li>\u6570\u636e\u7c7b\u578b decimal \u548c numeric \u5728\u5c0f\u6570\u5b58\u50a8\u65b9\u9762\u4e00\u6837\uff0cM$\u5efa\u8bae\u4f7f\u7528Decimal\u3002<\/li>\n\n\n\n<li>\u81ea\u52a8\u589e\u957f\u6807\u8bc6(IDENTITY)\u7684\u680f\u4f4d\uff0c\u5728\u8bbe\u8ba1\u671f\u8981\u8003\u8651\u8bb0\u5f55\u6570\u5c01\u9876\u503c\uff0c\u4ee5\u786e\u5b9a\u7528 int \u8fd8\u662f bigint\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u5c3d\u91cf\u7528 UNION ALL \u66ff\u6362 UNION\uff0c\u907f\u514d\u4f7f\u7528OR\u8fd0\u7b97\u7b26\uff0c\u6539\u7528Union All\uff0c\u4e0d\u8981\u5728 like \u6761\u4ef6\u503c\u7684\u524d\u9762\u52a0\u901a\u914d\u7b26%\u3002<\/li>\n\n\n\n<li>IN\u5b50\u53e5\u4e2d\u6761\u4ef6\u4e2a\u6570\u8981\u5c0f\u4e8e100\u4e2a\uff0c\u9884\u4f30\u8d85\u8fc7\u7684\u8bdd\uff0c\u6539\u7528\u5176\u5b83\u65b9\u6848\u3002<\/li>\n<\/ol>\n\n<div class=\"gb-shapes\"><div class=\"gb-shape gb-shape-1\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 1200 350\" preserveAspectRatio=\"none\"><path d=\"M1200 336.7V350H0V0s22.4 276.4 1200 336.7z\"\/><\/svg><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>\u540c\u6837\u4e00\u4e2a\u76ee\u7684\uff0c\u53ef\u4ee5\u7528\u4e0d\u540c\u7684SQL\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\u4f46\u6548\u7387\u4e0a\u4f1a\u6709\u4e0d\u540c\uff0c\u751a\u81f3\u540c\u6837\u7684\u8bed\u53e5\uff0c\u5728\u4e0d\u540c\u7684SQL Server\u7248\u672c &#8230; <a title=\"SQL\u8bed\u53e5\u4f18\u5316\u4e3e\u4f8b\" class=\"read-more\" href=\"http:\/\/www.jh1999.cn\/?p=1981\" aria-label=\"Read more about SQL\u8bed\u53e5\u4f18\u5316\u4e3e\u4f8b\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1981","post","type-post","status-publish","format-standard","hentry","category-kfbj"],"_links":{"self":[{"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/posts\/1981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1981"}],"version-history":[{"count":22,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/posts\/1981\/revisions"}],"predecessor-version":[{"id":2096,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=\/wp\/v2\/posts\/1981\/revisions\/2096"}],"wp:attachment":[{"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1981"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jh1999.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}