在工作中经常要写SQL语句少则百行,大都是几百行。
在C# 中,把SQL语句赋值为字符串。由于字符串过长,并且要利于可读性,一般会保留SQL格式。复制,粘贴,回车,一上午就贴了一个SQL语句,效果极其低下。
在工作空余时间,自己想有什么好的办法可以快速的贴过来Sql 语句?
1 .使用@ 字符,但是Sql 里面会有参数,还有特殊字符转化,这其中对思维,逻辑考量比较大,稍不留意就会出错。简单短少的语句可以使用。
2 .Vs 编辑器中的自动折行,代码格式化?对sql字符串没有识别作用。
3 .那就试试用Excel 拼接出自己想要的样式。
先用一个一百行以上的sql 语句来试试,复制一个100行的Sql 语句,粘贴到空白表格的D列(随便一列都可以)。
a .一定要以文本的方式粘贴,会自动占据表格的100列(保留Sql格式),不然都会粘贴到一列,会无比糟糕。
b .在c1单元格写公式 ,用拼接字符串‘&’拼出自己想要的格式 =""""&D1&" ""+"
c .然后自动填充,就得到自己想要的代码格式了
d .粘贴到VS编辑器中,把最后一行“+”替换成“;”
不到一分钟搞定。
贴一个代码小片段:
string[] ls_sql = new string[1]; ls_sql[0] = "select a.prdtcode,a.tran_qty,a.proce_no,a.place_order_no,a.sprod_id,a.pro_id,fy.mater,fy.man,fy.make " + "from " + "(select aa.prdtcode,sum(aa.tran_qty) as tran_qty,aa.proce_no,aa.place_order_no,aa.sprod_id,aa.pro_id " + "from " + "( select prdtcode,sum(tran_qty) as tran_qty,case when instr(proce_no,'\\',1) = 0 then proce_no when instr(proce_no,'\\',1) <> 0 then substr(proce_no,1,instr(proce_no,'\\',1) -1) end as proce_no, " + "place_order_no,sprod_id,pro_id " + "from ( " + " SELECT tran_detail.prdtcode, " + " tran_detail.tran_qty, " + " tran_detail.proce_no, " + " tran_master.place_order_no, " + " send_detail.prdtcode as sprod_id, " + " tran_detail.pro_id " + " FROM tran_detail, " + " tran_master,send_detail,material, " + " place_order_master " + " WHERE ( tran_master.tran_no = tran_detail.tran_no ) and " + " ( tran_master.mutuality_no = send_detail.order_no ) and " + " ( tran_detail.seq_no = send_detail.seq_no ) and " + "tran_detail.prdtcode = material.prdtcode and " + "tran_master.place_order_no = place_order_master.place_order_no and " + "material.pro_flag = '2' and " + " ( ( tran_master.kind = 'C' ) AND " + " ( tran_detail.proce_no <> tran_master.place_order_no ) AND " + " ( place_order_master.order_kind <> 'N' ) and " + " ( tran_detail.proce_no not in ('*','A') ) and " + " tran_detail.proce_no like 'ZH%' and " + " ( to_char(tran_master.tran_date,'yyyymm') = '" + day + "') ) " + "union all " + ……