sql server - SQL copying record with out specifying column list; ignoring Identity -


i'm trying copy record tablea tablea, using new identity. don't want specify column list have on 100 columns, , there may more in future. id chunk of code can run when/if things change.

after looking similar questions, have attempted code

select * #tmp tablea id = 1;     alter table #tmp drop column id;     insert tablea select * #tmp;     drop table #tmp; 

i still getting error

an explicit value identity column in table 'dbo.tablea' can specified when column list used , identity_insert on. 

running select * #tmp gives me expect. single record columns exception of id column.

any ideas?

thanks!

edit here pictures of properties of id column

enter image description here

use dynamic sql: list of columns (except id), build insert statement using list, , call exec on it:

select *    #tmp    tablea    id = 1; alter table #tmp drop column id;  declare @cols varchar(max);  select    @cols = coalesce(@cols + ',', '') + column_name    information_schema.columns    table_name = 'tablea' , column_name <> 'id'  --select @cols -- display column list debugging declare @sqltxt varchar(max) = 'insert tablea (' + @cols + ') select * #tmp';  --select @sqltxt -- display statement debugging  exec (@sqltxt)  drop table #tmp 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -