Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My query as follows

SQL
Insert INTO Accounts_Entity(ID,Entity,Address1,Address2,Address3,Address4,Address5,Signature)
SELECT MAX(CAST(ID as INT)) + ,'HIMT','HIMT2','HIMT3','HIMT4','HIMT5','HIMT6','off'
Accounts_Entity


When i run the above sql query shows error as follows

Invalid column name 'ID'

Table structure as follows (Table Name Accounts_Entity)

Field         Datatype
 ID           bigint
 Entity      varchar(50)
 Address1    varchar(50)
 Address2    varchar(50
 Address3    varchar(50)
 Address4    varchar(50)
 Address5    varchar(50)
 Signature   Varchar(50)


please help what is the mistake in my above sql query.

Regards,
Narasiman P.
Posted
Updated 26-Aug-15 18:26pm
v2

1 solution

Looks like there are some problems

  • You cast the ID into in even though it's already numeric, but bigint
  • You select MAX for the ID and you have a + oprator but no value after +
  • You don't have FROM keyword in the SELECT statement at all


However, I would choose a completely different approach. Instead of trying to generate the next value for the key column by yourself, let the database do it. You have at least three different options (in no particular order):


Edit:

If you're using My SQL, then use AUTO_INCREMENT[^]
 
Share this answer
 
v3
Comments
PIEBALDconsult 27-Aug-15 0:30am    
(Not directed at you, Mika.)
Being a fan of Sequences, in Oracle and SQL Server, I would create my own. But that's just me. IDENTITY/AUTO_INCREMENT are more trouble than they're worth.
Wendelius 27-Aug-15 1:29am    
I do agree that sequences provide additional benefits over IDENTITY and make life a bit more simpler. However NEWID is extremely useful in some scenarios such as replicated environments.

Maciej Los 27-Aug-15 2:00am    
Good advice, +5!
Wendelius 27-Aug-15 23:49pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900