insert into的问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 01:53:41
我想使用insert into 实现自动往临时表中插入数据
我临时表的列定义的时候使用了COL+i(i是一个循环加1的值)
在我插入临时表的时候写成如下
select count(1) from table where........
insert into temp (col+i) values(count(1))

系统报错,说col+i缺少逗号,可是我是想让他动态的写不同的列,如何才能实现呢?请高手帮忙阿
刚才有个sql写错了
是这样
select count(1) into count1 from table where ....
insert into temp(col+i)values(count1)
一楼的可能没有理解我的意思,我的意思是
怎样按照计数器的方式每次动态的插入不同的列
比如
insert into (列1) values (值1)

insert into (列2)values (值2)
.......
insert into (列I)values (值I)
以此类推。

要在数据库里处理的话,就必须构造SQL

while @i<@max
begin
@sql='insert into temp(col'+convert(varchar,@i)+'...) values.....'
exec(@sql)
set @i=@i+1
end

如果是前台的话,ADO的Field一般都有序号的,直接用对应的格式就ok了

select count(1) from table where........
insert into temp (col+i) values(count(1))
暂时设你的表table的动态种子列名为id

你的count(1)不是计算1的多少。。count(列名)
建议你
declare @i int
set @i=select count(*) from table
while(@i>0)
begin
insert into table(项名)values(值)where id=@i
set @i=@i+1
end

意思差不多。。我没编译。。

如果是嵌入式SQL,就是通过ODBC或者JDBC写程序那当然就只要在JAVA或者ODBC中动态生成一个字符串就行了.
而且你要求的这种功能恐怕未必有:
因为插入第一列时或产生100条记录.
插入第二列时又会产生100条新记录.
所以可能应该用update才是.