I have a table with three columns
KeyID | nvarchar(10) ColumnA | nvarchar(max) ColumnB | Nvarchar(max)
What i’m trying to do is a select insert, however with keyID i need to treat it like an indentity column.
For example if I had 3 rows I wanted to insert it would be :
1001 | Apple | Pear 1002 | Pear | Mango 1003 | Pineapple | Pine
But i’m trying to do this with an insert select, for example:
insert into myTable(KeyId,ColumnA,ColumnB) select 'x',OrigColA,OrigColB from myTableB
obviously ‘x’ is where i’m having the issue.
Thanks in advance!
This is no KeyId located in myTableB
This is a bit ugly because of the varchar, but you can try:
This of course assumes there is already a row in
myTableto get the current maximum value from.SQL Fiddle example
If that’s not always true, wrap
MAX(KeyId)with aCOALESCEand the constant of one less than your starting default. For example:Or if you know there isn’t a row and you just want to start at a constant, just do: