OLá pessoal estou executando uma classe java pra criar tabelas no banco de dados, em criar as tabelas ja tive sucesso, mas não estou conseguindo criar a stored procedure, estou usando o sqlserver
segue a classe
/**
*
*/
package com.atual.atualmobile.server.dao;
import java.sql.SQLException;
import java.sql.Statement;
/**
* AtualMobile 25/02/2009 TODO
*
* @author developer
*
*/
public class ConfiguraDB {
private static Conexao conexao;
static {
gerarTbPedido();
gerarTbItem();
gerarProcedure();
}
private static void executaSql(String sql) {
try {
conexao = new Conexao();
conexao.getConnection().createStatement().execute(sql);
conexao.closeConnection();
}
catch (SQLException e) {
conexao.closeConnection();
e.printStackTrace();
}
finally {
conexao.closeConnection();
}
}
public static void gerarTbPedido() {
StringBuffer sb = new StringBuffer();
sb.append("SET ANSI_NULLS ON ");
sb.append("SET QUOTED_IDENTIFIER ON ");
sb.append("SET ANSI_PADDING ON ");
sb.append("CREATE TABLE [dbo].[tbMobilePedido]( ");
sb.append(" [Num_docto] [int] IDENTITY(1,1) NOT NULL, ");
sb.append(" [Data] [datetime] NULL, ");
sb.append(" [Hora] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" [Status] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" CONSTRAINT [PK_tbMobilePedido] PRIMARY KEY CLUSTERED ");
sb.append("( ");
sb.append(" [Num_docto] ASC ");
sb.append(")WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ");
sb.append(") ON [PRIMARY] ");
sb.append(" ");
sb.append("SET ANSI_PADDING OFF ");
executaSql(sb.toString());
}
public static void gerarTbItem() {
StringBuffer sb = new StringBuffer();
sb.append("SET ANSI_NULLS ON ");
sb.append("SET QUOTED_IDENTIFIER ON ");
sb.append("SET ANSI_PADDING ON ");
sb.append("CREATE TABLE [dbo].[tbMobilePedidoItem]( ");
sb.append(" [Num_docto] [int] NOT NULL, ");
sb.append(" [Num_item] [int] NOT NULL, ");
sb.append(" [Cod_produto] [char](12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" [Cod_ref] [char](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" [Quantidade] [numeric](20, 8) NULL, ");
sb.append(" [Cod_unidade] [char](4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" [Valor] [decimal](15, 4) NULL, ");
sb.append(" [Observacao] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" [Ean] [char](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, ");
sb.append(" CONSTRAINT [PK_tbMobilePedidoItem] PRIMARY KEY CLUSTERED ");
sb.append("( ");
sb.append(" [Num_item] ASC, ");
sb.append(" [Num_docto] ASC ");
sb.append(")WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ");
sb.append(") ON [PRIMARY] ");
sb.append(" ");
sb.append("SET ANSI_PADDING OFF ");
sb
.append("ALTER TABLE [dbo].[tbMobilePedidoItem] WITH CHECK ADD CONSTRAINT [FK_tbMobilePedido_tbMobilePedidoItem] FOREIGN KEY([Num_docto]) ");
sb.append("REFERENCES [dbo].[tbMobilePedido] ([Num_docto]) ");
sb.append("ALTER TABLE [dbo].[tbMobilePedidoItem] CHECK CONSTRAINT [FK_tbMobilePedido_tbMobilePedidoItem] ");
executaSql(sb.toString());
}
public static void gerarProcedure() {
StringBuffer sb = new StringBuffer();
sb.append("SET ANSI_NULLS ON ");
sb.append("SET QUOTED_IDENTIFIER ON ");
sb.append(" ");
sb.append("CREATE PROCEDURE [dbo].[SP_consultaProdutosMobile] ");
sb.append("@codProduto char(12),@ean char(15),@ref char(15) ");
sb.append(" ");
sb.append("as ");
sb.append("declare @sql nvarchar(4000) ");
sb.append("set @sql = ' ");
sb.append(" ");
sb.append("select ");
sb.append("TOP 1 ");
sb.append("TB1.COD_PRODUTO, ");
sb.append("TB1.COD_REF, ");
sb.append("TB2.DESC_PRODUTO_EST, ");
sb.append("TB1.DESC_GRADE, ");
sb.append("TB1.EAN, ");
sb.append("TB2.COD_UNIDADE_PRI, ");
sb.append("ISNULL(LI.PRECO_V1,0) AS VALOR_PRODUTO, ");
sb.append("SUM(ISNULL(SALDO_V1,0)) AS SALDO_ESTOQUE ");
sb.append("FROM TBPRODUTOREF TB1 ");
sb.append("LEFT OUTER JOIN TBPRODUTOSALDO TB0 ");
sb.append("ON TB1.COD_PRODUTO=TB0.COD_PRODUTO AND ");
sb.append("TB1.COD_REF=TB0.COD_REF AND ");
sb.append("TB0.COD_FILIAL IN (''001'',''002'',''003'',''004'') ");
sb.append("INNER JOIN TBPRODUTO TB2 ");
sb.append("ON TB1.COD_PRODUTO=TB2.COD_PRODUTO ");
sb.append("LEFT OUTER JOIN TBLISTAPRECOITEM LI ");
sb.append("ON TB1.COD_PRODUTO=LI.COD_PRODUTO AND ");
sb.append("TB1.COD_REF=LI.COD_REF AND ");
sb.append("LI.COD_LISTA= 1 ");
sb.append("WHERE ");
sb.append(" ' ");
sb.append(" ");
sb.append(" if(@ean is not null) ");
sb.append(" begin ");
sb.append(" set @sql = @sql + '( TB1.EAN = ''' + rTrim(@ean) + ''' ) ' ");
sb.append(" end ");
sb.append(" else ");
sb.append(" begin ");
sb.append(" if(@ref is not null ) ");
sb.append(" begin ");
sb.append(" set @sql = @sql + '(TB0.COD_PRODUTO = ''' + rtrim(@codProduto) + ''' and TB1.COD_REF = ''' + rtrim(@ref) + ''') ' ");
sb.append(" end ");
sb.append(" else ");
sb.append(" begin ");
sb.append(" set @sql = @sql + '(TB0.COD_PRODUTO = '''+ rtrim(@codProduto) + ''' ) ' ");
sb.append(" end ");
sb.append(" ");
sb.append(" end ");
sb.append(" ");
sb.append(" ");
sb.append(" ");
sb.append(" set @sql = @sql + ' ");
sb.append(" and TB1.STATUS = ''A'' ");
sb.append(" GROUP BY ");
sb.append(" TB1.COD_PRODUTO, ");
sb.append(" TB1.COD_REF, ");
sb.append(" TB2.DESC_PRODUTO_EST, ");
sb.append(" TB1.DESC_GRADE, ");
sb.append(" TB1.EAN, ");
sb.append(" TB2.COD_UNIDADE_PRI, ");
sb.append(" LI.PRECO_V1 ");
sb.append(" ORDER BY ");
sb.append(" TB1.COD_PRODUTO, ");
sb.append(" TB1.COD_REF ");
sb.append(" ");
sb.append("' ");
sb.append("execute(@sql) ");
sb.append("print(@sql) ");
sb.append("print ('cod: ' + @codProduto) ");
sb.append("print('ean: ' + @ean) ");
sb.append("print('ref: ' + @ref) ");
try {
conexao = new Conexao();
Statement st = conexao.getConnection().createStatement();
st.addBatch(sb.toString());
st.executeBatch();
st.close();
conexao.closeConnection();
}
catch (SQLException e) {
conexao.closeConnection();
e.printStackTrace();
}
finally {
conexao.closeConnection();
}
}
}
mensagem de erro -->
java.sql.BatchUpdateException: ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch.
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeBatch(Unknown Source)
at com.atual.atualmobile.server.dao.ConfiguraDB.gerarProcedure(ConfiguraDB.java:188)
at com.atual.atualmobile.server.dao.ConfiguraDB.<clinit>(ConfiguraDB.java:22)
at com.atual.atualmobile.server.servlets.ListenerConfigDataBaseTest.testListenerConfigDataBase(ListenerConfigDataBaseTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
alguém ja viu este erro ?