.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Having a doubt in SQL query

Posted By: Savi3     Posted Date: September 04, 2008    Points:2   Category :Sql Server
How to retrieve a single value from the query

I need to increment the primary key in the table also after increment i've to add it..


select @ncode=count(N_ID) from [table name]
if @ncode=0
select @ncode=1
else
select @ncode=max(N_ID)+1 from [table name]

insert into [table name](N_ID,Name,Salary)values (@N_ID,@Name,@Salary)


I write this in stored procedure.. But i cant insert a record through the parameters in code behind

Responses
Author: Pankaj Mishra             
Posted Date: September 04, 2008     Points: 5   

Hi,
why dont you use auto generate feature for the primary key field in SQL Server i.e for the primary key column set its "identity" property to true. It will automatically increment the value and you dont need to pass from the Client.

Hope this will help you


Cheers
Pankaj

Author: Purushotham Ravada             
Posted Date: September 04, 2008     Points: 5   

Declare @ncode Numeric

select @ncode = isnull(max(N_ID),0)+1 from [table name]

insert into [table name](N_ID,Name,Salary)values (@ncode,@Name,@Salary)

Author: Savi3             
Posted Date: September 04, 2008     Points: 5   

ya Thanks Mr.Pankaj Mishra.. Its simplest way also executing very well..

Author: Santosh             
Posted Date: October 09, 2009     Points: 5   

HI
TRY WITH BELOW CODE


DECLARE @ID INT
SELECT @ID = MAX(ID) FROM TEST --(TABLENAME)
IF @ID IS NULL
INSERT INTO TEST VALUES(1,'SANTOSH')
ELSE
INSERT INTO TEST VALUES(@ID +1 ,'SANTOSH')

REGARDS
SANTOSH
http://santoshdotnetarena.blogspot.com/



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend