Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
ALTER TABLE AllotmentTab
ADD ID INT IDENTITY PRIMARY KEY,Registration AS '4MT08' + RIGHT('5100' + CAST(ID AS VARCHAR(50)), 6)
PERSISTED)



can any one telme what s the error.its showing error near PERSISTED
Posted
Comments
ythisbug 6-Mar-12 7:25am    
Msg 2705, Level 16, State 4, Line 1
Column names in each table must be unique. Column name 'ID' in table 'AllotmentTab' is specified more than once.

now coming error like this..i removed bracket after persisted

You are doing string concatenation in a data definition query - Registration AS '4MT08' + RIGHT('5100' + CAST(ID AS VARCHAR(50)), 6) cannot be included in this type of sql statement.
 
Share this answer
 
Comments
ythisbug 6-Mar-12 7:38am    
ACTUALLY I WANT TO ADD COLUMN THAT I CAN SET 4MT085100 VALUE IN THAT COLUMN THEN I WANT TO SET THIS AS AUTO INCREMENT.SO M TRYING THAT QUERY.CAN YOU SUGGEST ME?
PERSISTED) looks to be something on its own. It looks like something that belongs to something and not to the statement above.

Good luck!
 
Share this answer
 
Comments
Pete O'Hanlon 6-Mar-12 7:29am    
Sadly, that part has nothing to do with the error he's getting. The actual error is the QUERY syntax in the alter table - this is not allowed.
ythisbug 6-Mar-12 7:44am    
ok..ACTUALLY I WANT TO ADD COLUMN THAT I CAN SET 4MT085100 VALUE IN THAT COLUMN THEN I WANT TO SET THIS AS AUTO INCREMENT.SO M TRYING THAT QUERY.CAN YOU SUGGEST ME?
E.F. Nijboer 7-Mar-12 3:28am    
My advice would be to create another table that has the ID as you want it. You need to do some copying and renaming but I think that's the only way.
You are attempting to mix Data Manipulation and Query syntax in one place. The ALTER TABLE statement is intended to modify a table structure, which is what the first part is doing. The second part looks like you are trying to retrieve a value. What are you trying to achieve here?
 
Share this answer
 
Comments
ythisbug 6-Mar-12 7:31am    
ACTUALLY I WANT TO ADD COLUMN THAT I CAN SET 4MT085100 VALUE IN THAT COLUMN THEN I WANT TO SET THIS AS AUTO INCREMENT.SO M TRYING THAT QUERY.CAN YOU SUGGEST ME?
Hi,

As Pete o'Hanlon said. you are mixing your query,

You need two different query, first for alter your table then second for inserting your first record. and 4MT085100 is varchar field then it could not be auto incremental. you need to write Before Insert Trigger (it should execute before inserting row) that will generate your next number and achieve your increment behavior.

Thanks
-Amit
 
Share this answer
 
Comments
ythisbug 6-Mar-12 7:58am    
ok thanks can you tel me how to write query for that.m stuck for this
AmitGajjar 6-Mar-12 8:01am    
1) create alter query to modify your column
2) insert your first record.
3) create trigger.(if you don't know about trigger then first you need to go through then basics of triggers)

write some code and if you get stuck/ getting any error, you can post it.

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