ALTER proc [dbo].[proc_GetPrjWhere]
    @Dptid int = null,
    @Userid int = null,
    @PrjCode varchar(20) = null,
    @BCreatdate datetime = null,
    @ECreatdate datetime = null
as
declare @where varchar(200)
set @where = '1=1'
if(@Dptid is not null and @Userid is null)
begin
set @where = 'Dptid ='+LTRIM(@Dptid)
end
if(@Userid is not null and @Dptid is not null)
begin
set @where ='Userid ='+LTRIM(@Userid)+' and Dptid ='+LTRIM(@Dptid)
end
if(@PrjCode is not null)
begin
set @where ='PrjCode = '+@PrjCode
end
exec('select * from Info_prjvalues where '+@where+'')执行后
消息 207,级别 16,状态 1,第 1 行
列名 '有间客栈' 无效。(1 行受影响)

解决方案 »

  1.   

    set @where ='PrjCode = '''+@PrjCode+''''
      

  2.   

    大侠,请问存储过程执行后能不能看到它的Sql语句是怎样的?
      

  3.   

    ALTER proc [dbo].[proc_GetPrjWhere]
        @Dptid int = null,
        @Userid int = null,
        @PrjCode varchar(20) = null,
        @BCreatdate datetime = null,
        @ECreatdate datetime = null
    as
    declare @where varchar(200)
    set @where = '1=1'
    if(@Dptid is not null and @Userid is null)
    begin
    set @where = 'Dptid ='+LTRIM(@Dptid)
    end
    if(@Userid is not null and @Dptid is not null)
    begin
    set @where ='Userid ='+LTRIM(@Userid)+' and Dptid ='+LTRIM(@Dptid)
    end
    if(@PrjCode is not null)
    begin
    set 'PrjCode = '''+@PrjCode+''''
    end
    print 'select * from Info_prjvalues where '+@where+''  --print 
    exec('select * from Info_prjvalues where '+@where+'')
      

  4.   

    可以在exec之前print下看看。例如:print @sql
    exec(@sql)