Showing posts with label existing. Show all posts
Showing posts with label existing. Show all posts

Thursday, March 29, 2012

Alternating background colors in table

Is there anyway to have alternating background colors in a table that is in a list? The existing methods I've seen work except that the tables don't always start with the same background color, as so:

BEGIN LIST
TABLE 1
(white background)
(gainsboro background)
(white background)

TABLE 2
(gainsboro background)
(white background)
(gainsboro background)
END LIST

Hopefully this gives you an idea of what I'm talking about. In table 2 above, the first row should have a white background, just like in table 1. It appears that SSRS treats all rows within a list as 1 table.

Is there any solution for this problem?

Hi,

try this:


=Iif(RowNumber(Nothing) Mod 2, "gainsboro ", "White")

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

|||Thanks Jens, but that's what I'm already using. The color alternates, but it doesn't distinguish between occurrences of the table within a single list.|||

THen you have to provide the scope in the paranthetes. So replace the word Nothing with the name of the scope.

HTH, Jens Suessmeyer


http://www.sqlserver2005.de

Sunday, March 25, 2012

ALTER'ing an XML Column

I was wondering, is it possible to ALTER and existing XML column? I need the abiltiy to be able to type the column to a different xsd if the need arose. For example, the column was created and typed to an XSD called UserClaimsXSD. Then later on, the XSD itself changes and needs to be typed to the column.

I was looking through ALTER table but couldn't find anything. I ran SQL PRofiler and saw it is handled via SSMS - it builds a new table, with that new XSD and then copies the adta from the original table into the temp table. It then drops the original table and renames the temp table...

There has to be an easier way...

Thanks!!

You can first alter the column to untyped xml:

ALTER TABLE YourTable ALTER COLUMN xmlColumn xml NOT NULL;

Then modify or create the new schema

Lastly, change the column to the new schema (the data in the table must comply with the new schema)

ALTER TABLE YourTable ALTER COLUMN xmlColumn xml(NewSchemaCollection) NOT NULL;

ALTER'ing an XML Column

I was wondering, is it possible to ALTER and existing XML column? I need the abiltiy to be able to type the column to a different xsd if the need arose. For example, the column was created and typed to an XSD called UserClaimsXSD. Then later on, the XSD itself changes and needs to be typed to the column.

I was looking through ALTER table but couldn't find anything. I ran SQL PRofiler and saw it is handled via SSMS - it builds a new table, with that new XSD and then copies the adta from the original table into the temp table. It then drops the original table and renames the temp table...

There has to be an easier way...

Thanks!!

You can first alter the column to untyped xml:

ALTER TABLE YourTable ALTER COLUMN xmlColumn xml NOT NULL;

Then modify or create the new schema

Lastly, change the column to the new schema (the data in the table must comply with the new schema)

ALTER TABLE YourTable ALTER COLUMN xmlColumn xml(NewSchemaCollection) NOT NULL;

ALTER USER WITH LOGIN

What's the syntax to use the ALTER USER command to remap an orphaned user to
an existing login in SS2005?It should be:
ALTER USER user_name WITH LOGIN = login_name
Replace the <user_name> and <login_name> accordingly with the orphaned user
and the existing login.
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||Do you know about the "sp_change_users_login" sp?
You can check it out from the following link if you don't.
http://technet.microsoft.com/en-us/...y/ms174378.aspx
Ekrem ?nsoy
"ken s" <kens@.discussions.microsoft.com> wrote in message
news:8C6629E3-C7D7-467A-A54E-0D9DC5F480F9@.microsoft.com...
> What's the syntax to use the ALTER USER command to remap an orphaned user
> to
> an existing login in SS2005?|||The only catch is it that sp_change_users_login works only for SQL Server
logins, while ALTER USER WITH LOGIN supports both SQL Server and Windows
logins.
Plamen Ratchev
http://www.SQLStudio.comsql

ALTER USER WITH LOGIN

What's the syntax to use the ALTER USER command to remap an orphaned user to
an existing login in SS2005?It should be:
ALTER USER user_name WITH LOGIN = login_name
Replace the <user_name> and <login_name> accordingly with the orphaned user
and the existing login.
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||Do you know about the "sp_change_users_login" sp?
You can check it out from the following link if you don't.
http://technet.microsoft.com/en-us/library/ms174378.aspx
--
Ekrem Ã?nsoy
"ken s" <kens@.discussions.microsoft.com> wrote in message
news:8C6629E3-C7D7-467A-A54E-0D9DC5F480F9@.microsoft.com...
> What's the syntax to use the ALTER USER command to remap an orphaned user
> to
> an existing login in SS2005?|||The only catch is it that sp_change_users_login works only for SQL Server
logins, while ALTER USER WITH LOGIN supports both SQL Server and Windows
logins.
Plamen Ratchev
http://www.SQLStudio.com|||Yup, I already know that but Ken did not mention this need. I did not need
to mentioned this catch because it's already declared in the link I gave.
Alternative is alternative.
--
Ekrem Önsoy
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:15E92A6A-2E39-40B8-A9F9-0F90007BB3D7@.microsoft.com...
> The only catch is it that sp_change_users_login works only for SQL Server
> logins, while ALTER USER WITH LOGIN supports both SQL Server and Windows
> logins.
> Plamen Ratchev
> http://www.SQLStudio.com|||That worked fine on my development machine, but on the server I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'psts_web'.
Here's the code I tried to run:
ALTER USER psts_web WITH LOGIN psts_web
"psts_web" is the user name and the login name and they both exist in the db.
Thanks
/Ken|||Hi Ken,
You used incorrect syntax. It should be:
ALTER USER psts_web WITH LOGIN = psts_web
Note the part "... LOGIN = psts_web", you were missing the "=".
HTH,
Plamen Ratchev
http://www.SQLStudio.com

ALTER USER WITH LOGIN

What's the syntax to use the ALTER USER command to remap an orphaned user to
an existing login in SS2005?
It should be:
ALTER USER user_name WITH LOGIN = login_name
Replace the <user_name> and <login_name> accordingly with the orphaned user
and the existing login.
HTH,
Plamen Ratchev
http://www.SQLStudio.com
|||Do you know about the "sp_change_users_login" sp?
You can check it out from the following link if you don't.
http://technet.microsoft.com/en-us/library/ms174378.aspx
Ekrem ?nsoy
"ken s" <kens@.discussions.microsoft.com> wrote in message
news:8C6629E3-C7D7-467A-A54E-0D9DC5F480F9@.microsoft.com...
> What's the syntax to use the ALTER USER command to remap an orphaned user
> to
> an existing login in SS2005?
|||The only catch is it that sp_change_users_login works only for SQL Server
logins, while ALTER USER WITH LOGIN supports both SQL Server and Windows
logins.
Plamen Ratchev
http://www.SQLStudio.com
|||Yup, I already know that but Ken did not mention this need. I did not need
to mentioned this catch because it's already declared in the link I gave.
Alternative is alternative.
Ekrem nsoy
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:15E92A6A-2E39-40B8-A9F9-0F90007BB3D7@.microsoft.com...
> The only catch is it that sp_change_users_login works only for SQL Server
> logins, while ALTER USER WITH LOGIN supports both SQL Server and Windows
> logins.
> Plamen Ratchev
> http://www.SQLStudio.com
|||That worked fine on my development machine, but on the server I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'psts_web'.
Here's the code I tried to run:
ALTER USER psts_web WITH LOGIN psts_web
"psts_web" is the user name and the login name and they both exist in the db.
Thanks
/Ken
|||Hi Ken,
You used incorrect syntax. It should be:
ALTER USER psts_web WITH LOGIN = psts_web
Note the part "... LOGIN = psts_web", you were missing the "=".
HTH,
Plamen Ratchev
http://www.SQLStudio.com

ALTER TABLE/COLUMN syntax

Hi!
I want a add default value to existing column with int type with
following syntax:
ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
but got error
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.
Server SQL 2005 x64, in server Help Contents i see example
ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
Corporation'
What i do wrong ?
Sry my poor Eng.
AndrisSimply add the default with an ALTER TABLE:
ALTER TABLE firmNoliktava_test
ADD CONSTRAINT DF1_firmNoliktava_test
DEFAULT 1 FOR valstsID
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"Andris" <spameris@.gmail.com> wrote in message
news:eY4DCdHiGHA.3956@.TK2MSFTNGP02.phx.gbl...
Hi!
I want a add default value to existing column with int type with
following syntax:
ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
but got error
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.
Server SQL 2005 x64, in server Help Contents i see example
ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
Corporation'
What i do wrong ?
Sry my poor Eng.
Andris|||On Mon, 05 Jun 2006 11:08:02 +0300, Andris wrote:

>Hi!
>I want a add default value to existing column with int type with
>following syntax:
>ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
>but got error
>Msg 156, Level 15, State 1, Line 2
>Incorrect syntax near the keyword 'SET'.
>Server SQL 2005 x64, in server Help Contents i see example
>ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
>Corporation'
>
>What i do wrong ?
Hi Andris,
The example you have seen is not for SQL Server, but for SQL Server
Mobile edition. There are many syntax difference between "normal" SQL
Server and the mobile version. I've been tricked by this myself quite a
few times already - just remember to always check the heading of the
subject in Books Online to check if you're looking at a Mobile or a
T-SQL subject.
Hugo Kornelis, SQL Server MVP

ALTER TABLE/COLUMN syntax

Hi!
I want a add default value to existing column with int type with
following syntax:
ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
but got error
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.
Server SQL 2005 x64, in server Help Contents i see example
ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
Corporation'
What i do wrong ?
Sry my poor Eng.
AndrisSimply add the default with an ALTER TABLE:
ALTER TABLE firmNoliktava_test
ADD CONSTRAINT DF1_firmNoliktava_test
DEFAULT 1 FOR valstsID
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"Andris" <spameris@.gmail.com> wrote in message
news:eY4DCdHiGHA.3956@.TK2MSFTNGP02.phx.gbl...
Hi!
I want a add default value to existing column with int type with
following syntax:
ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
but got error
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SET'.
Server SQL 2005 x64, in server Help Contents i see example
ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
Corporation'
What i do wrong ?
Sry my poor Eng.
Andris|||On Mon, 05 Jun 2006 11:08:02 +0300, Andris wrote:
>Hi!
>I want a add default value to existing column with int type with
>following syntax:
>ALTER TABLE firmNoliktava_test ALTER COLUMN valstsID SET DEFAULT (1)
>but got error
>Msg 156, Level 15, State 1, Line 2
>Incorrect syntax near the keyword 'SET'.
>Server SQL 2005 x64, in server Help Contents i see example
>ALTER TABLE MyCustomers ALTER COLUMN CompanyName SET DEFAULT 'A. Datum
>Corporation'
>
>What i do wrong ?
Hi Andris,
The example you have seen is not for SQL Server, but for SQL Server
Mobile edition. There are many syntax difference between "normal" SQL
Server and the mobile version. I've been tricked by this myself quite a
few times already - just remember to always check the heading of the
subject in Books Online to check if you're looking at a Mobile or a
T-SQL subject.
--
Hugo Kornelis, SQL Server MVP

Alter table with PRIMARY KEY

I have an existing table (with records) having the ff: structure:

CREATE TABLE [dbo].[TEMP2_WORKORDER] (
[WorkOrderID] [int] IDENTITY (1, 1) NOT NULL ,
[JobType] [varchar] (3) NULL ,
[JobID] [varchar] (10) NULL ,

I want to be modify the structure to add a PRIMARY KEY to the [WorkOrderID] column.

I was using ALTER TABLE but can't get the right syntax. Please help!

ThanksALTER TABLE dbo.TEMP2_WORKORDER ADD CONSTRAINT
PK_testtable PRIMARY KEY CLUSTERED
(
WorkOrderID
)|||Thank you. It did the trick!

Alter table with Merge replication

Hi,
I have SQL server 2000 merge replication environment.
How can i propagate Add default constraint command on an existing
column withuot runnning the command on every subscriber.
I know i can use sp_repladdcolumn to add a column in the publisher and
let it propaget to all the subscribers, i want the same behaviour but
this time i am only adding a default constraint.
Thanks in Advance.Check out sp_addscriptexec in the SQL BOL.
HTH
Jerry
<bimalfernando@.gmail.com> wrote in message
news:1127788017.704066.291960@.o13g2000cwo.googlegroups.com...
> Hi,
> I have SQL server 2000 merge replication environment.
> How can i propagate Add default constraint command on an existing
> column withuot runnning the command on every subscriber.
> I know i can use sp_repladdcolumn to add a column in the publisher and
> let it propaget to all the subscribers, i want the same behaviour but
> this time i am only adding a default constraint.
> Thanks in Advance.
>

Thursday, March 22, 2012

Alter Table question

(using SQL Server 2000)
I notice that in the enterprise manager, I can insert a column into an
existing table at any position that I want (so, if my table has 3 columns,
and I want to add a fourth, I can put the column at the end, but I could
also insert it between the 1st and second columns).
Is there a way to do that with an SQL Alter Table statement (control
position of the new column)?No. If you script the code that EM uses you will see that it actually
creates a new table from scratch and then populates it with the old data.
--
David Portas
--
Please reply only to the newsgroup
--
"J.Marsch" <jeremy@.ctcdeveloper.com> wrote in message
news:OhX4r3TqDHA.2216@.TK2MSFTNGP12.phx.gbl...
> (using SQL Server 2000)
> I notice that in the enterprise manager, I can insert a column into an
> existing table at any position that I want (so, if my table has 3 columns,
> and I want to add a fourth, I can put the column at the end, but I could
> also insert it between the 1st and second columns).
> Is there a way to do that with an SQL Alter Table statement (control
> position of the new column)?
>
>|||Wow. That response was just about instantaneous. Thank you!
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:KNSdnc3thOvL-i-iRVn-tA@.giganews.com...
> No. If you script the code that EM uses you will see that it actually
> creates a new table from scratch and then populates it with the old data.
> --
> David Portas
> --
> Please reply only to the newsgroup
> --
> "J.Marsch" <jeremy@.ctcdeveloper.com> wrote in message
> news:OhX4r3TqDHA.2216@.TK2MSFTNGP12.phx.gbl...
> > (using SQL Server 2000)
> > I notice that in the enterprise manager, I can insert a column into an
> > existing table at any position that I want (so, if my table has 3
columns,
> > and I want to add a fourth, I can put the column at the end, but I could
> > also insert it between the 1st and second columns).
> >
> > Is there a way to do that with an SQL Alter Table statement (control
> > position of the new column)?
> >
> >
> >
>

Tuesday, March 20, 2012

ALTER TABLE dateAuto

I must add to an existing TABLE a column DateInsert
with a default value = date auto
if a new row is added the column must add datetime.now automaticly (like in acccess 2000) how can I do it ?
for MS SQL 2000
thank youCREATE TABLE #patp (
id INT IDENTITY
, asof DATETIME NOT NULL
DEFAULT GetDate()
, other VARCHAR(10) NULL
)

INSERT #patp (other) VALUES ('One')
INSERT #patp (other) VALUES ('Two')
INSERT #patp (other) VALUES ('Three')

SELECT * FROM #patp
-PatP|||alter table MyTable Add
DateAuto datetime CONSTRAINT DF_DateAuto DEFAULT (GetDate())|||thank you Pat Phelan and hmscott

great ! and fast !!!

Alter table and size changes

Question that I hope someone can direct me.
I do alter table command to (mostly) add columns to an existing table,
but sometimes to change the size of an existing column (such as char(30)
changing to char(50)).
I remember reading that changing the sze will actually leave the 30
character area and add at the end the 50 character, so the table is
taking up 80 characters instead of 50 (if this is wrong let me know).
So I am thinking I need to do a command that compares the size of the
existing table with the size of the table if it had been created
correctly using the create table command. How can I do that or is it
necessary?
Thanks.
Darin
*** Sent via Developersdex http://www.examnotes.net ***Hi, Darin
This happens only for fixed-length datatypes (for example char, but not
varchar). You can look at the xoffset column in sysobjects to see the
difference.
Try this, for example:
CREATE TABLE T1 (X char(1000), Y char(500))
ALTER TABLE T1 ALTER COLUMN X char(2000)
CREATE TABLE T2 (A char(2000), B char(500))
SELECT o.name as TableName, c.name as ColumnName, c.length, c.xoffset
FROM syscolumns c INNER JOIN sysobjects o ON c.id=o.id
WHERE o.name IN ('T1','T2')
DROP TABLE T1, T2
You will get something like this:
TableName ColumnName length xoffset
-- -- -- --
T1 X 2000 1504
T1 Y 500 1004
T2 A 2000 4
T2 B 500 2004
This may indicate that the row size for T1 is about 3506 bytes, whereas
the row size for T2 is about 2506 bytes.
For more informations, see:
http://www.nigelrivett.net/SQLAdmin...leProblems.html
However, I think you should worry about this problem only if are using
large columns with fixed-length data types. Usually, the large columns
are variable-length data types, i.e. varchar(50), not char(50), so this
is rarely a problem with a big impact.
Razvansql

Monday, March 19, 2012

Alter Table Alter Column

I need to Alter the multiple column of an Existing table
ALter TABLE Address
Alter Coumn Address1 varchar(250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
Address2 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
Address3 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL
But this gives me an error
please advice me on this
thanks
samayYou have to have a separate ALTER TABLE statement for each ALTER COLUMN I'm
afraid.
--
David Portas
SQL Server MVP
--

Alter Table Alter Column

I need to Alter the multiple column of an Existing table
ALter TABLE Address
Alter Coumn Address1 varchar(250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
Address2 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
Address3 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL
But this gives me an error
please advice me on this
thanks
samayYou have to have a separate ALTER TABLE statement for each ALTER COLUMN I'm
afraid.
David Portas
SQL Server MVP
--

Alter Table Alter Column

I need to Alter the multiple column of an Existing table
ALter TABLE Address
Alter Coumn Address1 varchar(250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
Address2 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
Address3 varchar(250)COLLATE SQL_Latin1_General_CP1_CI_AS NULL
But this gives me an error
please advice me on this
thanks
samay
You have to have a separate ALTER TABLE statement for each ALTER COLUMN I'm
afraid.
David Portas
SQL Server MVP

Alter Table Alter Column

I would like to add an Identity to an existing column in a table using a
stored procedure then add records to the table and then remove the identity
after the records have been added or something similar.
here is a rough idea of what the stored procedure should do. (I do not know
the syntax to accomplish this can anyone help or explain this?

Thanks much,

CBL

CREATE proc dbo.pts_ImportJobs
as

/* add identity to [BarCode Part#] */
alter table dbo.ItemTest
alter column [BarCode Part#] [int] IDENTITY(1, 1) NOT NULL

/* add records from text file here */

/* remove identity from BarCode Part#] */
alter table dbo.ItemTest
alter column [BarCode Part#] [int] NOT NULL
return

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

here is the original table

CREATE TABLE [ItemTest] (
[BarCode Part#] [int] NOT NULL ,
[File Number] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_File Number] DEFAULT (''),
[Item Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_Item Number] DEFAULT (''),
[Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_Description] DEFAULT (''),
[Room Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_Room Number] DEFAULT (''),
[Quantity] [int] NULL CONSTRAINT [DF_ItemTest_Quantity] DEFAULT (0),
[Label Printed Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Label Printed Cnt]
DEFAULT (0),
[Rework] [bit] NULL CONSTRAINT [DF_ItemTest_Rework] DEFAULT (0),
[Rework Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Rework Cnt] DEFAULT (0),
[Assembly Scan Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Assembly Scan Cnt]
DEFAULT (0),
[BarCode Crate#] [int] NULL CONSTRAINT [DF_ItemTest_BarCode Crate#] DEFAULT
(0),
[Assembly Group#] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_Assembly Group#] DEFAULT (''),
[Assembly Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_ItemTest_Assembly Name] DEFAULT (''),
[Import Date] [datetime] NULL CONSTRAINT [DF_ItemTest_Import Date] DEFAULT
(getdate()),
CONSTRAINT [IX_ItemTest] UNIQUE NONCLUSTERED
(
[BarCode Part#]
) ON [PRIMARY]
) ON [PRIMARY]
GO"me" <me@.work.com> wrote in message
news:10crrjkjjttmbbe@.corp.supernews.com...
> I would like to add an Identity to an existing column in a table using a
> stored procedure then add records to the table and then remove the
identity
> after the records have been added or something similar.
> here is a rough idea of what the stored procedure should do. (I do not
know
> the syntax to accomplish this can anyone help or explain this?
> Thanks much,
> CBL
>
>
> CREATE proc dbo.pts_ImportJobs
> as
> /* add identity to [BarCode Part#] */
> alter table dbo.ItemTest
> alter column [BarCode Part#] [int] IDENTITY(1, 1) NOT NULL
> /* add records from text file here */
> /* remove identity from BarCode Part#] */
> alter table dbo.ItemTest
> alter column [BarCode Part#] [int] NOT NULL
> return
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>
> here is the original table
> CREATE TABLE [ItemTest] (
> [BarCode Part#] [int] NOT NULL ,
> [File Number] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_File Number] DEFAULT (''),
> [Item Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Item Number] DEFAULT (''),
> [Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Description] DEFAULT (''),
> [Room Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Room Number] DEFAULT (''),
> [Quantity] [int] NULL CONSTRAINT [DF_ItemTest_Quantity] DEFAULT (0),
> [Label Printed Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Label Printed Cnt]
> DEFAULT (0),
> [Rework] [bit] NULL CONSTRAINT [DF_ItemTest_Rework] DEFAULT (0),
> [Rework Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Rework Cnt] DEFAULT (0),
> [Assembly Scan Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Assembly Scan Cnt]
> DEFAULT (0),
> [BarCode Crate#] [int] NULL CONSTRAINT [DF_ItemTest_BarCode Crate#]
DEFAULT
> (0),
> [Assembly Group#] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
> CONSTRAINT [DF_ItemTest_Assembly Group#] DEFAULT (''),
> [Assembly Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Assembly Name] DEFAULT (''),
> [Import Date] [datetime] NULL CONSTRAINT [DF_ItemTest_Import Date]
DEFAULT
> (getdate()),
> CONSTRAINT [IX_ItemTest] UNIQUE NONCLUSTERED
> (
> [BarCode Part#]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO

You can't add the IDENTITY property to an existing table - you need to
create a new table with the IDENTITY column. If you have existing data, you
can create it with a different name, INSERT the existing data, drop the
existing table, then rename the new table. Enterprise Manager will do this
for you if you add the property in the table designer.

But there are several ways to INSERT identity values into a table which
already has the IDENTITY property - I suspect that's what you're really
looking for. For loading a text file with BULK INSERT or bcp.exe, there are
options to keep identity values when you import (KEEPIDENTITY and the -E
switch, respectively). For INSERTs from another table, you can use SET
IDENTITY_INSERT ON.

Finally, DBCC CHECKIDENT is used after you've INSERTed, to make sure that
the identity seed is consistent with the table data. See Books Online for
more details on all these commands.

Simon|||Thanks for the help!

CBL

"me" <me@.work.com> wrote in message
news:10crrjkjjttmbbe@.corp.supernews.com...
> I would like to add an Identity to an existing column in a table using a
> stored procedure then add records to the table and then remove the
identity
> after the records have been added or something similar.
> here is a rough idea of what the stored procedure should do. (I do not
know
> the syntax to accomplish this can anyone help or explain this?
> Thanks much,
> CBL
>
>
> CREATE proc dbo.pts_ImportJobs
> as
> /* add identity to [BarCode Part#] */
> alter table dbo.ItemTest
> alter column [BarCode Part#] [int] IDENTITY(1, 1) NOT NULL
> /* add records from text file here */
> /* remove identity from BarCode Part#] */
> alter table dbo.ItemTest
> alter column [BarCode Part#] [int] NOT NULL
> return
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>
> here is the original table
> CREATE TABLE [ItemTest] (
> [BarCode Part#] [int] NOT NULL ,
> [File Number] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_File Number] DEFAULT (''),
> [Item Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Item Number] DEFAULT (''),
> [Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Description] DEFAULT (''),
> [Room Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Room Number] DEFAULT (''),
> [Quantity] [int] NULL CONSTRAINT [DF_ItemTest_Quantity] DEFAULT (0),
> [Label Printed Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Label Printed Cnt]
> DEFAULT (0),
> [Rework] [bit] NULL CONSTRAINT [DF_ItemTest_Rework] DEFAULT (0),
> [Rework Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Rework Cnt] DEFAULT (0),
> [Assembly Scan Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Assembly Scan Cnt]
> DEFAULT (0),
> [BarCode Crate#] [int] NULL CONSTRAINT [DF_ItemTest_BarCode Crate#]
DEFAULT
> (0),
> [Assembly Group#] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
> CONSTRAINT [DF_ItemTest_Assembly Group#] DEFAULT (''),
> [Assembly Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> CONSTRAINT [DF_ItemTest_Assembly Name] DEFAULT (''),
> [Import Date] [datetime] NULL CONSTRAINT [DF_ItemTest_Import Date]
DEFAULT
> (getdate()),
> CONSTRAINT [IX_ItemTest] UNIQUE NONCLUSTERED
> (
> [BarCode Part#]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO

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.

ALTER TABLE ... IDENTITY question....

Hi,
I am trying to programatically change the seed of an existing IDENTITY
column (Copy_ID). When I run the following command I get the error:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IDENTITY'.
ALTER TABLE Copy ALTER COLUMN Copy_ID Int IDENTITY (1,1);
Where am I going wrong?
Thanks in advance,
StuCheck out DBCC CHECKIDENT in the BOL.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Stu" <s.lock@.cergis.com> wrote in message
news:uGBp9R1RGHA.5500@.TK2MSFTNGP12.phx.gbl...
Hi,
I am trying to programatically change the seed of an existing IDENTITY
column (Copy_ID). When I run the following command I get the error:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IDENTITY'.
ALTER TABLE Copy ALTER COLUMN Copy_ID Int IDENTITY (1,1);
Where am I going wrong?
Thanks in advance,
Stu

Alter Table - Change Column Datatype

Hi,

I want to change the datatype of an existing column from char to
varbinary. When I run the "Alter Table" statement, I get the
following error message -

Disallowed implicit conversion from data type char to data type
varbinary, table 'test.dbo.testalter', column 'col1'. Use the CONVERT
function to run this query.

Can the CONVERT function be used as part of an alter table/alter
column? Is there another way besides renaming the table and creating
a new one?

Thanks,
BruceOn 19 Apr 2004 11:29:46 -0700, Bruce wrote:

>Hi,
>I want to change the datatype of an existing column from char to
>varbinary. When I run the "Alter Table" statement, I get the
>following error message -
>Disallowed implicit conversion from data type char to data type
>varbinary, table 'test.dbo.testalter', column 'col1'. Use the CONVERT
>function to run this query.
>Can the CONVERT function be used as part of an alter table/alter
>column? Is there another way besides renaming the table and creating
>a new one?
>Thanks,
>Bruce

Yes, there is another way: rename not the whole table, but just the
column, then create a new one:

EXEC sp_rename 'test.dbo.testalter.col1' 'col1old', COLUMN
go
ALTER TABLE test.dbo.testalter
ADD col1 varbinary(321) NULL
-- If it has to be NOT NULL, change this to read
-- ADD col1 varbinary(321) NOT NULL DEFAULT 0
go
UPDATE test.dbo.testalter
SET col1 = CAST(col1old AS varbinary(321))
go
ALTER TABLE test.dbo.testalter
DROP COLUMN col1old
go

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)