473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting the PK back from an insert sql statement, how?

Bob
Using sql 2005 and Vs2005 , snippet below.

Dim Conn As New SqlConnection(My.Settings.ConnectionString.ToStrin g)

Dim cmd As New SqlCommand

Dim MYPK as long

Conn.Open()

cmd.Connection = Conn

cmd.CommandText = "Insert Into Mytable (Field1,Field2)
Values('Value1','Value2')"

"When executed, this inserts only one new row. How can I get the primary key
of that new record back into variable MYPK

Any help appreciated,

Bob


Dec 28 '05 #1
2 819
Bob,

Have a look at scope_identity

http://msdn.microsoft.com/library/en...a-ses_6n8p.asp

I hope this helps,

Cor
Dec 28 '05 #2
Hi Bob,

Here is link for your reference.
320141 HOW TO: Retrieve an Identity Value from a Newly Inserted Record from
SQL
http://support.microsoft.com/?id=320141

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KIPAX | last post by:
I am inserting values into a row.. works OK If I try to insert a value into a row that doesnt exist (using where) it always returns true the same as if it did work I want to update rows that...
1
by: Jeff | last post by:
Hello all, I have an SQL Insert statement I'm trying to run via ExecuteNonQuery. It works fine so long as there are no nulls values. Here is the statement...(its a shortened version of what i...
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
2
by: M | last post by:
If I have a query I am writing, I can use the top 10 function to bring back the top 10 rows. That's all fine if all I want is 10 rows. What if I have a grouped query, and I have 5 entities that...
10
by: Rahul Babbar | last post by:
Hi, I am getting the following error, while executing the simple insert script on a few tables. INSERT INTO <table_name>(<col1>) VALUES (1); DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884,...
12
by: magmike | last post by:
Accidentally deleted a record. Anyway to get it back? If not, I know the ID number - which is an autonumber field. Because of the related data from other tables, would I be able to create a new...
2
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into...
9
by: KDawg44 | last post by:
Hi, I have PHP function that adds a record to the database. The table has an ID that is AUTO_INCREMENT. Is there anyway to get that ID back when I do any kind of insert? That ID is a foreign...
0
by: danindya | last post by:
I am writing a "C" program using Embedded Mysql that uses two threads performing the following operations in an infinite loop: - Thread 1 inserts 500 records to the database every 30 seconds and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.