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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Inserting values into a column!!

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-10-20 : 13:23:11
Hi,

I am trying to insert some rows into an existing table. The PK which is prdouctID has consecutive numbers starting at 1. It is currently on 151 for example Since I will be inserting rows what do I do for this particular column so that the productId for the inserted row gets 152 now.

Thanks,

SA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-20 : 13:34:54
If productID is using an identity column, then you just need to exclude that column from your INSERT column list.

Here's an example:

CREATE TABLE Table1
(Column1 int IDENTITY(1, 1) NOT NULL, Column2 varchar(5))

INSERT INTO Table1 (Column2) VALUES ('Tara')

SELECT * FROM Table1

DROP TABLE Table1

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-20 : 13:46:20
use this to check if its an identity column. it should be 1 for identity

SELECT COLUMNPROPERTY( OBJECT_ID('table name here'),'productID','IsIdentity')
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-10-20 : 13:54:09
Thank you to both of you. Brilliant. I will have to save this in my special file for future reference. Visakh I do get a 1 when I excute your IsIdentity statement. It feels like magic.

SA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-20 : 13:57:22
Welcome...that does tell that its an identity column. So you may use earlier suggestion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-20 : 13:59:09
Glad to help.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -