Monday, March 19, 2012

Alter Table Add field via JDBC, preparedStatement and Parameter fa

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 !!
| 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.

No comments:

Post a Comment