日期:2014-05-17  浏览次数:20529 次

消息 102,级别 15,状态 1,第 3 行
create proc P_TCC_INF_UseCaseQuery  
as  
begin  
declare @opename varchar(200)
declare @tablename varchar(200)
declare @module_displayname varchar(200)
declare @projectidField varchar(200)
declare @keyField varchar(100)
declare @registerdatefield varchar(200)
declare @registernameField varchar(100)
declare @statusfield varchar(1000)
declare @sql varchar(8000)
declare @sid bigint
declare @cur cursor
 
create table #t(
TOpeName varchar(200),TtableName varchar(200),
TModule_DisplayName varchar(200),TprojectidField varchar(200),
TkeyField varchar(100),Tregisterdatefield varchar(200),
TregisternameField varchar(100),Tstaus varchar(1000)
)

set @cur = cursor
for
select OpeName,tableName,Module_DisplayName,projectidField,keyField,registerdatefield,
registernameField,statusfield from TCC_V_INF_UseCaseQuery
 
open @cur  
fetch next from @cur into @OpeName,@tableName,@Module_DisplayName,
@projectidField,@keyField,@registerdatefield,@registernameField,@statusfield  
while @@FETCH_STATUS=0  
begin  
set @sql = 'insert into #t (TprojectidField,TkeyField,Tregisterdatefield,TregisternameField,Tstaus) 
select '+@projectidField+','+@keyField+',
'+@registerdatefield+','+@registernameField+','+@statusfield+' from ' +@tablename+ ' '
exec (@sql)
fetch next from @cur into @OpeName,@tableName,@Module_DisplayName,@projectidField,
@keyField,@registerdatefield,@registernameField,@statusfield
end 
close @cur  
deallocate @cur 
select * from #t
drop table #t
end
 
哪位大侠神仙指点一下迷津啊,老是保这个错:
消息 102,级别 15,状态 1,第 3 行 ',' 附近有语法错误。

不胜感激啊!!!!!!!!!!!!!!!!!
 

------解决方案--------------------
你后面动态拼接的时候,如果有参数为''值,就会报语法错误。

set @sql = 'insert into #t (TprojectidField,TkeyField,Tregisterdatefield,TregisternameField,Tstaus)
select '+@projectidField+','+@keyField+',
'+@registerdatefield+','+@registernameField+','+@statusfield+' from ' +@tablename+ ' '

上面是5个字段,下面是5个值,值为null 或是 '' 的时候会出错的。