格式化sql代码
㈠ 大家用什么工具格式化sql语句
用这个工具格式化:https://www.toolsou.com/app/sql,支持mysql、Oracle、sql server
㈡ sql server 2008怎么自动格式化sql语句
sql server 2008自动格式化sql语句的具体步骤如下:
1、依照如下步骤打开Sql查询编辑器:开始 → Microsoft SQL Server 2012→ SQL Server Management Studio。
这样就解决了“sql server 2008自动格式化sql语句”的问题。
㈢ sqlyog如何格式化SQL语句,如同plsql里的一样
1、首先打开sqlyog桌面的sqlyog客户端软件。
㈣ sql*plus涓镐庝箞镙煎纺鍖栬緭鍑
set echo off
set feedback off
set newpage none
set pagesize 5000
set linesize 500
set verify off
set pagesize 0
set term off
set trims on
set linesize 600
set heading off
set timing off
set verify off
set numwidth 38
SPOOL c:\drop_table.sql
SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;
1锛庝娇鐢⊿QL*PLUS锷ㄦ佺敓鎴愭壒閲忚剼链
灏唖pool涓巗elect锻戒护缁揿悎璧锋潵浣跨敤锛屽彲浠ョ敓鎴愪竴涓鑴氭湰锛岃剼链涓鍖呭惈链夊彲浠ユ壒閲忔墽琛屾煇涓浠诲姟镄勮鍙ャ
渚1锛
鐢熸垚涓涓鑴氭湰锛屽垹闄SCOTT鐢ㄦ埛涓嬬殑镓链夌殑琛锛
a. 鍒涘缓gen_drop_table.sql鏂囦欢锛屽寘钖濡备笅璇鍙ワ细
SPOOL c:\drop_table.sql
SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;
SPOOL OFF
b. 浠SCOTT鐢ㄦ埛锏诲綍鏁版嵁搴
SQLPLUS > @ 钬..\gen_dorp_table.sql
c. 鍦╟鐩樻牴鐩褰曚笅浼氱敓鎴愭枃浠秃rop_table.sql鏂囦欢锛屽寘钖鍒犻櫎镓链夎〃镄勮鍙ワ纴濡备笅镓绀:
'DROPTABLE'||TABLE_NAME||';'
--------------------------------------------------------------------------------
DROP TABLE DEPT;
DROP TABLE EMP;
DROP TABLE PARENT;
DROP TABLE STAT_VENDER_TEMP;
DROP TABLE TABLE_FORUM;
5 rows selected.
SQL> SPOOL OFF
d. 瀵圭敓鎴愮殑drop_table.sql鏂囦欢杩涜岀紪杈戝幓鎺変笉蹇呰佺殑閮ㄥ垎锛屽彧鐣欎笅drop table 钬..璇鍙
e. 鍦╯cott鐢ㄦ埛涓嬭繍琛宒orp_table.sql鏂囦欢锛屽垹闄scott鐢ㄦ埛涓嬫墍链夌殑琛ㄣ
SQLPLUS > @ c:\dorp_table.sql
鍦ㄤ笂闱㈢殑镎崭綔涓锛屽湪鐢熸垚镄勮剼链鏂囦欢涓浼氭湁澶氢綑镄勫瓧绗︼纴濡傝繍琛岀殑sql璇鍙ワ纴镙囬桡纴鎴栬繑锲炵殑琛屾暟锛岄渶瑕佹垜浠缂栬緫璇ヨ剼链钖庡啀杩愯岋纴缁椤疄闄呯殑镎崭綔甯︽潵璇稿氢笉渚裤傛噿𨱍版槸浜虹殑链镐э纴杩欎绩浣挎垜浠鐢ㄦ洿绠绂呰柉鍗旷殑锷炴硶𨱒ュ疄鐜颁笂闱㈢殑浠诲姟銆
a. 鍒涘缓gen_drop_table.sql鏂囦欢锛屽寘钖濡备笅璇鍙ワ细
set echo off
set feedback off
set newpage none
set pagesize 5000
set linesize 500
set verify off
set pagesize 0
set term off
set trims on
set linesize 600
set heading off
set timing off
set verify off
set numwidth 38
SPOOL c:\drop_table.sql
SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;
SPOOL OFF
b. 浠SCOTT鐢ㄦ埛锏诲綍鏁版嵁瀛欓棴搴
SQLPLUS > @ 钬..\gen_dorp_table.sql
c. 鍦╟鐩樻牴鐩褰曚笅浼氱敓鎴愭枃浠秃rop_table.sql鏂囦欢锛屽寘钖鍒犻櫎镓链夎〃镄勮鍙ワ纴濡备笅镓绀:
DROP TABLE DEPT;
DROP TABLE EMP;
DROP TABLE PARENT;
DROP TABLE STAT_VENDER_TEMP;
DROP TABLE TABLE_FORUM;
d. 鍦╯cott鐢ㄦ埛涓嬭繍琛宒orp_table.sql鏂囦欢锛屽垹闄scott鐢ㄦ埛涓嬫墍链夌殑琛ㄣ
SQLPLUS > @ c:\dorp_table.sql
2锛庡皢涓涓琛ㄤ腑镄勬暟鎹瀵煎嚭鐢熸垚涓涓鏂囨湰鏂囦欢锛屽垪涓庡垪涔嬮棿浠モ,钬濋殧璐哄嚡钥呭紑
set echo off
set feedback off
set newpage none
set pagesize 5000
set linesize 500
set verify off
set pagesize 0
set term off
set trims on
set linesize 600
set heading off
set timing off
set verify off
set numwidth 38
SPOOL c:\drop_table.sql
select DEPTNO || ',' || DNAME FROM DEPT;
SPOOL OFF
灏嗕笂闱㈢殑鍐呭逛缭瀛树负涓涓鏂囨湰鏂囦欢钖庯纴浠scott锏诲綍锛屾墽琛岃ユ枃浠跺悗鏄剧ず缁撴灉锛
10,ACCOUNTING
20,RESEARCH
30,SALES
40,OPERATIONS
阃氲繃涓婇溃镄勪袱涓渚嫔瓙锛屾垜浠鍙浠ュ皢锛
set echo off
set feedback off
set newpage none
set pagesize 5000
set linesize 500
set verify off
set pagesize 0
set term off
set trims on
set linesize 600
set heading off
set timing off
set verify off
set numwidth 38
SPOOL c:\鍏蜂綋镄勬枃浠跺悕
浣犺佽繍琛岀殑sql璇鍙
SPOOL OFF
浣滀负涓涓妯$増锛屽彧瑕佸皢蹇呰佺殑璇鍙ュ亣濡傝繖涓妯$増灏卞彲浠ヤ简銆
鍦╫racle镄勮缉鏂扮増链涓锛岃缮鍙浠ョ敤set colsep锻戒护𨱒ュ疄鐜颁笂闱㈢殑锷熻兘锛
SQL> set colsep ,
SQL> select * from dept;
10,ACCOUNTING ,NEW YORK
20,RESEARCH ,DALLAS
30,SALES ,CHICAGO
40,OPERATIONS ,BOSTON
35,aa ,bb
3锛庡姩镐佺敓鎴恠pool锻戒护镓闇镄勬枃浠跺悕
鍦ㄦ垜浠涓婇溃镄勪緥瀛愪腑锛宻pool锻戒护镓闇瑕佺殑鏂囦欢钖嶉兘鏄锲哄畾镄勚傛湁镞舵垜浠闇瑕佹疮澶﹕pool涓娆★纴骞朵笖姣忔spool镄勬枃浠跺悕閮戒笉鐩稿悓锛屽傛枃浠跺悕鍖呭惈褰揿ぉ镄勬棩链燂纴璇ュ备綍瀹炵幇锻锛
column dat1 new_value filename;
select to_char(sysdate,'yyyymmddhh24mi') dat1 from al;
spool c:\&&filename..txt
select * from dept;
spool off;
4锛庡备綍浠庤剼链鏂囦欢涓寰楀埌WINDOWS鐜澧冨彉閲忕殑鍊硷细
鍦╳indos涓锛
spool c:\temp\%ORACLE_SID%.txt
select * from dept;
...
spool off
鍦ㄤ笂闱㈢殑渚嫔瓙涓锛岄氲繃%ORACLE_SID%镄勬柟寮忓紩鐢ㄧ幆澧冨彉閲廜RACLE_SID镄勫硷纴濡傛灉ORACLE_SID镄勫间负orcl锛屽垯鐢熸垚镄剆pool鏂囦欢钖崭负锛歰rcl.txt
鍦║NIX涓锛
spool c:\temp\$ORACLE_SID.txt
select * from dept;
...
spool off
鍦ㄤ笂闱㈢殑渚嫔瓙涓锛岄氲繃$ORACLE_SID镄勬柟寮忓紩鐢ㄧ幆澧冨彉閲廜RACLE_SID镄勫硷纴濡傛灉ORACLE_SID镄勫间负orcl锛屽垯鐢熸垚镄剆pool鏂囦欢钖崭负锛歰rcl.txt
5锛庡备綍鎸囧畾缂虹渷镄勭紪杈戣剼链镄勭洰褰
鍦╯ql*plus涓锛屽彲浠ョ敤save锻戒护锛屽皢涓娄竴𨱒℃墽琛岀殑sql璇鍙ヤ缭瀛桦埌涓涓鏂囦欢涓锛屼絾鏄濡备綍璁剧疆璇ユ枃浠剁殑缂虹渷鐩褰曞憿锛
阃氲繃SQL> set editfile c:\temp\file.sql 锻戒护锛屽彲浠ヨ剧疆鍏剁己鐪佺洰褰曚负c:\tmpe锛岀己鐪佹枃浠跺悕涓篺ile.sql銆
6锛庡备綍闄ゅ幓琛ㄤ腑鐩稿悓镄勮
镓惧埌鐩稿悓镄勮岋细
SELECT * FROM dept a
WHERE ROWID <> (SELECT MAX(ROWID)
FROM dept b
WHERE a.deptno = b.deptno
AND a.dname = b.dname -- Make sure all columns are compared
AND a.loc = b.loc);
娉ㄩ喷锛
濡傛灉鍙镓绵eptno鍒楃浉钖岀殑琛岋纴涓婇溃镄勬煡璇㈠彲浠ユ敼涓猴细
SELECT * FROM dept a
WHERE ROWID <> (SELECT MAX(ROWID)
FROM dept b
WHERE a.deptno = b.deptno)
鍒犻櫎鐩稿悓镄勮岋细
DELETE FROM dept a
WHERE ROWID <> (SELECT MAX(ROWID
FROM dept b
WHERE a.deptno = b.deptno
AND a.dname = b.dname -- Make sure all columns are compared
AND a.loc = b.loc);
娉ㄦ剰锛氢笂闱㈠苟涓嶅垹闄ゅ垪鍊间负null镄勮屻
7锛庡备綍钖戞暟鎹搴扑腑鎻掑叆涓や釜鍗曞紩鍙(钬欌)
Insert inot dept values(35,钬檃a钬欌欌欌檅b钬,钬檃钬欌檅钬);
鍦ㄦ彃鍏ユ椂锛岀敤涓や釜钬栾〃绀轰竴涓钬欍
8锛庡备綍璁剧疆sql*plus镄勬悳瀵昏矾寰勶纴杩欐牱鍦ㄧ敤@锻戒护镞讹纴灏变笉鐢ㄨ緭鍏ユ枃浠剁殑鍏ㄨ矾寰勚
璁剧疆SQLPATH鐜澧冨彉閲忋
濡傦细
SQLPATH = C:\ORANT\DBS;C:\APPS\SCRIPTS;C:\MYSCRIPTS
9锛嶡涓嶡@镄勫尯鍒鏄浠涔堬纻
@绛変簬start锻戒护锛岀敤𨱒ヨ繍琛屼竴涓狲ql鑴氭湰鏂囦欢銆
@锻戒护璋幂敤褰揿墠鐩褰曚笅镄勶纴鎴栨寚瀹氩叏璺寰勶纴鎴栧彲浠ラ氲繃SQLPATH鐜澧冨彉閲忔悳瀵诲埌镄勮剼链鏂囦欢銆
@@鐢ㄥ湪鑴氭湰鏂囦欢涓锛岀敤𨱒ユ寚瀹氱敤@@镓ц岀殑鏂囦欢涓嶡@镓鍦ㄧ殑鏂囦欢鍦ㄥ悓涓鐩褰曪纴钥屼笉鐢ㄦ寚瀹氩叏璺寰勶纴涔熶笉浠岙QLPATH鐜澧冨彉閲忔寚瀹氱殑璺寰勪腑瀵绘垒鏂囦欢锛岃ュ懡浠や竴鑸鐢ㄥ湪宓屽楄剼链鏂囦欢涓銆
10锛&涓&&镄勫尯鍒
&鐢ㄦ潵鍒涘缓涓涓涓存椂鍙橀噺锛屾疮褰挞亣鍒拌繖涓涓存椂鍙橀噺镞讹纴閮戒细鎻愮ず浣犺緭鍏ヤ竴涓鍊笺
&&鐢ㄦ潵鍒涘缓涓涓鎸佷箙鍙橀噺锛屽氨镀忕敤鐢╠efine锻戒护鎴栧甫new_vlaue瀛楀彞镄刢olumn锻戒护鍒涘缓镄勬寔涔呭彉閲忎竴镙枫傚綋鐢&&锻戒护寮旷敤杩欎釜鍙橀噺镞讹纴涓崭细姣忔¢亣鍒拌ュ彉閲忓氨鎻愮ず鐢ㄦ埛阌鍏ュ硷纴钥屽彧鏄鍦ㄧ涓娆¢亣鍒版椂鎻愮ず涓娆°
濡傦纴灏嗕笅闱涓夎岃鍙ュ瓨涓轰竴涓鑴氭湰鏂囦欢锛岃繍琛岃ヨ剼链鏂囦欢锛屼细鎻愮ず涓夋★纴璁╄緭鍏deptnoval镄勫硷细
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
灏嗕笅闱涓夎岃鍙ュ瓨涓轰竴涓鑴氭湰鏂囦欢锛岃繍琛岃ヨ剼链鏂囦欢锛屽垯鍙浼氭彁绀轰竴娆★纴璁╄緭鍏deptnoval镄勫硷细
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
11锛庡紩鍏镄勭洰镄
Copy锻戒护鍦ㄤ袱涓鏁版嵁搴扑箣闂存嫹璐濇暟鎹镞剁壒鍒链夌敤锛岀壒鍒鏄璇ュ懡浠ゅ彲浠ュ湪涓や釜鏁版嵁搴扑箣闂翠紶阃抣ong鍨嫔瓧娈电殑鏁版嵁銆
缂虹偣锛
鍦ㄤ袱涓鏁版嵁搴扑箣闂翠紶阃掓暟鎹镞讹纴链夊彲鑳戒涪澶辩簿搴(lose precision)銆
12锛庨梾浠涔埚湪淇鏀瑰ぇ閲忕殑琛屾椂锛屾垜镄勮剼链浼氩彉寰楀緢鎱锛
褰挞氲繃PL/SQL鍧椾慨鏀逛竴涓琛ㄤ腑镄勮稿氲屾椂锛屼綘浼氩垱寤哄湪琛ㄤ笂鍒涘缓涓涓猚ursor锛屼絾鏄鍙链夊湪浣犲叧闂珰ursor镞讹纴镓崭细閲婃斁ROLLBACK SEGMENT锛岃繖镙凤纴褰揷ursor浠岖劧镓揿紑镞讹纴淇鏀硅繃绋嬩细鍙樻参锛岃繖鏄锲犱负鏁版嵁搴扑笉寰椾笉鎼滃诲ぇ閲忕殑rollback segment浠ヤ究浜庣淮鎶よ讳竴镊存с备负浜嗛伩鍏嶈繖镙锋儏鍐碉纴璇旷潃鍦ㄨ〃涓婂姞涓涓镙囧织瀛楁垫潵鎻忚堪璇ヨ屾槸钖﹀凡缁忚淇鏀癸纴铹跺悗鍏抽棴璇cursor锛岀劧钖庡啀镓揿紑璇 cursor銆傛疮娆″彲浠ヤ慨鏀5000琛.
SPOOL OFF
闇瑕佸啓涓涓绠鍗旷殑鑴氭湰锛屽皢镓链夋暟鎹鏂囦欢銆佹带鍒舵枃浠躲佸湪绾挎棩蹇楁枃浠剁瓑杈揿嚭鍒颁竴涓鏂囦欢涓锛屼竴鍙f皵鍐椤畬涔嫔悗锛屽彂鐜拌緭鍑虹粨鏋滀腑姣忚屼箣闂撮兘链変竴绌鸿岋纴闱炲父涓岖埥锛岀爷绌朵简涓涓 sqlplus 镄勮剧疆鍙傛暟涔嫔悗瑙e喅浜嗛梾棰桡纴鎶婂嚑涓甯哥敤镄勫弬鏁板湪杩欓噷锅氢竴涓灏忕粨锛屾柟渚夸互钖庢煡璇銆
set echo on/off 鏄钖︽樉绀鸿剼链涓镄勯渶瑕佹墽琛岀殑锻戒护
set feedback on/off 鏄钖︽樉绀 select 缁撴灉涔嫔悗杩斿洖澶氩皯琛岀殑鎻愮ず
set linesize n 璁剧疆涓琛屾渶澶氭樉绀哄氩皯瀛楃︼纴涔嫔墠灏辨槸锲犱负 n 璁惧缑杩囧ぇ锛屽艰嚧琛屼笌琛屼箣闂存湁绌虹槠琛
set termout on/off 鍦ㄦ墽琛岃剼链镞舵槸钖﹀湪灞忓箷涓婅緭鍑虹粨鏋滐纴濡傛灉 spool 鍒版枃浠跺彲浠ュ皢鍏跺叧闂
set heading on/off 鏄钖︽樉绀烘煡璇㈢粨鏋灭殑鍒楀悕锛屽傛灉璁剧疆涓 off锛屽皢鐢ㄧ┖锏借屼唬镟匡纴濡傛灉瑕佸幓闄よョ┖锏借岋纴鍙浠ョ敤 set pagesize 0
set pagesize n 璁剧疆姣忛〉镄勮屾暟锛屽皢 n 璁句负 0 鍙浠ヤ笉鏄剧ず镓链 headings, page breaks, titles, the initial blank line, and other formatting information
set trimspool on/off 鍦 spool 鍒版枃浠舵椂鏄钖﹀幓闄よ緭鍑虹粨鏋滀腑琛屾汤灏剧殑绌虹槠瀛楃︼纴涔嫔墠镄勯殧琛屽彲浠ョ敤璇ュ弬鏁板幓鎺夛纴鍜岃ュ弬鏁板瑰簲镄勬槸 trimout锛屽悗钥呯敤浜庡睆骞曡緭鍑
set trimout on/off 鏄钖﹀幓鎺夊睆骞曚笂杈揿嚭缁撴灉琛屾汤灏剧殑绌虹槠瀛楃
SQL> select * from aa;
BB CC
---------- ----------
1 999999.99
SQL> col bb format ''''99.9'''';
SQL> select * from aa;
BB CC
----- ----------
1.0 999999.99
SQL> col bb head ''''99.9'''';
SQL> select * from aa;
99.9 CC
----- ----------
1.0 999999.99
SQL> col bb head ''''瀛﹀彿'''';
SQL> select * from aa;
瀛﹀彿 CC
----- ----------
1.0 999999.99
SQL> col bb format ''''99'''';
SQL> select * from aa;
瀛﹀彿 CC
---- ----------
1 999999.99
SQL> col bb head ''''瀛|鍙'''';
SQL> select * from aa;
瀛
鍙 CC
--- ----------
1 999999.99
SQL> select count(bb),sum(cc) from aa;
COUNT(BB) SUM(CC)
---------- ----------
8192 8191999918
SQL> col sum(cc) format ''''9999999999.99'''';
SQL> select count(bb),sum(cc) from aa;
COUNT(BB) SUM(CC)
---------- --------------
8192 8191999918.08
SQL> col sum(cc) format ''''9999999999.99'''' head ''''镐诲垎'''';
SQL> col count(bb) format ''''999999'''' head ''''浜烘暟'''';
SQL> select count(bb),sum(cc) from aa;
浜烘暟 镐诲垎
------- --------------
8192 8191999918.08
㈤ dbvisualizer怎样格式化sql语句
打开dbvisualizer,书写一个比较复杂的SQL语句。
(1)在编辑区域,或者在sql语句上,点击鼠标右键。
(2)选择Format Sql->Format Current
可以看到SQL语句已经被格式化了。
说明:
Format Current,是格式化当前光标所在处,或者被选中的SQL。
Format Buffer,是格式化当前编辑区内所有的SQL。
我们通常使用前者。
如果您认为格式化之后的效果不够好,可以自己进行调整。
点击菜单栏上的【Tools】,再点击【Tool Properties...】
可以看到SQL Formatting区域:
Align and Indent是设置对齐和缩进。
Line Breaks是设置换行的位置。
Whitespace是设置空格
比较常用的是修改换行方式:
(1)点击Line Breaks选项卡
(2)将After Comma的勾选去掉,不需要在逗号后面就换行。
(3)将After "SELECT,FROM,WHRE,..."的勾选能去掉,不需要在每个关键字后面都换行,比如from后面,where后面不需要换行。
(4)点击【OK】按钮保存即可。
注:格式化快捷键Ctrl+Shift+F
㈥ 鍦╬gAdmin III涓镄凷QL缂栬緫鍣ㄧ獥鍙i噷镐庝箞镙煎纺鍖朣QL璇鍙
toad涓镙煎纺鍖杝ql璇鍙ョ殑镎崭綔鏂规硶锛1銆侀夋嫨𨱍宠侀夊疗钖电夸腑镄勮鍙ワ纴鍗曞嚮缂栬緫宸ュ叿镙忎笂镄勬牸寮忓寲浠g爜(FormatCode)锛屾垨阃夋嫨涓嬫媺绠澶撮夋嫨锛氢粎镙煎纺鍖栧ぇ灏忓啓銆侀厤缃鏂囦欢浠g爜锛屾垨镙煎纺鍖栫▼搴忛夐”锛堟墦寮阃夐”璐ュ瓭(Options)绐楀彛锛夈2銆佽佹牸寮忓寲鏁翠釜鑴氱扮楁湰鍗曞嚮缂栬緫
㈦ notepad++ 怎么格式化代SQL码
方法/步骤
1、下载安装notepad++
2、打开notepad++,选择“插件漏宽派”-“Plugin Manager”-“Show Plugin Manager”返贺
3、在“Plugin Manager”中,找到“PoorMansTSqlFormatter”插件并安装。有时候因为网络问题,会失败,多尝试几次!
4、安装好插件后,简单巧培配置下。
5.点击格式化,看效果
㈧ sql中格式化字符串或时间,遇到多少,写多少,持
--求开始时间和结束时间相差的天数
DECLARE @result int
DECLARE @StartDayStartTime DATETIME
DECLARE @EndDayEndTime DATETIME
set @result=DATEDIFF (d,CONVERT(Datetime,@StartDay),CONVERT(Datetime,@EndDay))
--将2012-07-12 15:56:53 格式化为20120712 ,这种需求大多是因为每日数据量更新过大,所以为每天创建一张表,在实现跨多天查询的时候,你懂得。
--执行sql,并返回,最后求和,@sql务必要声明为nvarchar类型
declare @sql nvarchar(500)
declare @sm decimal(18,2) --开始里程
declare @em decimal(18,2)--结束里程
declare @sum decimal(18,2)--当天时间段内的里程
declare @temp decimal(18,2)--里程变量
set @sql='SELECT top 1 @temp=Convert(decimal(18,2),'+@GpsPath+'.Mileage) FROM '+@GpsPath+' WHERE GpsTime<='''+CONVERT(varchar(100),@E, 21)+''' AND TerminalNo=''02200051'' and '+@GpsPath+'.Mileage>0 ORDER BY GpsTime desc'
exec sp_executesql @sql,N'@temp decimal(18,2) output',@em out print @em
set @sql='SELECT top 1 @temp=Convert(decimal(18,2),'+@GpsPath+'.Mileage) FROM '+@GpsPath+' WHERE GpsTime>='''+CONVERT(varchar(100),@S, 21)+''' AND TerminalNo=''02200051'' and '+@GpsPath+'.Mileage>0 ORDER BY GpsTime asc'
exec sp_executesql @sql,N'@temp decimal(18,2) output',@sm out print @sm
set @sum=Convert(decimal(18,2),@em)-Convert(decimal(18,2),@sm)