CREATE / ALTER PROCEDURE'必须是查询批处理中的第一个语句

时间:2014-05-08 16:19:29

标签: java sql sql-server

我如何在java中构造sql查询才能执行多个create语句?

以下是我现在所拥有的:

public static String CreateDatabaseOfflinerObjects(boolean createTables){

    String result = createTables 
            ? 
                    "CREATE TABLE [dbo].[ofLastMessageTime](" +
                       "[id] [int] IDENTITY(1,1) NOT NULL," +
                       "[receiverJid] [nvarchar](max) NOT NULL," +
                       "[senderJid] [nvarchar](max) NOT NULL," +
                       "[lastDeliveredTime] [bigint] NULL," +
                       "[lastReadTime] [bigint] NULL," +

                       "CONSTRAINT [ofLastMessageTime_pk] PRIMARY KEY CLUSTERED " +
                       "(" +
                            "[id] ASC" +
                        ")"+
                        "WITH ("+
                                "PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"+
                              ")" + 
                        "ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] " 
            : "";

    result += 
               (
                  " ; " +

                    "CREATE PROCEDURE [dbo].[UpdateDeliveredStatus] " +

                            "@receiverJid  nvarchar(MAX), " +
                            "@senderJid  nvarchar(MAX), " +
                            "@lastDeliveredTime bigint " +
                            "AS " +
                            "BEGIN " +

                            "SET NOCOUNT ON; " +

                            "UPDATE ofLastMessageTime " +
                            "SET lastDeliveredTime = @lastDeliveredTime " +
                            "WHERE senderJid = @senderJid " +
                            "AND receiverJid = @receiverJid " +

                            "IF @@ROWCOUNT=0 "  +
                            "INSERT INTO ofLastMessageTime ( receiverJid, senderJid, lastDeliveredTime ) " +
                            "VALUES ( @receiverJid,     @senderJid , @lastDeliveredTime ) "+

                        "END" +

                   "; " +

                    "CREATE PROCEDURE [dbo].[UpdateReadStatus] " +

                        "@receiverJid  nvarchar(MAX), " +
                        "@senderJid  nvarchar(MAX), " +
                        "@lastReadTime bigint " +
                        "AS " +
                        "BEGIN " +

                        "SET NOCOUNT ON; " +

                        "UPDATE ofLastMessageTime " +
                        "SET lastReadTime = @lastReadTime " +
                        "WHERE senderJid = @senderJid " +
                        "AND receiverJid = @receiverJid " +

                        "IF @@ROWCOUNT=0 "  +
                        "INSERT INTO ofLastMessageTime ( receiverJid, senderJid, lastReadTime ) " +
                        "VALUES ( @receiverJid,     @senderJid , @lastReadTime ) "+

                    "END" +
                     "; "
              );

    return result;
}

我知道我必须有GO分隔符并且我读到somwhere对于java sql我可以使用分号,但这对我没有帮助。感谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试在Java JDBC批处理中执行此操作,我相信,

http://www.tutorialspoint.com/javaexamples/jdbc_executebatch.htm

我相信代码看起来像:

import java.sql.*;

public class jdbcConn {
    public static void main(String[] args) throws Exception{
      Class.forName("org.apache.sqlserver.jdbc.ClientDriver");
      Connection con = DriverManager.getConnection
      ("jdbc:sqlserver://localhost;databaseName=test;integratedSecurity=true;");
      Statement stmt = con.createStatement
      (ResultSet.TYPE_SCROLL_SENSITIVE,
      ResultSet.CONCUR_UPDATABLE);
      String createTable1 = "CREATE TABLE [dbo].[ofLastMessageTime](" +
                   "[id] [int] IDENTITY(1,1) NOT NULL," +
                   "[receiverJid] [nvarchar](max) NOT NULL," +
                   "[senderJid] [nvarchar](max) NOT NULL," +
                   "[lastDeliveredTime] [bigint] NULL," +
                   "[lastReadTime] [bigint] NULL," +

                   "CONSTRAINT [ofLastMessageTime_pk] PRIMARY KEY CLUSTERED " +
                   "(" +
                        "[id] ASC" +
                    ")"+
                    "WITH ("+
                            "PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"+
                          ")" + 
                    "ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]";
      String createProc1 = "CREATE PROCEDURE [dbo].[UpdateDeliveredStatus] " +

                        "@receiverJid  nvarchar(MAX), " +
                        "@senderJid  nvarchar(MAX), " +
                        "@lastDeliveredTime bigint " +
                        "AS " +
                        "BEGIN " +

                        "SET NOCOUNT ON; " +

                        "UPDATE ofLastMessageTime " +
                        "SET lastDeliveredTime = @lastDeliveredTime " +
                        "WHERE senderJid = @senderJid " +
                        "AND receiverJid = @receiverJid " +

                        "IF @@ROWCOUNT=0 "  +
                        "INSERT INTO ofLastMessageTime ( receiverJid, senderJid, lastDeliveredTime ) " +
                        "VALUES ( @receiverJid,     @senderJid , @lastDeliveredTime ) "+

                    "END";
      con.setAutoCommit(false);
      stmt.addBatch(createTable1);
      stmt.addBatch(createProc1);
      stmt.executeBatch();
      con.commit();
      System.out.println("Batch executed");
      }
} 

答案 1 :(得分:0)

我不相信你能在一个大的声明中做到这一点,除非你的Sql连接技术(例如jdbc / CallableStatement等)支持一个分隔符标记,它将隐式用于分离批处理(如{{ 1}}在使用像GO)这样的工具时会这样做。

您需要做的是为每个表单独创建SSMS(例如作为字符串的集合或数组),然后分别执行每个DDL(例如,在循环中,如果你使用了收集方法)。您不需要每次都创建新连接。

如果您需要一个全有或全无的结果,您也可以在一个ACID事务下包装DDL命令序列。