簡體   English   中英

Like的SQL字符串數組參數

[英]SQL String array parameter for Like

我有一個MS SQL存儲過程,我需要將其傳遞給一個字符串數組以供LIKE運算符使用。 由於沒有內置的數據類型可以實現此目標,因此我如何最好地實現這一目標? 我已經看到了通過定界符傳遞varchar的實現,並且SQL腳本中的邏輯對此進行了拆分並動態地構建了語句。

這些實現對我而言似乎都不是干凈的,如何最好地實現?

謝謝

DECLARE @ContractId int
DECLARE @QuantityPerVehicle decimal(18, 4)
DECLARE @OrderQuantity decimal(18, 4)
DECLARE @OrderDelivered decimal(18, 4)
DECLARE @Description varchar(200)

DECLARE @PendingUsage decimal(18, 4) = 0

DECLARE db_cursor CURSOR FOR select ContractId, QuantityPerVehicle, OrderQuantity, OrderDelivered, Description FROM prod.BillOfMaterialLineDetails where ([description] like '%line item 1%' OR [description] like '%line item 2%' OR [description] like '%line item 3%') and ContractId is not null
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @ContractId, @QuantityPerVehicle, @OrderQuantity, @OrderDelivered, @Description

    WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE @JobsRemaining int
        SELECT @JobsRemaining = COUNT(JobId) FROM prod.JobDetails where JobStarted is null and ContractId = @ContractId
        SET @PendingUsage = @PendingUsage + (@JobsRemaining * @QuantityPerVehicle)

        FETCH NEXT FROM db_cursor INTO @ContractId, @QuantityPerVehicle, @OrderQuantity, @OrderDelivered, @Description
    END

CLOSE db_cursor
DEALLOCATE db_cursor


select @PendingUsage as PendingUsage

這對我來說很干凈

SELECT contractid, 
       quantitypervehicle, 
       orderquantity, 
       orderdelivered, 
       description 
FROM   prod.billofmateriallinedetails B 
       JOIN (VALUES ('%line item 1%'), 
                    ('%line item 2%'), 
                    ('%line item 3%')) tc(line_item) 
         ON B.description LIKE tc.line_item 
WHERE  contractid IS NOT NULL 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM