• 游标后面select 带有in时


    今天遇到一个问题,使用游标时,在给游标填充值的时候,select  语句中带有 where查询条件,并且还有 in子句。

    本来我是这样写的,试了很多次都不出结果,当然number in (304010,305013)是可以出结果的。

    declare @Did varchar(10)
    declare @zdcode varchar(50) set @zdcode='304010,305013'
    declare cur_id cursor for select Number from dbo.IM_Metadata where number in (@zdcode) 
    open cur_id 
    fetch next from cur_id into @Did
    while @@fetch_status=0    
     begin  
    print @Did
    fetch next from cur_id into @Did
    end
    close cur_id
     deallocate cur_id


    后来一番折腾,求助了群里同志们

    果然有了不错的解决方案

    就是添加一个函数:

    CREATE   FUNCTION [dbo].[f_split]  
    (  
    @c VARCHAR(MAX) ,  
    @split VARCHAR(50)  
    )  
    RETURNS @t TABLE ( col VARCHAR(50) )  
    AS  
    BEGIN  
        WHILE ( CHARINDEX(@split, @c) <> 0 )  
            BEGIN  
                INSERT  @t( col )  
                VALUES  ( SUBSTRING(@c, 1, CHARINDEX(@split, @c) - 1) )  
                SET @c = STUFF(@c, 1, CHARINDEX(@split, @c), '')  
            END  
        INSERT  @t( col ) VALUES  ( @c )  
        RETURN  
    END  

    然后使用时这样:

    declare cur_id cursor for select Number from dbo.IM_Metadata where number in (select * from f_split(@zdcode , ',')) 

    ok问题解决了。

  • 相关阅读:
    viewpoint vw适配 兼容方案
    函数参数默认值
    vue v-bind 的prop属性
    vue 全局错误处理 errorHandler
    Python模块学习
    频谱共享---小记
    LTE的信道
    PLMN(公共陆地移动网络)
    单元测试框架GoogleTest
    OpenRAN是什么
  • 原文地址:https://www.cnblogs.com/agile2011/p/4108926.html
Copyright © 2020-2023  润新知