Debug SQL Easily by Charles Carroll

This page demonstrates how you can format a SQL statement to help pinpoint common errors. Common errors are discussed @

Common SQL Errors
http://www.learnasp.com/learn/dbtroubleshoot2.asp

Syntax Error in SQL Statement
http://www.learnasp.com/learn/FAQdbSQLSyntax.asp

by encapsulating ADO error trapping and display the SQL in an attractive form that separates components onto separate lines and makes debugging simpler (missing commas, single-quotes and like).

<Test Script Below>

<html><head>
<title>debug2.asp</title>
</head><body bgcolor="#FFFFFF">
<%
on   error resume next
myDSN = "DSN=Student;uid=student;pwd=magic"
Set Conn = Server.CreateObject("ADODB.Connection")
conn.open myDSN

SQL="update test set fname='Ted',lname='Wilson',city='Rockville',state='MD',zip='20849',rank=7,datehire='1/15/92', SSN='219-92-2677' WHERE personid=7"
Conn.Execute SQL,howmany
IF  howmany="" THEN
   howmany=0
END IF
response.write "Affected <b>" & howmany & "</b> records<br>"
Call SQLerrorreport(SQL,conn)

SQL="Insert Into junk (fname,lname,city,state,zip,rank,datehire,ssn) VALUES ('ted','wilson','rockville', 'md', '20849',7,'1/15/92','219-92-2677')"
Conn.Execute SQL,howmany
IF  howmany="" THEN
   howmany=0
END IF
response.write "Affected <b>" & howmany & "</b> records</b><br>"
Call SQLerrorreport(SQL,conn)

Conn.Close
set conn=nothing
%>

</body></html>
<!--#include file="lib_debug2.asp"-->

The library that does the work:

<%
SUB SQLErrorReport(parmSQL,parm_conn)
   HowManyErrs=parm_conn.errors.count
   IF HowManyErrs=0 then
      exit sub
   END IF
   pad="&nbsp;&nbsp;&nbsp;&nbsp;"
   lb="<br>" & vbcrlf
   comma="<font color=red>"
   squote="<font color=blue>"
   response.write "ADO Error(s) executing:<br>"
   for counter= 0 to HowManyErrs-1
      errornum=parm_conn.errors(counter).number
      errordesc=parm_conn.errors(counter).description
      response.write pad & "Error#=<b>" & errornum & "</b><br>"
      response.write pad & "Error description=<b>"
      response.write errordesc & "</b><p>"
   next
   SQLstmt=parmSQL
   SQLstmt=replace(SQLstmt,",", "<b>" & comma & ",</b></font>" & lb)
   SQLstmt=replace(SQLstmt,"'","<b>" & squote & "'</b></font>")
   SQLstmt=replace(SQLstmt,"(",lb & "(")
   SQLstmt=replace(SQLstmt,"set",lb & "set" & lb)
   SQLstmt=replace(SQLstmt,"SET",lb & "SET" & lb )
   SQLstmt=replace(SQLstmt," where ",lb & " where " & lb)
   SQLstmt=replace(SQLstmt," WHERE ",lb & " WHERE " & lb )
   
   SQLstmt=replace(SQLstmt,")",")" & lb)
         response.write "SQL statement attempted:<br>"
         response.write SQLstmt & "<br>"

END SUB
%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLDebug是八九软件开发的一个免费的智能查询分析器,它支持Access、MSSQL、MYSQL和ORACLE数据库,它的特性有: SQL语法快速自动完成 能根据数据库类型自动补全数据库保留字、表名、字段名、函数等。 强大的脚本编辑器 强大的SQL代码编辑功能,类似VS、Delphi、Java的快捷方式,如果你熟悉以上编写,编辑将会使用很顺手。 可中断的数据查询 查询数据可以随时中断,避免一查100多万条数据,程序长久没有反应。 强大的数据库结构分析 可以查看用户表、系统表、视图、存储过程、函数、用户自定义数据类型,而且表和视图可以查看:表结构、主键、外键、索引;视图和存储过程还可以查看创建SQL脚本。 强大的二次数据分析功能 对查询出来的数据进行二次分析,提供过滤,排序、查找、编辑、导出。特点:支持把字段置为null,显示每个字段的属性(完全和数据库数据类型对应),对长文本的查看,大多SQL查询器在显示很长文本,会耗尽系统资源,对空字段显示,不可编辑字段用红色字体显示,给用户显示更直观。对BLOB字段支持导入导出。 强大的数据导出功能 数据可以导出为:TXT、CSV、HTML、RTF、XLS,而且还用鼠标支持选择一部分数据导出。还支持选择表导出成Access文件,方便携带数据。 很好用的数据库结构比较和数据比较 对同种数据库之间比较表结构不同,特别适合维护几个版本的数据库,而且还可以比较两个表之间的数据。 支持同时查询几个数据库 软件采用独特的界面技术和数据库链接池技术,实现可以同时查询几个不同/相同类别的数据库。 软件界面简洁、美观、大方,而且软件还是免费的,无任何使用和升级的限制。作者保留版权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值