我的存储过程如下: CREATE PROCEDURE nn AS declare @sql varchar(255) drop table goodzsale set @sql='' /*依据一个表里的值建立一个新表*/ declare temptabl cursor for select comShortName from DataSaleArea open temptabl declare @a varchar(50) fetch temptabl into @a while @@fetch_status=0 begin set @sql=@sql+@a+' int,' fetch next from temptabl into @a end close temptabl set @sql=@sql+'mobtype varchar(255),mobname varchar(255)' exec('create table goodzsale('+@sql+')') open temptabl //将另一个表里的值分组插入新表中 fetch temptabl into @a while @@fetch_status=0 begin exec('insert into goodzsale(mobtype,mobname,'+@a+') select mobtype,mobname,count(mobcode) from view_mobhouse1 where comShortName ='''+@a+''' group by comShortName,mobtype,mobname') fetch next from temptabl into @a end close temptabl GO 如果在delphi里query连到odbc再执行该存储过程正确,但是如果通过datamodule里放的database连到数据库上就会提示出错。
你把这一部分语句拿出来放到Query中执行看看: insert into goodzsale(mobtype,mobname,'+@a+') select mobtype,mobname,count(mobcode) from view_mobhouse1 where comShortName ='''+@a+''' group by comShortName,mobtype,mobname') 注意其中的变量全部换成实际的值。这也许有助于你查错。
CREATE PROCEDURE nn AS
declare @sql varchar(255)
drop table goodzsale
set @sql=''
/*依据一个表里的值建立一个新表*/
declare temptabl cursor for select comShortName from DataSaleArea
open temptabl
declare @a varchar(50)
fetch temptabl into @a
while @@fetch_status=0
begin
set @sql=@sql+@a+' int,'
fetch next from temptabl into @a
end
close temptabl set @sql=@sql+'mobtype varchar(255),mobname varchar(255)'
exec('create table goodzsale('+@sql+')')
open temptabl //将另一个表里的值分组插入新表中
fetch temptabl into @a
while @@fetch_status=0
begin
exec('insert into goodzsale(mobtype,mobname,'+@a+') select mobtype,mobname,count(mobcode) from view_mobhouse1 where comShortName ='''+@a+''' group by comShortName,mobtype,mobname')
fetch next from temptabl into @a
end
close temptabl
GO
如果在delphi里query连到odbc再执行该存储过程正确,但是如果通过datamodule里放的database连到数据库上就会提示出错。
insert into goodzsale(mobtype,mobname,'+@a+') select mobtype,mobname,count(mobcode) from view_mobhouse1 where comShortName ='''+@a+''' group by comShortName,mobtype,mobname')
注意其中的变量全部换成实际的值。这也许有助于你查错。