Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 no property for IDENTITY in SQL server express

Author  Topic 

shirlyxu
Starting Member

5 Posts

Posted - 2008-12-28 : 17:45:53
Why does I can't find the property for IDENTITY on the list of data type in SQL server management studio 2005 express when I use design to create new table.

If I type identity in data type, I get the message "invalid data type."

If I create the new table using the script to set the identity(1,1),

CREATE TABLE register ( user_id int identity(1,1), Name char(50) )

after I execute it, the identity(1,1)disappear automatically.

Thanks for any help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-28 : 19:07:36
Identity isn't a data type. The identity didn't disappear. Please explain what you mean by that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

shirlyxu
Starting Member

5 Posts

Posted - 2008-12-28 : 19:50:26
I want this column of user_id to be auto generated and auto increment( making it a primary key and setting the Identity Column to yes/true). But I still can't find it in list of data type.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-28 : 20:12:23
It isn't a data type. The code that you posted is correct to create the identity.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-29 : 00:34:37
did you try inserting records to table after you created it by above statement. it should work fine.
Go to Top of Page

jasKoenig
Starting Member

1 Post

Posted - 2008-12-29 : 05:11:20
First of all IDENTITY is not a datatype so u can't select the same within the table designer from DataType column, but instead, specify the appropriate datatype, lets say int is the Data Type and then, go to the column properties ( just below the table designer) -> Identity Specification -> Make IsIdentity = true. Thats It.
Go to Top of Page

shirlyxu
Starting Member

5 Posts

Posted - 2008-12-30 : 12:21:42
Maybe I'm not clear to explain my question.

May I ask which specify of the appropriate datatype can I use to auto generated and increment for user_ID (set it a primary key)?

I carefully check every different datatype in datatype drop down list of column , but I still can't find it.

I have used the uniqueidentifier,but it can't auto increment.

Thank you for your reply.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-30 : 12:24:17
I'd use int or bigint, depending upon how many rows you expect in the table. Your code above is correct though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 12:27:29
quote:
Originally posted by shirlyxu

Maybe I'm not clear to explain my question.

May I ask which specify of the appropriate datatype can I use to auto generated and increment for user_ID (set it a primary key)?

I carefully check every different datatype in datatype drop down list of column , but I still can't find it.

I have used the uniqueidentifier,but it can't auto increment.

Thank you for your reply.




for making unique identifier fields auto incrementing with sequential values, you should define a default constraint on them using NEWSEQUENTIALID() function. this will cause them to be filled with sequential values automatically for each insertion.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-30 : 12:54:54
Just use a simple identity to autoincrement, avoid the use of a GUID if possible.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 13:03:32
yeah...i have heard integer is better choice as GUIDs need more size and can cause storage memory problems. Also they will not perform much in range queries.
And i have always preferred to use integer identity columns
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-12-30 : 14:33:11
>>May I ask which specify of the appropriate datatype can I use to auto generated and increment for user_ID (set it a primary key)?

Just for the sake of completeness - From Books Online:
quote:
The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns.


>>after I execute it, the identity(1,1)disappear automatically.
It didn't "disappear". You may not see the identity property in object explorer, try scripting the table to a new query window and you will see the identity property in the resulting code.

Be One with the Optimizer
TG
Go to Top of Page

shirlyxu
Starting Member

5 Posts

Posted - 2008-12-30 : 16:54:03
I don't know what is wrong in my code, the forum_ID still can't be incremented automatically.

When I run code,only the data of one row can be inserted into table.

There is no error message when adding second one.

The code is below:

alter PROCEDURE [dbo].[sp_forumNewUser]
@forum_ID bigint,@forum_DiscussID bigint, @forumUsername varchar(50), @forumEmail varchar(50),

AS
BEGIN
SET NOCOUNT ON;
DECLARE @MaxRegID bigint

SELECT @MaxRegID = max(forum_ID) from forumRegistration
SET @MaxRegID = @MaxRegID+ 1

IF(Select countbig(1) from forumRegistration where forum_ID = @forum_ID)>0
BEGIN
UPDate forumRegistration
set forumUsername = @forumUsername
where forum_ID = @MaxRegID
END
ELSE
BEGIN
insert into forumRegistration(forum_ID,forum_DiscussID,forumUsername,forumEmail)
values(@forum_ID,@forum_DiscussID,@forumUsername,@forumEmail)
END
END
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-30 : 17:00:47
You need to exclude forum_ID from the column list in order to let SQL Server manage it for you.

insert into forumRegistration(forum_DiscussID,forumUsername,forumEmail)
values(@forum_DiscussID,@forumUsername,@forumEmail)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-30 : 22:40:34
Identity is not a datatype, You can implement identifier columns by using the IDENTITY property. This enables the developer to specify both an identity number for the first row inserted into the table (Identity Seed property) and an increment (Identity Increment property) to be added to the seed to determine successive identity numbers. When values are inserted into a table that has an identifier column, the SQL Server 2005 Database Engine automatically generates the next identity value by adding the increment to the seed
Go to Top of Page

shirlyxu
Starting Member

5 Posts

Posted - 2008-12-31 : 15:17:32
Thank you everyone for helping me.

I was confused the uniqueidentifier datatype with IDENTITY property, I understand it now.

I have deleted forum_ID from INSERT command as Tkizer said, but the data only can be inserted into the table once, and it doesn’t work on the second time. I don’t know what value can use for (auto generated and increment) Forum_ID in cmd.Parameters.AddWithValue("@Forum_ID", -1); I use -1,it is not right.

Happy New Year!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-31 : 15:18:52
I don't understand what you are trying to do. Never pass in Forum_ID when doing an INSERT command.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

prpball
Starting Member

19 Posts

Posted - 2008-12-31 : 16:06:54
Are you just trying to insert a row into the column and have the forum_id be incremented each time?

Have you set the user_id Identity to Yes?
Go to SQL Server Management Studio and right click on the table and choose modify it. Click the column name and look at the Column Properties. Scroll down to Identity Specification and click the plus sign next to it. Choose (is Identity) = Yes and set increment and speed to 1.

Anytime you insert date into the table you will leave off the user_id field name and SQL will manage it for you.

For instance:
insert into forumRegistration(forum_DiscussID,forumUsername,forumEmail)
values(@forum_DiscussID,@forumUsername,@forumEmail)

This statement will only run once unless you call it more than once.
Go to Top of Page

prpball
Starting Member

19 Posts

Posted - 2008-12-31 : 16:08:22
Don't pass any parameter for form_id. Completely ignore it in your code.
Go to Top of Page

pkj1383
Starting Member

3 Posts

Posted - 2009-01-01 : 08:11:25
Hi,

Please avoid the insertion in FormID column and check property of that column its set as Identity yes or not.

Prashant Joshi
Go to Top of Page
   

- Advertisement -