日期:2014-05-18  浏览次数:20443 次

java中sql语句执行有错误,但是打印出语句后在sql管理中没有问题
java中sql语句执行有错误,但是打印出语句后在sql管理中没有问题
java中代码如下:
String sqlview="if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[员工考勤卡号-按最新启 用日期]') and OBJECTPROPERTY(id, N'IsView') = 1)"+
"drop view [dbo].[员工考勤卡号-按最新启用日期]" +
"\n"+"GO"+"\n"+
"SET QUOTED_IDENTIFIER ON"+  
"\n"+"GO"+"\n"+
"SET ANSI_NULLS ON"+  
"\n"+"GO"+"\n"+
"CREATE VIEW dbo.[员工考勤卡号-按最新启用日期]"+"\n"+
" AS "+"\n"+
"SELECT DISTINCT SMT_PersonnelID, SMT_CardSerial, SMT_DateTime, SMT_State"+"\n"+
"FROM dbo.Smart_Card ta"+"\n"+
"WHERE (SMT_DateTime ="+"\n"+
"(SELECT MAX(tb.SMT_DateTime) AS Expr1"+"\n"+
"FROM dbo.Smart_Card AS tb"+"\n"+
"WHERE (tb.SMT_PersonnelID = ta.SMT_PersonnelID)))"+
"\n"+"GO"+"\n"+
"SET QUOTED_IDENTIFIER OFF"+ 
"\n"+"GO"+"\n"+
"SET ANSI_NULLS ON"+  
"\n"+"GO"+"\n"+
  ;
打印出的语句为:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[员工考勤卡号-按最新启用日期]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[员工考勤卡号-按最新启用日期]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.[员工考勤卡号-按最新启用日期]
 AS 
SELECT DISTINCT SMT_PersonnelID, SMT_CardSerial, SMT_DateTime, SMT_State
FROM dbo.Smart_Card ta
WHERE (SMT_DateTime =
(SELECT MAX(tb.SMT_DateTime) AS Expr1
FROM dbo.Smart_Card AS tb
WHERE (tb.SMT_PersonnelID = ta.SMT_PersonnelID)))
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

这个打印出来的语句直接复制到sqlserver management中是可以执行的 没有问题
但是eclipse中直接报错:com.microsoft.sqlserver.jdbc.SQLServerException: 'GO' 附近有语法错误。

请大家分析下。。。。。。。。。

------解决方案--------------------
GO就执行上面的语句了,这种情况建议你把所有GO之前的拆分下分开执行。
------解决方案--------------------
把程序里的 \n"+"GO"+"\n"+
 都去掉