日期:2014-05-17  浏览次数:20738 次

拼接sql语句时使用go,提示go附近有语法错误
本帖最后由 liuxu430524 于 2014-01-14 09:57:53 编辑
各位好:下面这个SQL语句是访问另外一个服务器中的数据库,在sql中能正常运行,
        然后将这句sql复制到java后台时,提示"go 附近有语法错误",上百度搜,说是go必须独占一行,前后不允许有sql语句,我想问一下拼接后的sql语句在sql2005运行时如何自动换行,如果去掉go,在sql中都会报错
谢谢

exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;
reconfigure;
go

--查询语句

exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
go



------解决方案--------------------
+char(13)  --换行 然后在+ go
------解决方案--------------------
用;号吧,不能用go的,或者你试试:
StringBuffer sql=new StringBuffer("exec sp_configure 'show advanced options',1;")
.append(" reconfigure; ")
.append(" exec sp_configure 'Ad Hoc Distributed Queries',1;")
.append(" reconfigure; char(13) char(13)")
.append(" select sum(attendanceTime) as attendanceTime,SSN,name from ( ")
//查询语句
.append(" exec sp_configure 'Ad Hoc Distributed Queries',0;")
.append(" reconfigure;")
.append(" exec sp_configure 'show advanced options',0;")
.append(" reconfigure; char(13) ");
------解决方案--------------------
建议你把这2个语句,分别分装到存储过程中,分别调用这2个存储过程:



create proc proc_on
as
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;
reconfigure;
go



--查询语句
create proc proc_off
as
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
go