-- exec sp_helptext add_book1 CREATE proc add_book1 --创建存储过程 @DocCode VARCHAR(30) --创建参数 as BEGIN INSERT INTO book_new_msg ( book_num, book_msg, book_name, book_class, price, ware_people, Docdate, doctype ) SELECT a.book_num, a.boo_msg, a.book_name, a.book_class, a.price, b.ware_people, b.DocDate, b.doctype FROM BOOK_MSG1 a INNER JOIN BOOK_WARE1 b ON a.DocCode = b.Doccode WHERE a.DocCode = @DocCode END BEGIN CREATE TABLE #T (book_num INT, book_sum INT) INSERT INTO #T (book_num, book_sum) SELECT book_num, SUM (book_count) FROM book_msg1 WHERE DocCode= @DocCode GROUP BY book_num UPDATE a SET book_sum = ISNULL(a.book_sum, 0) + b.book_sum FROM book_sun a INNER JOIN #T b ON a.book_num = b.book_num INSERT INTO book_sun (book_num, book_sum) SELECT a.book_num, a.book_sum FROM #t a LEFT JOIN book_sun b ON a.book_num = b.book_num WHERE ISNULL(b.book_num, '') = '' END
改 存储过程利用 inner join 和left join 减少了 if else 判断的操作提高了效率
/* 成品出货日期(取最早)对应 入库的时间 fqh 2019-7-2 */ CREATE PROCEDURE USP_PROD_DELIVERY ( @begindate DATETIME, @enddate DATETIME, @cltcodehd VARCHAR(20) = '', @cltnamehd VARCHAR(100) = '', @stcodehd VARCHAR(20) = '', @stnamehd VARCHAR(50) = '' ) AS BEGIN --返回表 CREATE TABLE #T ( lot_no VARCHAR(20), style VARCHAR(50), modelcode VARCHAR(50), docdate DATETIME, cltcode VARCHAR(20), cltname VARCHAR(100), stcode VARCHAR(20), stname VARCHAR(50), out_qty INT, in_date VARCHAR(10), in_qty INT ) CREATE INDEX IX_#T_LOT_NO ON #T(LOT_NO) INSERT INTO #T(lot_no,docdate,stcode,out_qty,in_date,in_qty) SELECT m.refcodeitem,m.out_date,m.stcode,m.out_qty,n.periodid,n.in_qty FROM ( -- 取出指令最早出库时间&累计出库数 SELECT b.refcodeitem,MIN(a.docdate) AS out_date,a.stcode,SUM(b.basedigit) AS out_qty FROM imatdochd a WITH(NOLOCK) INNER JOIN imatdocitem b WITH(NOLOCK) ON a.doccode = b.doccode WHERE a.docdate >= @begindate AND a.docdate <= @enddate AND (@stcodehd = '' OR a.stcode = @stcodehd) AND (@cltcodehd = '' OR a.cltcode = @cltcodehd) AND a.formid = 2444 AND a.docstatus >= 250 AND LEFT(b.refcodeitem,2) NOT IN('SC') GROUP BY b.refcodeitem,a.stcode ) m INNER JOIN ( -- 取出每次指令入库时间&数量 SELECT b.refcodeitem,a.periodid,a.stcode,SUM(b.basedigit) AS in_qty FROM imatdochd a WITH(NOLOCK) INNER JOIN imatdocitem b WITH(NOLOCK) ON a.doccode = b.doccode WHERE a.formid = 1001 AND a.docstatus >= 100 GROUP BY b.refcodeitem,a.periodid,a.stcode ) n ON m.refcodeitem = n.refcodeitem AND m.stcode = n.stcode UPDATE a SET modelcode = b.modelcode, style = b.style, cltname = b.cltname FROM #t a INNER JOIN proorderhd b WITH(NOLOCK) ON a.lot_no = b.predoccode UPDATE a SET stname = b.stname FROM #t a INNER JOIN ostorage b WITH(NOLOCK) ON a.stcode = b.stcode SELECT *FROM #t ORDER BY docdate ASC DROP TABLE #T END
使用索引降低查询速度, 使用聚合函数 获取查询内容, 使用select 套select 加inner join 获取 链接数据
update 修改 数据 ,利用 ''update set 字段=from 表 inner join 表 on 字段=字段'' 的方式 进行修改
UPDATE pc_forecast_mtl_type set memo=( SELECT a.memo from MTL_STORE_AGE_DETAIL_INPUT a INNER JOIN pc_forecast_mtl_type b on a.mtl_no=b.matcode ) , plandate =( SELECT a.plandate from MTL_STORE_AGE_DETAIL_INPUT a INNER JOIN pc_forecast_mtl_type b on a.mtl_no=b.matcode where a.mtl_no=b.matcode ) update a set memo = b.memo from pc_forecast_mtl_type a inner join MTL_STORE_AGE_DETAIL_INPUT b on a.matcode = b.mtl_no where a.matcode =b.mtl_no
insert into select 的使用
INSERT INTO #t_mtl(vndcode,matcode) SELECT vndcode,matcode FROM proprice_sheet WHERE (ISNULL(@vndcodehd,'') = '' OR vndcode = @vndcodehd) AND (ISNULL(@matcodehd,'') = '' OR matcode = @matcodehd) AND ISNULL(check_flag,0) = 1
返回数据 类型结果
/* 替换核料单里成易料号 为 维涛料号 */ alter PROCEDURE USP_UPT_CY_MTL_NO @doccode VARCHAR(20), @memo VARCHAR(200) OUTPUT AS BEGIN -- 把成易料号转成 维涛料号 UPDATE a SET matcode = b.matcode FROM probomitem a INNER JOIN proimatgeneral b ON a.matcode = cy_code WHERE a.doccode = @doccode --根据维涛料号取得 材料名称等资料 UPDATE probomitem SET matname = b.matname, colortext = b.color, bomspecial = b.standardmode, baseuom = b.uom, uomrate = b.uomrate, baseuomrate = b.baseuomrate FROM probomitem a INNER JOIN proimatgeneral b ON a.matcode = b.matcode WHERE a.doccode = @doccode --取出成易料号无对应的维涛料号资料 SELECT @memo = STUFF((SELECT DISTINCT ','+ a.matcode FROM probomitem a LEFT JOIN proimatgeneral b WITH(NOLOCK) ON a.matcode = b.cy_code WHERE doccode = @doccode AND ISNULL(b.matcode,'') = '' for xml path('')),1,1,'') SET @memo = @memo + ' 材料未编对应料号!' SELECT @memo END
年与时驰,意与日去,遂成枯落,
多不接世,悲守穷庐,将复何及。