if exists(select * from sysobjects where name=@provinceName)
  set @f=1 
else 
  set @f=0

解决方案 »

  1.   

    if exists(select * from sysobjects where name=@provinceName
     set @f=1 
    else set @f=0
    这样就行了,变量在字符串内要使用也必须在字符串里面声名.
      

  2.   

    set @s1 = 'if exists(select * from sysobjects where name='''+@provinceName+''') set @f=1 else set @f=0' 
    exec(@s1) 明显的语法错误
      

  3.   

    if exists(select * from sysobjects where name=@provinceName) --补个括号
     set @f=1 
    else set @f=0
      

  4.   

    create trigger MyTrigger 
    on MyTable 
    for update 
    as 
    set nocount on 
    if update(bustype_name) 
    declare @provinceName varchar(100); 
    declare @s1 varchar(250) 
    declare @f varchar(10) 
    declare provinceCursor cursor for select province from address where isnull(address_name,'')=''
    open provinceCursor 
    fetch next from provinceCursor into @provinceName 
    while(@@fetch_status=0) 
    begin 
    set @provinceName = 'dx_business_'+@provinceName 
    print(@provinceName) 
    --set @s1 = 'if exists(select * from sysobjects where name='''+@provinceName+''') set @f=1 else set @f=0' 
    --*************************************************************************
    if exists(select * from sysobjects where name='+@provinceName+')  
    set @f=1 
    else 
    set @f=0
    --****************************************************************************
    print (@f) 
    if (@f=1) 
    print('exists') else 
    print (@f) 
    set @provinceName = ''; 
    fetch next from provinceCursor into @provinceName end 
    close provinceCursor 
    deallocate provinceCursor 
      

  5.   

    if exists(select  * from sysobjects where name=@provinceName)
    begin
    set @f=1 
    end
    else 
    set @f=0
    end 
      

  6.   

    create trigger MyTrigger 
    on MyTable 
    for update 
    as 
    set nocount on 
    if update(bustype_name) 
    declare @provinceName varchar(100); 
    declare @s1 varchar(250) 
    declare @f varchar(10) 
    declare provinceCursor cursor for select province from address where isnull(address_name,'')=''
    open provinceCursor 
    fetch next from provinceCursor into @provinceName 
    while(@@fetch_status=0) 
    begin 
    set @provinceName = 'dx_business_'+@provinceName 
    print(@provinceName) 
    --set @s1 = 'if exists(select * from sysobjects where name='''+@provinceName+''') set @f=1 else set @f=0' 
    --*************************************************************************
    if exists(select  * from sysobjects where name=@provinceName)
    begin
    set @f=1 
    end
    else 
    begin
    set @f=0
    end 
    --****************************************************************************
    print (@f) 
    if (@f=1) 
    print('exists') else 
    print (@f) 
    set @provinceName = ''; 
    fetch next from provinceCursor into @provinceName end 
    close provinceCursor 
    deallocate provinceCursor 
      

  7.   

    试下7楼的
    我6楼的
    掉了个begin
    if exists(select  * from sysobjects where name=@provinceName)
    begin
    set @f=1 
    end
    else 
    begin
    set @f=0
    end 
      

  8.   

    在exec 中运行的程序,是另一个批处理,在执行 exec 的程序中定义的 @f 的生命周期不在此批处理内.
      

  9.   

    这样就可以用了,我用exec是因为看了这篇文章,http://www.itpub.net/thread-949474-1-1.html。
    一直觉得如果要执行带有变量的语句就要用exec。···看来进入了误区。
    只是不知道我这里的问题和文章中的问题有什么区别?
      

  10.   

    我顺便把那篇文章发上来,大家帮忙看看:地址:http://www.itpub.net/thread-949474-1-1.html
    ·······················在触发器中的变量老提示要声明变量的问题 急疯了??
    完整代码是
    CREATE TRIGGER tougao  ON dbo.yichangxinwen 
    instead   of  insert
    asdeclare  @biaoming  nvarchar(200) 
    if (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '编辑'
    begin
    set @biaoming = 'adlishi'
    endif (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '通讯员'
    begin
    set @biaoming = 'txlishi'
    endif (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '本台记者'
    begin
    set @biaoming = 'bjlishi'
    endif (select quanxian from denglu where yonghu = (select yonghu  from inserted)) = '集体记者'
    begin
    set @biaoming = 'jjlishi'
    end
    if exists(select *  from yichangxinwen where yonghu = (select yonghu  from inserted) and nianyue = (select convert(char(7),getdate(),20)) )
    begin    insert yichangxinwen(biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu)
            select biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu from inserted;
        update txlishi 
            set touzong=touzong+1
            from @biaoming inner join inserted yichangxinwen on @biaoming.yonghu = yichangxinwen.yonghu and @biaoming.nianyue = (select convert(char(7),getdate(),20))
    endif not exists(select *  from yichangxinwen where yonghu = (select yonghu  from inserted) and nianyue = (select convert(char(7),getdate(),20)) )
    begin
                       
         insert yichangxinwen(biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu)
            select biaoti,wenzhang,select1,yonghu,select2,yonghu2,shijian,nianyue,shenghe,danwei,riqi,canshu from inserted;
         insert @biaoming(yonghu,nianyue,touzong)
            select yonghu,convert(char(7),getdate(),20),1 from inserted
                   
    end结果是老说@biaoming 必须声明     是不是触发器中变量的使用方法不正确????  
                    在线等!!!!
      

  11.   

    declare  @biaoming  nvarchar(200) --@biaoming是变量
    insert @biaoming(yonghu,nianyue,touzong) --你把@biaoming当表用了