回 帖 发 新 帖 刷新版面

主题:用Transact-SQL 如何写记录集循环判断?

SELECT @webID=webID,@webAddress=webAddrees,@content=content FROM webPageInfo

WHILE 条件如何写
BEGIN
    输出结果
END

asp代码:
......
do while not rs.eof

  ....
rs.movenext
Loop
........

是否需要看游标的知识?

回复列表 (共2个回复)

沙发

如果想用T-SQL实现, 要用游标

板凳

DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName FROM Northwind.dbo.Employees
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
   FETCH NEXT FROM Employee_Cursor
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor

如果使用两个循环会是怎样?好像判断条件“@@FETCH_STATUS = 0”不行啊》
注释
由于 @@FETCH_STATUS 对于在一个连接上的所有游标是全局性的,要小心使用 @@FETCH_STATUS 。在执行一条 FETCH 语句后,必须在对另一游标执行另一 FETCH 语句前测试 @@FETCH_STATUS 。在任何提取操作出现在此连接上前,@@FETCH_STATUS 的值没有定义。

例如,用户从一个游标执行一条 FETCH 语句,然后调用一个存储过程,此存储过程打开并处理另一个游标的结果。当控制从被调用的存储过程返回后,@@FETCH_STATUS 反映的是在存储过程中执行的最后的 FETCH 语句的结果,而不是在存储过程被调用之前的 FETCH 语句的结果。
两个循环的条件如何判断????



SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER     PROCEDURE IndexOrderID
@SearchKeys varchar(500),@p_tot varchar(500) output AS
--参数SearchKeys:多个搜索词连成字符串,形如:   ":AAA:BBB:CCC:"


-----定义搜索词临时表,由于存贮搜索词,相当数组的作用。
create table #SearchKey_table
(
rowID bigint  IDENTITY (1, 1) PRIMARY KEY,
SearchKey varchar(100),
SearchKeyOrderID int,
KeyRight numeric(10,2)
)
----------------------------------------

-----分析搜索词-------------------------
DECLARE @SearchKeyNum int --搜索词的个数
DECLARE @p int --分析搜索词字符串的前指针
DECLARE @q int --分析搜索词字符串的后指针
DECLARE @searchKeyLen int --搜索词的长度
DECLARE @searchKey varchar(100) --分割后单个搜索词
DECLARE @KeyRight numeric(10,2)--某个关键词加权值
DECLARE @SearchKeyOrderID int--某个搜索词排序值
DECLARE @i int --循环次数
set @p=1
set @q=1
set @SearchKeyNum=0
set @i=0
WHILE len(@SearchKeys) > 0 and @q<>0 and  @q<len(@SearchKeys)
BEGIN
    set @i=@i+1
    set @SearchKeyNum=@SearchKeyNum+1
    set @q=CHARINDEX(':',@SearchKeys,@p+1)
    if @q=0 set @q=len(@SearchKeys)
    set @searchKeyLen=@q-@p-1
    set @searchKey=SUBSTRING(@SearchKeys,@p+1,@searchKeyLen)
    set @p=@q
    set @SearchKeyOrderID=@i
    INSERT INTO #SearchKey_table  (SearchKey) values(@searchKey)
END
---------------------------------------


------定义临时排序表--------------------
create table #indexOrderID_table
(
rowID bigint  IDENTITY (1, 1) PRIMARY KEY,
webID bigint not null ,
webAddress varchar(500),
title varchar(500),
ContentIndex varchar(5255),
OrderID numeric(10,2)
)
DECLARE @webID bigint
DECLARE @webAddress varchar(500)
DECLARE @content varchar(8000)
DECLARE @contentLen int
DECLARE @OrderID int
DECLARE @MatchNum int --匹配的个数


SET NOCOUNT ON

DECLARE SearchIndex CURSOR FOR
SELECT webID,webAddrees,content FROM webPageInfo WHERE isnew=0
OPEN SearchIndex
FETCH NEXT FROM SearchIndex INTO @webID, @webAddress,@content



------数据库搜索一遍------------
WHILE @@FETCH_STATUS = 0
BEGIN
   select @@FETCH_STATUS
   FETCH NEXT FROM SearchIndex INTO @webID, @webAddress,@content
   --select @webID as x,@webAddress as y,@content as content
   --set @content='aaa邮箱sdfsdfds美女sdfn邮箱kdv'
   --处理多个搜索词的全文匹配情况
   DECLARE SearchKey CURSOR FOR
   SELECT SearchKey,SearchKeyOrderID FROM  SearchKey_table
   OPEN SearchKey
   FETCH NEXT FROM SearchKey INTO @SearchKey ,@SearchKeyOrderID
   set @i=0
   WHILE @i< @SearchKeyNum
   BEGIN

      set @i=@i+1
      --处理某个搜索词的全文匹配情况
      FETCH NEXT FROM SearchKey INTO @SearchKey ,@SearchKeyOrderID
      set @p=1
      set @q=1
      set @MatchNum=0
      WHILE len(@content) > 0 and @q<>0 and  @q<len(@content)
      BEGIN
          set @q=CHARINDEX(@searchKey,@content,@p+len(@searchKey))
          if @q=0
            set @q=len(@content)
          else
            set @MatchNum=@MatchNum+1
       --select @p as p,@q as q ,@i as i
          set @p=@q
      END

      if @MatchNum=1
      BEGIN
       --计算关联度
        set @KeyRight=1+(@SearchKeyOrderID-1)*(1/2*@SearchKeyNum)
        set @OrderID=@KeyRight
        INSERT INTO #indexOrderID_table  (webID,webAddress,OrderID) values(@webID,@webAddress,@OrderID)
      End

      if @MatchNum>1
      BEGIN
    --计算关联度
        set @KeyRight=1+(@SearchKeyOrderID-1)*(1/2*@SearchKeyNum)
        set @OrderID=@KeyRight+(@KeyRight-1/@MatchNum)
        INSERT INTO #indexOrderID_table  (webID,webAddress,OrderID) values(@webID,@webAddress,@OrderID)
      End
    
   END
   CLOSE SearchKey
   DEALLOCATE SearchKey


END



CLOSE SearchIndex
DEALLOCATE SearchIndex

SELECT * FROM #SearchKey_table

SELECT * FROM #indexOrderID_table




GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



declare @tot_amt int
execute IndexOrderID ':邮箱:美女:',@tot_amt output

我来回复

您尚未登录,请登录后再回复。点此登录或注册