/***************************************************************************
* Procedure sp_file_article_records
* CreateDate: 10-05-2010
* Author: 
* Revisions:
* Date: null Author: none
* Description: null
****************************************************************************/if exists (select 1 from sys.objects where name = 'sp_file_article_records' and type = 'p')
drop procedure sp_file_article_records
gocreate procedure sp_file_article_records
@Id int,
@PeriodicalSortId int,
@PeriodicalId int,
@PageId int,
@ArticleSortId int,
@LoginId int,
@DateStart datetime,
@DateEnd datetime,
@TheState int,
@Page int,
@Rows int,
@Key varchar (20)
as
declare @SqlStr varchar (50)
declare @Str varchar (50)set @Str = 'TheState = '+@TheState+' '
if @Id <> 0 or @Id <> null
set @Str += 'and Id = '+@Id+' '
if @PeriodicalSortId <> 0 or @PeriodicalSortId <> null
set @Str += 'and PeriodicalSortId ='+@PeriodicalSortId+' '
if @PeriodicalId <> 0 or @PeriodicalId <> null
set @Str += 'and PageId ='+@PageId+' '
if @ArticleSortId <> 0 or @ArticleSortId <> null
set @Str += 'and ArticleSortId ='+@ArticleSortId+' '
if @LoginId <> 0 or @LoginId <> null
set @Str += 'and LoginId ='+@LoginId+' '
if (@DateStart <> null or @DateStart <> null)
and
(@DateEnd <> null or @DateEnd <> null)
set @Str += 'and UploadDate Between '+@DateStart+' and '+@DateEnd+' '
if @Key <> '' or @Key <> null
set @Str += 'and (Title like %'+@Key+'% or Content like %'+@Key+'% 'set @SqlStr = 'select Top '+@Rows+' * from cpn_file_article where '
set @SqlStr+= 'Id not in (select Top '+(@Page - 1) * @Rows +' Id where '
set @SqlStr+= @Str
set @SqlStr+= 'order by Id desc) and '
set @SqlStr+= @Str
set @SqlStr+= 'order by Id desc'
print @sqlstr
go
execute sp_file_article_records 1,1,1,1,1,1,'2010-05-10 09:40:16','2010-05-10 09:40:16',0,10,100,'KKK'
错误:在将 varchar 值 'TheState = ' 转换成数据类型 int 时失败。

解决方案 »

  1.   

    拼写字符串的时候不是非要把所有的变量都显示的转成varchar吧?
      

  2.   

    @Id <> null
    不等于null 要用 is not null
      

  3.   

    create procedure sp_file_article_records
        @Id int,
        @PeriodicalSortId int,
        @PeriodicalId int,
        @PageId int,
        @ArticleSortId int,
        @LoginId int,
        @DateStart datetime,
        @DateEnd datetime,
        @TheState int,
        @Page int,
        @Rows int,
        @Key varchar (20)
    as
    declare @SqlStr varchar (50)
    declare @Str varchar (50)set @Str = 'TheState = '+ltrim(@TheState)+' '
    if @Id <> 0 or @Id <> null
        set @Str += 'and Id = '+ltrim(@Id)+' '
    if @PeriodicalSortId <> 0 or @PeriodicalSortId <> null
        set @Str += 'and PeriodicalSortId ='+ltrim(@PeriodicalSortId)+' '
    if @PeriodicalId <> 0 or @PeriodicalId <> null
        set @Str += 'and PageId ='+ltrim(@PageId)+' '
    if @ArticleSortId <> 0 or @ArticleSortId <> null
        set @Str += 'and ArticleSortId ='+ltrim(@ArticleSortId)+' '
    if @LoginId <> 0 or @LoginId <> null
        set @Str += 'and LoginId ='+ltrim(@LoginId)+' '
    if (@DateStart <> null or @DateStart <> null)
        and
        (@DateEnd <> null or @DateEnd <> null)
        set @Str += 'and UploadDate Between '''+@DateStart+''' and '''+@DateEnd+''' '
    if @Key <> '' or @Key <> null
        set @Str += 'and (Title like ''%'+@Key+'%'' or Content like ''%'+@Key+'%'' 'set @SqlStr = 'select Top '+ltrim(@Rows)+' * from cpn_file_article where '
    set @SqlStr+= 'Id not in (select Top '+ltrim(@Page - 1) * ltrim(@Rows) +' Id where '
    set @SqlStr+= @Str
    set @SqlStr+= 'order by Id desc) and '
    set @SqlStr+= @Str
    set @SqlStr+= 'order by Id desc'
    print @sqlstr
    go
      

  4.   

    ltrim(@Page - 1) * ltrim(@Rows)->ltrim((@Page - 1) * Rows) 
      

  5.   

    alter procedure sp_file_article_records
        @Id int,
        @PeriodicalSortId int,
        @PeriodicalId int,
        @PageId int,
        @ArticleSortId int,
        @LoginId int,
        @DateStart datetime,
        @DateEnd datetime,
        @TheState int,
        @Page int,
        @Rows int,
        @Key varchar (20)
    as
    declare @SqlStr varchar (1000)
    declare @Str varchar (100)set @Str = 'TheState = '+ltrim(@TheState)+' '
    if @Id <> 0 or @Id <> null
        set @Str += 'and Id = '+ltrim(@Id)+' '
    if @PeriodicalSortId <> 0 or @PeriodicalSortId <> null
        set @Str += 'and PeriodicalSortId ='+ltrim(@PeriodicalSortId)+' '
    if @PeriodicalId <> 0 or @PeriodicalId <> null
        set @Str += 'and PageId ='+ltrim(@PageId)+' '
    if @ArticleSortId <> 0 or @ArticleSortId <> null
        set @Str += 'and ArticleSortId ='+ltrim(@ArticleSortId)+' '
    if @LoginId <> 0 or @LoginId <> null
        set @Str += 'and LoginId ='+ltrim(@LoginId)+' '
    if (@DateStart <> null or @DateStart <> null)
        and
        (@DateEnd <> null or @DateEnd <> null)
        set @Str += 'and UploadDate Between '''+@DateStart+''' and '''+@DateEnd+''' '
    if @Key <> '' or @Key <> null
        set @Str += 'and (Title like ''%'+@Key+'%'' or Content like ''%'+@Key+'%'' 'set @SqlStr = 'select Top '+ltrim(@Rows)+' * from cpn_file_article where '
    set @SqlStr+= 'Id not in (select Top '+ltrim((@Page - 1) * @Rows) +' Id where '
    set @SqlStr+= @Str
    set @SqlStr+= 'order by Id desc) and '
    set @SqlStr+= @Str
    set @SqlStr+= 'order by Id desc'
    print @sqlstr
    go
    execute sp_file_article_records 1,1,1,1,1,1,'2010-05-10 09:40:16','2010-05-10 09:40:16',0,10,100,'KKK'select Top 100 * from cpn_file_article where Id not in (select Top 900 Id where TheState = 0 and Id = 1 and PeriodicalSortId =1 and PageId =1 and ArticleSortId =1 and LoginId =1 anorder by Id desc) and TheState = 0 and Id = 1 and PeriodicalSortId =1 and PageId =1 and ArticleSortId =1 and LoginId =1 anorder by Id desc
      

  6.   

    这里的判断为什么不管用,当@PeriodicalSortId = 0的时候,还是会把字符串叠加上??
      

  7.   

    这里的判断为什么不管用,当@PeriodicalSortId = 0的时候,还是会把字符串叠加上??