declare @cc int
exec sp_executesql N'select @cc=sum(I_RecCount) from T_Category where I_CategoryID in ('+@name+N')'
                   N'@cc int out',
                   @cc out
select @cc

解决方案 »

  1.   

    declare @cc int
            ,@sql nvarchar(4000)
            ,@name varchar(100)
    set @sql=' select @cc=sum(I_RecCount) from T_Category '+
             ' where charindex(I_CategoryID,@name)>0'
    exec sp_executesql @sql
                       ,N'@cc int out,@name varchar(100)'
                       ,@cc out,@name
      

  2.   

    --create   proc P_SoBiGo_RecCountLogivCategory
    --as ------------
    ----@name  为('434231,4321,4231,4321,')这样的类型
    ----I_categoryID 为整型 , S_IncludeCategoryID 为('3421,4321,4321,')
    declare @cc int
    declare @name varchar(1024)
    DECLARE   tot_cursor  CURSORFOR  SELECT S_IncludeCategoryID FROM T_LogicCategoryopen tot_Cursor
    fetch next from tot_Cursor into @name
    ---------------
    --OPEN  tot_cursor--FETCH NEXT FROM  tot_cursor INTO @name
    set @name=substring(@name,0,(len(@name)-1))
    WHILE (@@fetch_status <> -1)
    BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
    -- PRINT 'add user defined code here' 
    -- eg.
    --PRINT 'updating record for ' + @name
                    exec  ('select ('+@cc+')=sum(I_RecCount) from T_Category 
                         where I_CategoryID in ('+@name+')')
                    
                    print @cc
    --UPDATE T_LogicCategory
    --SET I_RecCount=@cc
    --WHERE CURRENT OF  tot_cursor
                ---fetch next from tot_cursor into @name
    END
    FETCH NEXT FROM  tot_cursor INTO @name
            set @name=substring(@name,0,(len(@name)-1))
    ENDCLOSE  tot_cursor
    DEALLOCATE  tot_cursor
      

  3.   

    --try:--create   proc P_SoBiGo_RecCountLogivCategory
    --as ------------
    ----@name  为('434231,4321,4231,4321,')这样的类型
    ----I_categoryID 为整型 , S_IncludeCategoryID 为('3421,4321,4321,')
    declare @cc int
    declare @name varchar(1024)
    declare @sql nvarchar(4000)set @sql=''
    DECLARE   tot_cursor  CURSORFOR  SELECT S_IncludeCategoryID FROM T_LogicCategoryopen tot_Cursor
    fetch next from tot_Cursor into @name
    ---------------
    --OPEN  tot_cursor--FETCH NEXT FROM  tot_cursor INTO @name
    set @name=substring(@name,0,(len(@name)-1))
    WHILE (@@fetch_status <> -1)
    BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
    -- PRINT 'add user defined code here' 
    -- eg.
    --PRINT 'updating record for ' + @name
    set @sql=' select @cc=sum(I_RecCount) from T_Category '+
              ' where charindex(I_CategoryID,@name)>0'
    exec sp_executesql @sql
                       ,N'@cc int out,@name varchar(100)'
                       ,@cc out,@name
                    print @cc
    --UPDATE T_LogicCategory
    --SET I_RecCount=@cc
    --WHERE CURRENT OF  tot_cursor
                ---fetch next from tot_cursor into @name
    END
    FETCH NEXT FROM  tot_cursor INTO @name
            set @name=substring(@name,0,(len(@name)-1))
    ENDCLOSE  tot_cursor
    DEALLOCATE  tot_cursor
      

  4.   

    数据类型 int 对于函数 charindex 无效。允许的类型为: char/varchar、nchar/nvarchar 和 binary/varbinary。I_CategoryID  是属于整型
      

  5.   

    declare @lin int
    declare @pagesize int
    declare @tblname char(40)
    declare @strwhere varchar(200)
    declare @sqlstr  varchar(200)
    set @tblname="t_category"
    set @strwhere=''
    set @pagesize=20
    select @lin=count(*) from ['+@tblname+']