Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cursor 删除sql语句中的第一个单词_Cursor_Trim_Sql Function - Fatal编程技术网

Cursor 删除sql语句中的第一个单词

Cursor 删除sql语句中的第一个单词,cursor,trim,sql-function,Cursor,Trim,Sql Function,我在Microsoft SQL中有一个创建自动SQL行的函数(Querybuilder)。在本例中,我的一个游标中有一行,看起来像这样: set @Query = @Query + ' AND ('+ (case when(@CountryDelivery = 1) then 'Delivery' else 'pickup' end) +'.CountryUK '+ (case(@CountryExcl) when 1 then ' <> ''' else ' = '''

我在Microsoft SQL中有一个创建自动SQL行的函数(Querybuilder)。在本例中,我的一个游标中有一行,看起来像这样:

  set @Query = @Query + ' AND ('+ (case when(@CountryDelivery = 1) then 'Delivery' else     'pickup' end) +'.CountryUK '+ (case(@CountryExcl) when 1 then ' <> ''' else ' = ''' end) + @country + ''')'    
set@Query=@Query+'和('+(case when(@CountryDelivery=1)然后'Delivery'或者'picking'结束)+'.CountryUK'+(case(@countryexc)when 1然后'''else'=''结束)+@country+''
这将使我的结果看起来像: “AND(country='USA')和(country='Canada')……”等,都取决于用户在其数据表中存储了多少个国家

我的挑战是,我希望在查询开始时删除“AND”语句,但如果用户添加更多国家,则不希望删除

这是我的全部功能:

SET @Query = @Query + ' ('  
declare Accountring_Country1Cursor Cursor for 
        select Country, Exclude, IsDelivery, ID from dbo.Accounting_Country1 where Service_ID = @Service_ID        
OPEN Accountring_Country1Cursor
FETCH NEXT FROM Accountring_Country1Cursor 
INTO @country, @CountryExcl, @CountryDelivery, @country_ID
WHILE @@FETCH_STATUS = 0
BEGIN
set @Query = @Query + ' AND ('+ (case when(@CountryDelivery = 1) then 'Delivery'     else 'pickup' end) +'.CountryUK '+ (case(@CountryExcl) when 1 then ' <> ''' else ' = ''' end) + @country + ''')'    
    FETCH NEXT FROM Accountring_Country1Cursor 
INTO @country, @CountryExcl, @CountryDelivery,@country_ID
END 
CLOSE Accountring_Country1Cursor
DEALLOCATE Accountring_Country1Cursor 
SET @Query = @Query + ') '              
RETURN @Query

     END
SET@Query=@Query+'('
声明Accounting\u Country1游标用于
从dbo.Accounting\u Country1中选择Country、Exclude、IsDelivery、ID,其中Service\u ID=@Service\u ID
开户银行
从Accounting_Country1Cursor获取下一个
进入@country、@CountryExcl、@CountryDelivery、@country\u ID
而@@FETCH\u STATUS=0
开始
设置@Query=@Query+'和('+(case-when(@CountryDelivery=1)然后是'Delivery'或'picking'结束)+'.CountryUK'+(case(@CountryExcl)when-1然后是'''else'=''结束)+@country++''
从Accounting_Country1Cursor获取下一个
进入@country、@CountryExcl、@CountryDelivery、@country\u ID
结束
结清帐目
取消分配会计科目
设置@Query=@Query+)'
RETURN@Query
结束
试试这个:

declare @filter varchar(max);
set @filter = '(1 = 1)';

declare Accountring_Country1Cursor Cursor for select Country, Exclude, IsDelivery, ID from dbo.Accounting_Country1 where Service_ID = @Service_ID        
OPEN Accountring_Country1Cursor

FETCH NEXT FROM Accountring_Country1Cursor INTO @country, @CountryExcl, @CountryDelivery, @country_ID
WHILE @@FETCH_STATUS = 0
BEGIN
    set @filter = @filter + ' AND ('+ (case when(@CountryDelivery = 1) then 'Delivery'     else 'pickup' end) +'.CountryUK '+ (case(@CountryExcl) when 1 then ' <> ''' else ' = ''' end) + @country + ''')'    
    FETCH NEXT FROM Accountring_Country1Cursor INTO @country, @CountryExcl, @CountryDelivery,@country_ID
END 

CLOSE Accountring_Country1Cursor
DEALLOCATE Accountring_Country1Cursor 

SET @Query = @Query + '(' + @filter + ') '              
RETURN @Query
或者,稍微更复杂但更通用:

set @Query = substring(@Query, 0, 1) + substring(@Query, 4, len(@Query))

首先将where子句组合到一个不同的变量中,然后用值“(1=1)”初始化它,怎么样?然后您会得到“(1=1)和(someothercondition)”,这在语法上很好,无论如何查询计划优化器都应该删除它。您好!我不明白你想说什么。但是我忘了提到我的函数名是FunctionTest(),也许我可以在FunctionTest上做一些修剪来删除第一个单词?非常感谢您的回复!我会把它写进我的代码里,然后再给你回复!:-)请确保您确实需要以这种方式构建查询,并且正确验证输入数据——例如,如果您不验证@Country,这可能会被用来注入任何有害的SQL。如果您手动填写Accounting_Country1表,请忽略我(至少用户无法访问)。当我使用代码时,它不起作用,可能我会为您发布整个查询,问题可能是我使用嵌套的游标吗?这是完整的功能,因为你可以看到顶部的国家以和开头,底部关闭,因为它有嵌套的游标,我希望你能帮助@米什米什:您可能忘了将
@Filter
值添加到
@Query
。或者,您可能必须在第一个循环之前声明
@Filter
变量。你犯了什么样的错误?语法错误?还是问错了?
@Filter
@Query
中有什么内容?
set @Query = substring(@Query, 0, 1) + substring(@Query, 4, len(@Query))