在存储的Proc游标中调用函数

时间:2011-07-01 16:15:53

标签: sql-server tsql sql-server-2008

我有一个函数“Split”,我用光标在存储过程中调用它。

我的a_upload表格结构为:(int, varchar(100))

upload_id   allowed_file_extensions
---------------------------------------------------------------------
1           .xls, .doc, .pdf, .docx, .xlsx, .pptx, .txt
2           .xls, .doc, .pdf, .jpeg, .jpg, .docx, .xlsx, .pptx, .txt

c_file_extensions是:(int, varchar(100), bit)

id  description  is_enabled
---------------------------
1   .xls         1
2   .doc         1
3   .pdf         0
4   .rtf         1

存储过程:

DECLARE @is_enabled Varchar(10), @allowed_file_extensions Varchar(100)

   SET @is_enabled = 'True';

DECLARE cur CURSOR FORWARD_ONLY FOR 
  SELECT items 
    FROM split((SELECT allowed_file_extensions 
                  FROM a_upload 
                 WHERE upload_id = 1), ',') 

OPEN cur
FETCH NEXT FROM cur INTO @allowed_file_extensions
WHILE @@fetch_status=0
BEGIN

    IF EXISTS(SELECT * 
                FROM c_file_extensions 
               WHERE description = @allowed_file_extensions 
                 AND is_enabled = 0)
      SET @is_enabled = 'False';

FETCH NEXT FROM cur INTO @allowed_file_extensions
END
CLOSE cur
DEALLOCATE cur

SELECT @is_enabled AS Output

功能拆分:

CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))       
  RETURNS @temptable TABLE (items varchar(8000)) AS

BEGIN
    DECLARE @idx int       
    DECLARE @slice varchar(8000)       

    SELECT @idx = 1    

    IF LEN(@String) < 1 OR @String IS NULL RETURN

    WHILE @idx!= 0       
    BEGIN       
        SET @idx = CHARINDEX(@Delimiter,@String)       
        IF @idx != 0       
          SET @slice = LEFT(@String,@idx - 1)       
        ELSE       
          SET @slice = @String       

          IF(LEN(@slice)>0)  
            INSERT INTO @temptable
             (Items) 
            VALUES
             (@slice)       

          SET @String = RIGHT(@String, LEN(@String) - @idx)       
          IF LEN(@String) = 0 BREAK
    END  
RETURN      
END  

对于upload_id = 1,我希望输出为“False”,但我总是得到“True”。我试图调试,发现“if exists (select * from c_file_extensions where description = @allowed_file_extensions and is_enabled = 0)”无法正常工作。

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您正在与具有前导空格的项目进行比较。

试试这个:

  select items from split((select ext from a_upload where id = 1), ',') 

这导致:

.xls
 .doc
 .pdf
 .docx
 .xlsx
 .pptx
 .txt

要解决此问题,您可以:

  • 修改光标定义:
   
select  LTRIM(items) from split((select ext from a_upload where id = 1), ',') 
  • 修改您的dbo.split()以删除第29行的前导空格:
   
set @String = LTRIM(right(@String,len(@String) - @idx))