declare @sql nvarchar(2000)
        SET @sql = CASE @ShowType
        WHEN 1  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=1'
        WHEN 2  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=2 and CHARINDEX(','+LTRIM(@UserID)+',', ','+ShowType+',')>0'
        WHEN 3  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=3 and ShowType='+LTRIM(@UserID)+''
        ELSE '0' END
set @sql=@sql+' order by ID desc'  
ExEc (@sql) 

解决方案 »

  1.   

       SET @sql = CASE @ShowType
            WHEN 1  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=1'
            WHEN 2  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=2 and CHARINDEX('',''+LTRIM(@UserID)+'','', '',''+ShowType+'','')>0'
            WHEN 3  THEN 'select top '+cast(@TopNum as varchar)+' * from RLNotices where NoticeType=3 and ShowType='+LTRIM(@UserID)+''
            ELSE '0' END
      

  2.   

    DECLARE @sql NVARCHAR(2000)    
    SET @sql = CASE @ShowType
                    WHEN 1 THEN 'select top '+CAST(@TopNum AS VARCHAR)+
                         ' * from RLNotices where NoticeType=1'
                    WHEN 2 THEN 'select top '+CAST(@TopNum AS VARCHAR)+
                         ' * from RLNotices where NoticeType=2 and CHARINDEX('','+LTRIM(@UserID)+','', '',''
                         +ShowType+'','')>0'
                    WHEN 3 THEN 'select top '+CAST(@TopNum AS VARCHAR)+
                         ' * from RLNotices where NoticeType=3 and ShowType='+LTRIM(@UserID)
                    ELSE '0'
               END
    SET @sql = @sql+' order by ID desc'  
    EXEC (@sql) 
      

  3.   

    例如:
    select 'aa''a'
    会显示出
    aa'a
    又如
    set @x='aa''a'
    @x的内容其实就是aa'a