sde Posted January 5, 2004 Posted January 5, 2004 i have a table which increments the id field by 1. what is the logic i would use to insert a new row and retrieve the new id? do i have to use 2 queries? Quote codenewbie
Moderators Robby Posted January 5, 2004 Moderators Posted January 5, 2004 Are you using SQL Server? if so, use @@Identity if your inserting in a stored proc. Quote Visit...Bassic Software
sde Posted January 5, 2004 Author Posted January 5, 2004 i am using sql server. i think i need to brush up on my db admin knowledge, cause i don't know what you really mean. gives me a place to start my google search though. thanks :) Quote codenewbie
Moderators Robby Posted January 6, 2004 Moderators Posted January 6, 2004 Create a Stored Proc to do your inserts. Here's a sample you can run in Query Analyzer.... SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS OFF GO CREATE PROCEDURE dbo.Some_Stored_Proc @Fname varchar(30)= null,-- not required @Lname varchar(30)--required AS declare @id int SET @id = 0 if (@Lname != '')--ensures a valid input BEGIN Begin transaction INSERT INTO Table1 ([Fname],[Lname],[date_created]) Values (@Fname, @Lname,getdate()) SET @id = @@IDENTITY commit transaction return @id --return the new ID number to the caller END SET QUOTED_IDENTIFIER OFF GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.