存储过程有参数@aid,@bid,@cid等参数
DECLARE @Where varchar(1024)
if(@aid=0)
set @Where='bid='+@bid
else
set @Where='cid='+@cid
.....下面判断是否存在记录 IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and + @Where))
 begin 
      update 数据
  where @Where
 end
 else
 begin
insert 数据
 end这么写有语法错误,高手给指点一下吧

解决方案 »

  1.   

    print出来看看是什么样子的
      

  2.   

    你这里头很多语法错误的地方,看你的样子应该是想用动态SQL的,等等我找个例子给你
      

  3.   

    主要是这块儿 加那个@Where 条件的时候错
    IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and + @Where))
      

  4.   

    不能直接加,这种要用动态SQL
      

  5.   

    首先变量赋值的时候要考虑到数据的兼容性问题,你赋值的地方可以参考:
    set @strAircomE =' AND [dbo].[IsExtisAir](AircomE,'''+@FlightNo+''',IsUse)=1 '
    其次:IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and + @Where)) 不可以这样写,既然你用的是拼接sql语句的方式去执行sql,就要使用exec()或者exec sp_executesql @sqlstring 这种方式来执行你拼接的sql 。update 数据  where @Where ---同理
      

  6.   

    拼接好sql串,exec执行是吧?
      

  7.   

    我不清楚能不能把IF EXISTS这些也写进去
      

  8.   


    declare @sql varchar(max)
    set @sql='
    IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and '+ @Where+'))
     begin 
          update 数据
      where @Where
     end
     else
     begin
    insert 数据
     end'
     --print @sql
     exec(@sql)
      

  9.   

    declare @sql varchar(max)
    set @sql='
    IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and '+ @Where+'))
     begin 
          update 数据
      where '+@Where +
     'end
     else
     begin
    insert 数据
     end'
     --print @sql
     exec(@sql)
      

  10.   

    declare @sql varchar(max)
    set @sql='
    IF(EXISTS(SELECT 1 FROM table1 WHERE 1=1 and '+ @Where+'))
     begin 
          update 数据
      where '+@Where +
     'end
     else
     begin
    insert 数据
     end'
     --print @sql
     exec(@sql)