我想将表中的某些行复制到同一个表中。 id
表的TForms
列不是自动增量,而是primary key
。我怎样才能做到这一点?
set @sourcetid = 17
set @newtbn = 15
set @template ='
INSERT INTO ' +@DB + '.[Tforms]
(id
,[tablename])
select id,
@newtbn)
from '+ @DB+ '.[Tforms] where tid=' +str(@sourcetid)
exec sp_Executesql @template
答案 0 :(得分:0)
一种方法是获取最大ID并将其添加到每个当前id:
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char name[101], temp;
// take input one character at a time
while(scanf("%c", &temp)){
// stop when newline or carriage return
if(temp == '\n' || temp == '\0' || !isalpha(temp) ){
break;
}
// save character in array
name[i] = temp;
// move to the next position of the array
i++;
}
printf( "%s", temp );
return 0;
}
注意:
insert into ' +@DB + '.[Tforms](id, [tablename])
select (max_id + id) as id, @newtbn
from (select t.*, max(id) over () as max_id
from '+ @DB+ '.[Tforms] t
) t
where tid = ' +str(@sourcetid) ;
。如果1
s为负数,则无法使用此方法。id
或identity
,这样您就不必手动设置它。newid()
)。编辑:
无论最小值和最大值如何,更通用的方法都有效:
sp_executesql