RE: Alter Table Add field via JDBC, preparedStatement and Parameter fa

From: Carb Simien [MSFT] (CarbinoS_at_online.microsoft.com)
Date: 08/03/04

  • Next message: Yuval: "Re: SQL Server and Tomcat"
    Date: Tue, 03 Aug 2004 22:02:50 GMT
    
    

    --------------------
    | Thread-Topic: Alter Table Add field via JDBC, preparedStatement and
    Parameter fa
    | thread-index: AcR0xrMPXWJxd53zRn66DN+jgi9f0g==
    | X-WBNR-Posting-Host: 217.146.157.251
    | From: "=?Utf-8?B?ZGJpbmZvcm1hdA==?="
    <dbinformat@discussions.microsoft.com>
    | Subject: Alter Table Add field via JDBC, preparedStatement and Parameter
    fa
    | Date: Wed, 28 Jul 2004 10:17:02 -0700
    | Lines: 15
    | Message-ID: <19D18530-C3A9-4BFE-82C3-B742C37ED580@microsoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.sqlserver.jdbcdriver
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
    | Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: cpmsftngxa10.phx.gbl microsoft.public.sqlserver.jdbcdriver:6211
    | X-Tomcat-NG: microsoft.public.sqlserver.jdbcdriver
    |
    | I want to add a field to an existing MS-SQL-2000-table via
    Microsoft-JDBC-SP3-driver, Version 2.2.0040.
    | I tried to do this with a preparedStatement object for alter table in
    Java and wanted to pass the fieldname and fieldtype via Parameter. Then I
    get the message: cannot find the datatype @P2 (which seems to be the
    internal placeholder for params). What is the mistake or is it not possible
    to use the alter table command with prepared statement and parameters ?
    | Would be great, if someone knows something about it.
    | Here is something of the non working code:
    |
    | PreparedStatement pstmtM1;
    | String sqlParamM1 = "ALTER TABLE LIMESTAB ADD [ ? ] [ ? ]";
    | ...
    | pstmtM1 = conM.prepareStatement(sqlParamM1);
    | pstmtM1.setString (1, "orderno");
    | pstmtM1.setString(2,"varchar");
    | pstmtM1.executeUpdate();
    |
    | Thank's !!
    |
    |

    Hi,

    You cannot submit an ALTER TABLE statement using parameters like this.
    Below is how SQL Server is interpreting your code:

            exec sp_executesql N'ALTER TABLE LIMESTAB ADD [ @P1 ] [ @P2 ]', N'@P1
    nvarchar(4000) ,@P2 nvarchar(4000) ', N'orderno', N'varchar'

    Even in straight T-SQL, you must dynamically build the query string and
    then execute it using either sp_executesql or EXECUTE. Since you are using
    Java, you should just build your string in the code and then execute it
    using a standard Statement object:

            Statement stmt = conn.createStatement();
            String colname = "orderno";
            String coltype = "varchar";
            String sql = "ALTER TABLE LIMESTAB ADD ";
            stmt.executeUpdate(sql + " " + colname + " " + coltype);

    Carb Simien, MCSE MCDBA MCAD
    Microsoft Developer Support - Web Data

    Please reply only to the newsgroups.
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Are you secure? For information about the Strategic Technology Protection
    Program and to order your FREE Security Tool Kit, please visit
    http://www.microsoft.com/security.


  • Next message: Yuval: "Re: SQL Server and Tomcat"

    Relevant Pages

    • Re: Bug - Cannot ALTER field to NULL
      ... Microsoft never claimed JET to be ANSI-92 compliant either. ... to alter the NULL status of a field via SQL. ... accepted it as valid SQL but did not perform the requested action. ... ALTER TABLE test ALTER COLUMN f1 INT NULL ...
      (microsoft.public.access.queries)
    • Re: Web Site Date Stamp has switched to German!!
      ... > How does the affiliates programme work? ... > alter any part of your page? ... Is it implemented using Microsoft ... > technology that perhaps recognises the "feature" of frontpage you are ...
      (uk.net.web.authoring)
    • Re: How can VB6 alter the NAME of an EXISTING FIELD in ACCESS 2002???
      ... Is it possible to have a VB6 app alter the name of an *existing* field ... new field to the Access database. ... Access 10.0 Object Library" and "Microsoft ActiveX Data Objects 2.7 ... ALTER TABLE Table1 ALTER COLUMN OldName NewName INT ...
      (microsoft.public.vb.general.discussion)
    • Re: SqlCommand.CommandTimeout Property
      ... >> Using Microsoft Application Block for Data Access, ...
      (microsoft.public.dotnet.framework.adonet)

    Loading