Showing posts with label identity. Show all posts
Showing posts with label identity. Show all posts

Wednesday, March 28, 2012

How to assign the @@IDENTITY to a variable

Hi,

HOW can I assign the value of @.@.IDENTITY to the any variable in SQL SERVER .

Thanks,


DECLARE @.var int
INSERT INTO <table> VALUES <...> SELECT @.var= @.@.IDENTITY

On another note, I'd recommend using SCOPE_IDENTITY() instead of @.@.IDENTITY. check out books online for some info. ScopeIdentity is more accurate in returning the autonumber id.

How to Assign Identity Key values in Replication

This is a very basic question on replication.

I'm having a central Server with SQL Server 2005 Standard Edition and Other sites with Sql Express Server 2005.

Other sites will also be adding New records and data will be replicated to Central server and from there it will be distributed to all sites.

Question is that if Other sites are also adding Records how i can assing Identity values in those databases. There are few restricitons on this :-

1. I don't want to use GUID.

2. Numbers should be sequential that is after 1000, 1001, 1002 etc. should come.

i thought of adding Negative Values in the primary key on other sites and then when data is replicated on central server then replace it with sequential key but i'm not clear on how to accomplish this.

any help will be highly appreciable.

You can specify identity ranges. See books online topic "Replicating Identity Columns". You can also search books online for "replication identity" for a range of topics.|||

thanks for your reply.

i saw the topics which you have mentioned. As per my requirement i/o specify individual ranges i have to keep this Identity column in Sequence for all sites. so i will have to do this manually..

is it possible to point towards some code which does that as i'm sure this is a very common requirement and lot of people must have already written generic code to accomplish this.

How to assign different identity ranges to publisher and subscribers?

Hello,
There is a @.identity_range parameter, which controls the identity
range size initially allocated both to the publisher and to
subscribers in sql server 2005 merge replication. I need to assign
different identity ranges for publisher and subscribers. Is it
possible to achieve it? The @.pub_identity_range parameter does not
seem to work.
Thanks you,
Jawad
You can do it manually using checkident or using the create publication
wizard (see the article properties dialog, its in the identity range
management section).
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"JDee" <jawwad.ali@.gmail.com> wrote in message
news:1174476685.219315.29270@.n59g2000hsh.googlegro ups.com...
> Hello,
> There is a @.identity_range parameter, which controls the identity
> range size initially allocated both to the publisher and to
> subscribers in sql server 2005 merge replication. I need to assign
> different identity ranges for publisher and subscribers. Is it
> possible to achieve it? The @.pub_identity_range parameter does not
> seem to work.
> Thanks you,
> Jawad
>

Friday, March 23, 2012

How to alter column to identity(1,1)

I tried

Alter table table1 alter column column1 Identity(1,1);

Alter table table1 ADD CONSTRAINT column1_def Identity(1,1) FOR column1

they all can not work,

any idea about this? thanks

Code Snippet

--create test table

createtable table1 (col1 int, col2 varchar(30))

insertinto table1 values(100,'olddata')

--add identity column

altertable table1 add col3 intidentity(1,1)

GO

--rename or remove old column

execsp_rename'table1.col1','oldcol1','column'

OR

altertable table1 dropcolumn col1

--rename new column to old column name

execsp_rename'table1.col3','col1','column'

GO

--add new test record and review table

insertinto table1 values('newdata')

select*from table1

|||

You can't alter the existing columns for identity.

You have 2 options,

1. Create a new table with identity & drop the existing table

2. Create a new column with identity & drop the existing column

But take spl care when these columns have any constraints / relations.

Code Snippet

/*

For already craeted table Names

Drop table Names

Create table Names

(

ID int,

Name varchar(50)

)

Insert Into Names Values(1,'SQL Server')

Insert Into Names Values(2,'ASP.NET')

Insert Into Names Values(4,'C#')

*/

Code Snippet

--In this Approach you can retain the existing data values on the newly created identity column

CREATE TABLE dbo.Tmp_Names

(

Id int NOT NULL IDENTITY (1, 1),

Name varchar(50) NULL

)ON [PRIMARY]

go

SET IDENTITY_INSERT dbo.Tmp_Names ON

go

IF EXISTS(SELECT * FROM dbo.Names)

INSERT INTO dbo.Tmp_Names (Id, Name)

SELECT Id, Name FROM dbo.Names TABLOCKX

go

SET IDENTITY_INSERT dbo.Tmp_Names OFF

go

DROP TABLE dbo.Names

go

Exec sp_rename 'Tmp_Names', 'Names'

Code Snippet

--In this approach you can’t retain the existing data values on the newly created identity column;

--The identity column will hold the sequence of number

Alter Table Names Add Id_new Int Identity(1,1)

Go

Alter Table Names Drop Column ID

Go

Exec sp_rename 'Names.Id_new', 'ID','Column'

|||

thank you

but I could not change the existing number order and add identity

any idea about this?

|||Nope. There is no way to alter a column to have the identity property. You will have to create a new table and insert into it if you want initial control over the values in the column.

how to ALTER COLUMN ?

Hi,
i do a create:
CREATE TABLE myTable ( id int IDENTITY(1,1) NOT NULL)
and now i want to remove the IDENTITY definition from colum id
using ALTER myTable ALTER COLUMN id ... '
so that column id has definition as when i do a:
CREATE TABLE myTable ( id int NULL)
How can i do this?
thanks, HelmutHelmut
CREATE TABLE Test (col INT NOT NULL IDENTITY(1,1))
GO
insert into Test default values
ALTER TABLE Test ADD col1 INT NULL
GO
ALTER TABLE Test DROP COLUMN col
GO
EXEC sp_rename 'TEST.COL1', 'COL', 'COLUMN'
Note: If you had some data inserted into the table before addin a new column
and need to move it ,so use UPDATE command as
UPDATE Test SET col1=col
"Helmut Woess" <user22@.inode.at> wrote in message
news:dzg357eusfly$.19hvp25nnxfh1.dlg@.40tude.net...
> Hi,
> i do a create:
> CREATE TABLE myTable ( id int IDENTITY(1,1) NOT NULL)
> and now i want to remove the IDENTITY definition from colum id
> using ALTER myTable ALTER COLUMN id ... '
> so that column id has definition as when i do a:
> CREATE TABLE myTable ( id int NULL)
> How can i do this?
> thanks, Helmut|||I hate the sp_rename command. Using SQL2000 and SQL2005 Beta 2, I managed
to loose an entire table because of that command. The table in question had
17million rows. So be carful with it.
Regards
Colin Dawson
www.cjdawson.com
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:eIFAYrEbGHA.4060@.TK2MSFTNGP02.phx.gbl...
> Helmut
> CREATE TABLE Test (col INT NOT NULL IDENTITY(1,1))
> GO
> insert into Test default values
> ALTER TABLE Test ADD col1 INT NULL
> GO
> ALTER TABLE Test DROP COLUMN col
> GO
> EXEC sp_rename 'TEST.COL1', 'COL', 'COLUMN'
> Note: If you had some data inserted into the table before addin a new
> column and need to move it ,so use UPDATE command as
> UPDATE Test SET col1=col
>
> "Helmut Woess" <user22@.inode.at> wrote in message
> news:dzg357eusfly$.19hvp25nnxfh1.dlg@.40tude.net...
>|||Colin
This stored procedure is documented and supported officialy by MS .

>I hate the sp_rename command. Using SQL2000 and SQL2005 Beta 2, I managed
>to loose an entire table because of that command. The table in question
>had 17million rows. So be carful with it.
I just did some testing on the table that has 50 million rows and it worked
nice. Can you provide a repro where you are loosing an entire of table?
"Colin Dawson" <newsgroups@.cjdawson.com> wrote in message
news:vJ15g.61831$wl.42277@.text.news.blueyonder.co.uk...
>I hate the sp_rename command. Using SQL2000 and SQL2005 Beta 2, I managed
>to loose an entire table because of that command. The table in question
>had 17million rows. So be carful with it.
> Regards
> Colin Dawson
> www.cjdawson.com
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:eIFAYrEbGHA.4060@.TK2MSFTNGP02.phx.gbl...
>|||Yes, I know. I was at the MS Labs when the table got lost. Fortunately I
had a backup of the table, so I retried and lost the table again, and a
third time. Bottom line is, that if you try to rename a large table there
is a chance that you will loose it completely.
If you want to take your chances and risk loosing data on a production
system - or more to the point, creating unneeded downtime if you've been
cautious, and sensible enough to backup before hand. Then go ahead.
Regards
Colin Dawson
www.cjdawson.com
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uCabo8EbGHA.4292@.TK2MSFTNGP04.phx.gbl...
> Colin
> This stored procedure is documented and supported officialy by MS .
>
> I just did some testing on the table that has 50 million rows and it
> worked nice. Can you provide a repro where you are loosing an entire of
> table?
>
>
> "Colin Dawson" <newsgroups@.cjdawson.com> wrote in message
> news:vJ15g.61831$wl.42277@.text.news.blueyonder.co.uk...
>

Monday, March 12, 2012

How to add IDENTITY to existing column

Hi All
Is it possible to alter an EXISTING column to make it an IDENTITY seed
column using T-SQL. I've looked at the BOL help for alter table/column but
can't seem to decipher how it should be done. It can be easily done using
enterprise manager or using SQL-SMO, but for my purposes I need to use
T-SQL.
Thanks in advance!
Ryanfrom BOL : " IDENTITY Specifies that the new column is an identity column.
....."
This means you can only specify it when you add the column to the table.
If you can, use SQL Enterprise Manager, Design the table and generate the
script.
You'll see it generates something like this (so it creates / copies / drops)
:
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Tmp_TJOBIdoc_exe
(
column_a int NULL,
column_b int NOT NULL IDENTITY (1, 1),
column_c int NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_TJOBIdoc_exe ON
GO
IF EXISTS(SELECT * FROM dbo.TJOBIdoc_exe)
EXEC('INSERT INTO dbo.Tmp_TJOBIdoc_exe (column_a, column_b, column_c)
SELECT column_a, column_b, column_c FROM dbo.TJOBIdoc_exe TABLOCKX')
GO
SET IDENTITY_INSERT dbo.Tmp_TJOBIdoc_exe OFF
GO
DROP TABLE dbo.TJOBIdoc_exe
GO
EXECUTE sp_rename N'dbo.Tmp_TJOBIdoc_exe', N'TJOBIdoc_exe', 'OBJECT'
GO
ALTER TABLE dbo.TJOBIdoc_exe ADD CONSTRAINT
column_a_un UNIQUE NONCLUSTERED
(
column_a
) ON [PRIMARY]
GO
COMMIT
jobi
"Ryan Clarke" <ryan@.turfsport.co.za> wrote in message
news:bfoj7v$q7q$1@.ctb-nnrp2.saix.net...
> Hi All
> Is it possible to alter an EXISTING column to make it an IDENTITY seed
> column using T-SQL. I've looked at the BOL help for alter table/column but
> can't seem to decipher how it should be done. It can be easily done using
> enterprise manager or using SQL-SMO, but for my purposes I need to use
> T-SQL.
> Thanks in advance!
> Ryan
>

How to add Identity property to existing table?

Has anybody ever tried to do this. I can't figure it out. All I want to do is take an existing table that already has values in the column that I want to change and add the identity property to yes and set the identity seed and increment to a specific number. I know you can do it in the CREATE TABLE statement but is there a way to use the ALTER TABLE command?Create a new table with same schema and the Identity column, then load the data into the new table, and then drop the old table.|||i don't think so. even when you trying to remove the identity from a table, it will drop and recreate the table in the backgound. it can't just
modify the table structure.

Originally posted by randyfoy
Has anybody ever tried to do this. I can't figure it out. All I want to do is take an existing table that already has values in the column that I want to change and add the identity property to yes and set the identity seed and increment to a specific number. I know you can do it in the CREATE TABLE statement but is there a way to use the ALTER TABLE command?|||even though I can just go to the design view of the table through the Enterprise Manager and change the property? It works fine if I do it manually.|||and try to run profiler while you're doing it, guess you'd answer your own question

How to add IDENTITY constraint

Dear All,

I wanted to add a IDENTITY constraint in a column using the ALTER command, How do I go about doing that?

Please help.

Code Snippet

create table #what (something varchar(10))

alter table #what
add anIdentity int identity

insert into #what
select 'This' union all
select 'is' union all
select 'just' union all
select 'another' union all
select 'test'
select * from #what

/*
something anIdentity
- --
This 1
is 2
just 3
another 4
*/

|||

Sorry for incomplete or unclear question.

What I mean is Altering the existing column and adding Identity to that column

|||

I think to do that you will need to do the work of creating a new table and move the data into the new table. Care needs to be take to pay attention to the seeding of the new identity column and all constraint work that needs to be done.

Will someone please check me on this?

|||

Unfortunately, i don't think you are able to alter an existing column to be an identity. Your best workaround is to create a new table with the correct schema, dump the data into it and then rename it.

However, I appreciate this can be a messy and impractical approach. Your only other option is to just add an extra identity column.

HTH

|||The following should work:

Code Snippet

CREATE TABLE TestTable
(
ToBeIdentity INT,
TestCol VARCHAR(20)
)

INSERT INTO TestTable(ToBeIdentity, TestCol) VALUES(1, 'Col1')
INSERT INTO TestTable(ToBeIdentity, TestCol) VALUES(2, 'Col2')
INSERT INTO TestTable(ToBeIdentity, TestCol) VALUES(3, 'Col3')
INSERT INTO TestTable(ToBeIdentity, TestCol) VALUES(4, 'Col4')

SELECT
*
INTO
#TempTransfer
FROM
TestTable

ALTER TABLE TestTable DROP COLUMN ToBeIdentity

DELETE FROM TestTable

ALTER TABLE TestTable ADD ToBeIdentity INT IDENTITY(1, 1)

SET IDENTITY_INSERT TestTable ON
INSERT INTO TestTable
(ToBeIdentity,
TestCol)
SELECT
*
FROM
#TempTransfer
SET IDENTITY_INSERT TestTable OFF

INSERT INTO TestTable(TestCol) VALUES('Col5')

SELECT
*
FROM
TestTable

DROP TABLE #TempTransfer


Unfortunately, you have to specify a column list when inserting the data into the table again. Other than that, you should be able to use it for most tables without modification.
|||

It is not necessary to create a new table and transfer the data -with a very large table that could be quite a performance hit.

As this example demonstrates, you can add an IDENTITY column to an existing table. (NOTE: This does not guarantee any order to the data.)

Code Snippet


CREATE TABLE #MyTable
( RowID int,
MyValue varchar(20)
)


INSERT INTO #MyTable VALUES ( 25, 'Value 1' )
INSERT INTO #MyTable VALUES ( 2, 'Value 2' )
INSERT INTO #MyTable VALUES ( 61, 'Value 3' )
INSERT INTO #MyTable VALUES ( 33, 'Value 4' )


-- Keep Existing Column and Data
ALTER TABLE #MyTable
ADD NewRowID int IDENTITY(1, 1)


SELECT *
FROM #MyTable


RowID MyValue NewRowID
-- -- --
25 Value 1 1
2 Value 2 2
61 Value 3 3
33 Value 4 4

-- If you want to keep the Column Name -but not data
ALTER TABLE #MyTable
DROP COLUMN RowID, NewRowID


ALTER TABLE #MyTable
ADD RowID int IDENTITY(1, 1)


SELECT *
FROM #MyTable


DROP TABLE #MyTable

MyValue RowID
-- --
Value 1 1
Value 2 2
Value 3 3
Value 4 4

If you want to maintain or coerce order, creating a new table and transferring the data would allow you to use ORDER BY on the insert statement.


|||

Arnie Rowland wrote:

It is not necessary to create a new table and transfer the data -with a very large table that could be quite a performance hit.

As this example demonstrates, you can add an IDENTITY column to an existing table.

Code Snippet


CREATE TABLE #MyTable
( RowID int,
MyValue varchar(20)
)

...

Your foreign keys on the original column become messed up unless you preserve the original values.

how to add identity column to a view

hi all,
how to add identity column to a view in sql server 2000.
plz help.
Amit,
Sorry, but you cannot add an identity column to a view. What is your need?
Are you trying to get a row number attached to each row returned? Must it
be in a view or can you use some other mechanism?
You can do the following, but it will get expensive if the result set is
quite large.
SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
RowNumber, a.*
FROM MyTable AS a
ORDER BY RowNumber
RLF
"amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>
|||Hi
Along with Russells reply..
You should probably add the "row number" in the client application as this
would be the most efficient way to generate a sequence number.
John
"amit sharma" wrote:

> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>
|||hi Russell,
thanx for your reply.
i have a table with identity column and values are not continous in that
column.
thats why i want to create a view which has continous values in identity
column.
so that i can implement custom paging in my front end application.
"Russell Fields" wrote:

> Amit,
> Sorry, but you cannot add an identity column to a view. What is your need?
> Are you trying to get a row number attached to each row returned? Must it
> be in a view or can you use some other mechanism?
> You can do the following, but it will get expensive if the result set is
> quite large.
> SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
> RowNumber, a.*
> FROM MyTable AS a
> ORDER BY RowNumber
> RLF
> "amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
> news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
>
>
|||Hi
You may want to read
http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html
John
"amit sharma" wrote:
[vbcol=seagreen]
> hi Russell,
> thanx for your reply.
> i have a table with identity column and values are not continous in that
> column.
> thats why i want to create a view which has continous values in identity
> column.
> so that i can implement custom paging in my front end application.
>
> "Russell Fields" wrote:

how to add identity column to a view

hi all,
how to add identity column to a view in sql server 2000.
plz help.Amit,
Sorry, but you cannot add an identity column to a view. What is your need?
Are you trying to get a row number attached to each row returned? Must it
be in a view or can you use some other mechanism?
You can do the following, but it will get expensive if the result set is
quite large.
SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
RowNumber, a.*
FROM MyTable AS a
ORDER BY RowNumber
RLF
"amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>|||Hi
Along with Russells reply..
You should probably add the "row number" in the client application as this
would be the most efficient way to generate a sequence number.
John
"amit sharma" wrote:

> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>|||hi Russell,
thanx for your reply.
i have a table with identity column and values are not continous in that
column.
thats why i want to create a view which has continous values in identity
column.
so that i can implement custom paging in my front end application.
"Russell Fields" wrote:

> Amit,
> Sorry, but you cannot add an identity column to a view. What is your need
?
> Are you trying to get a row number attached to each row returned? Must it
> be in a view or can you use some other mechanism?
> You can do the following, but it will get expensive if the result set is
> quite large.
> SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
> RowNumber, a.*
> FROM MyTable AS a
> ORDER BY RowNumber
> RLF
> "amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
> news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
>
>|||Hi
You may want to read
http://databases.aspfaq.com/databas...-recordset.html
John
"amit sharma" wrote:
[vbcol=seagreen]
> hi Russell,
> thanx for your reply.
> i have a table with identity column and values are not continous in that
> column.
> thats why i want to create a view which has continous values in identity
> column.
> so that i can implement custom paging in my front end application.
>
> "Russell Fields" wrote:
>

how to add identity column to a view

hi all,
how to add identity column to a view in sql server 2000.
plz help.Amit,
Sorry, but you cannot add an identity column to a view. What is your need?
Are you trying to get a row number attached to each row returned? Must it
be in a view or can you use some other mechanism?
You can do the following, but it will get expensive if the result set is
quite large.
SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
RowNumber, a.*
FROM MyTable AS a
ORDER BY RowNumber
RLF
"amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>|||Hi
Along with Russells reply..
You should probably add the "row number" in the client application as this
would be the most efficient way to generate a sequence number.
John
"amit sharma" wrote:
> hi all,
> how to add identity column to a view in sql server 2000.
> plz help.
>|||hi Russell,
thanx for your reply.
i have a table with identity column and values are not continous in that
column.
thats why i want to create a view which has continous values in identity
column.
so that i can implement custom paging in my front end application.
"Russell Fields" wrote:
> Amit,
> Sorry, but you cannot add an identity column to a view. What is your need?
> Are you trying to get a row number attached to each row returned? Must it
> be in a view or can you use some other mechanism?
> You can do the following, but it will get expensive if the result set is
> quite large.
> SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
> RowNumber, a.*
> FROM MyTable AS a
> ORDER BY RowNumber
> RLF
> "amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
> news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
> > hi all,
> >
> > how to add identity column to a view in sql server 2000.
> > plz help.
> >
>
>|||Hi
You may want to read
http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html
John
"amit sharma" wrote:
> hi Russell,
> thanx for your reply.
> i have a table with identity column and values are not continous in that
> column.
> thats why i want to create a view which has continous values in identity
> column.
> so that i can implement custom paging in my front end application.
>
> "Russell Fields" wrote:
> > Amit,
> >
> > Sorry, but you cannot add an identity column to a view. What is your need?
> > Are you trying to get a row number attached to each row returned? Must it
> > be in a view or can you use some other mechanism?
> >
> > You can do the following, but it will get expensive if the result set is
> > quite large.
> >
> > SELECT (select count(*) from MyTable where KeyColumn <= a.KeyColumn) AS
> > RowNumber, a.*
> > FROM MyTable AS a
> > ORDER BY RowNumber
> >
> > RLF
> >
> > "amit sharma" <amitsharma@.discussions.microsoft.com> wrote in message
> > news:351A9D82-3E62-465C-90AE-1405C2747667@.microsoft.com...
> > > hi all,
> > >
> > > how to add identity column to a view in sql server 2000.
> > > plz help.
> > >
> >
> >
> >

Wednesday, March 7, 2012

How to add an identity PK column to a View

For an ASP.net project, I have had a DropDownList with a static ArrayList.
The ArrayList will be defined from a View, where there is no Identity PK.

I also have used cbxDropDownListName.SelectedIndex to add new data to
a table, where an Indetity PK is used to reference the ArrayList.

I am wondering how can I add an identity PK to my view?

TIA,
Jeffrey

Why don't you select an identity column from one of the tables in the view? Pick an identity column that, by the view definition, would be unique in the view result set.