declare @pid varchar(200),@strWhere varchar(800)

exec('select ' +@pid+'=picid from t_classpicture where 1=1 ' +@strWhere+')')这段SQL拼凑语句出错,报“消息 102,级别 15,状态 1,第 1 行
'=' 附近有语法错误。”这种错误,求解,不胜感激

解决方案 »

  1.   

    不要急着exec ,先print 
      

  2.   

    exec换成
    print 'select ' +@pid+'=picid from t_classpicture where 1=1 ' +@strWhere+')'
      

  3.   

    也print了,但是消息框中没见到打印的语句?
      

  4.   

    继续求解,print其它语句可以打印出来,但是这句却没有显示,不显示也不理解什么原因
      

  5.   

    ---@pid后面需要空格
    exec('select ' +@pid+ '=picid from t_classpicture where 1=1 ' +@strWhere+')')
      

  6.   

    还是有问题,print('select ' +@pid+ '=picid from t_classpicture where 1=1 ' +@strWhere)无任何消息,没有打印语句出来,但是换成exec出错,错误同上,无语啊,汗如雨注
      

  7.   

    说明这两个变量中至少有一个为null
    你最好在用前先set @变量=''
      

  8.   

    将' +@pid+ '改成这样就可以了'''+@pid+'''Create table t_classpicture(picid varchar(100),createper varchar(20))
    insert into t_classpicture
    select '100','bill' union all
    select '200','james'declare @pid varchar(200)
    set @pid = '100'
    declare @strWhere varchar(800)
    set @strWhere = ' and createper = ''james'''
    exec ('select '''+@pid+''' = picid from t_classpicture where 1=1 '+@strWhere+'')
      

  9.   

    这个查询语句中 '+@pid+' 这个变量好象是多余的.