मुझे पता है कि यह बहुत देर हो चुकी है, लेकिन मेरे पास एक समान स्थिति थी। मेरे पास संग्रहीत प्रक्रियाओं के एक सेट के लिए "लाइक इन" ऑपरेटर की आवश्यकता है, जो कई मापदंडों को स्वीकार करते हैं और फिर कई आरडीबीएमएस सिस्टम से डेटा एकत्र करने के लिए उन मापदंडों का उपयोग करते हैं, इस प्रकार कोई आरडीबीएमएस-विशिष्ट चालें काम नहीं करेगी, हालांकि संग्रहीत कार्यविधि और कोई कार्य MS SQL सर्वर पर चलेगा, इसलिए हम प्रत्येक RDBMS के लिए पूर्ण SQL स्टेटमेंट बनाने की कार्यक्षमता के लिए T-SQL का उपयोग कर सकते हैं, लेकिन आउटपुट को काफी RDBMS- स्वतंत्र होना चाहिए।
यह वह समय है जब मैं एक सीमांकित स्ट्रिंग (जैसे कि एक पैरामीटर एक संग्रहीत प्रक्रिया में आने वाला पैरामीटर) को एसक्यूएल के ब्लॉक में बदल देता हूं। मैं इसे "LIKE IN" के लिए "लिचेन" कहता हूं। उसे ले लो?
Lichen.sql
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =======================================================================
-- Lichen - Scalar Valued Function
-- Returns nvarchar(512) of "LIKE IN" results. See further documentation.
-- CREATOR: Norman David Cooke
-- CREATED: 2020-02-05
-- UPDATED:
-- =======================================================================
CREATE OR ALTER FUNCTION Lichen
(
-- Add the parameters for the function here
@leadingAnd bit = 1,
@delimiter nchar(1) = ';',
@colIdentifier nvarchar(64),
@argString nvarchar(256)
)
RETURNS nvarchar(512)
AS
BEGIN
-- Declare the return variable here
DECLARE @result nvarchar(512)
-- set delimiter to detect (add more here to detect a delimiter if one isn't provided)
DECLARE @delimit nchar(1) = ';'
IF NOT @delimiter = @delimit
SET @delimit = @delimiter
-- check to see if we have any delimiters in the input pattern
IF CHARINDEX(@delimit, @argString) > 1 -- check for the like in delimiter
BEGIN -- begin 'like in' branch having found a delimiter
-- set up a table variable and string_split the provided pattern into it.
DECLARE @lichenTable TABLE ([id] [int] IDENTITY(1,1) NOT NULL, line NVARCHAR(32))
INSERT INTO @lichenTable SELECT * FROM STRING_SPLIT(@argString, ';')
-- setup loop iterators and determine how many rows were inserted into lichen table
DECLARE @loopCount int = 1
DECLARE @lineCount int
SELECT @lineCount = COUNT(*) from @lichenTable
-- select the temp table (to see whats inside for debug)
--select * from @lichenTable
-- BEGIN AND wrapper block for 'LIKE IN' if bit is set
IF @leadingAnd = 1
SET @result = ' AND ('
ELSE
SET @result = ' ('
-- loop through temp table to build multiple "LIKE 'x' OR" blocks inside the outer AND wrapper block
WHILE ((@loopCount IS NOT NULL) AND (@loopCount <= @lineCount))
BEGIN -- begin loop through @lichenTable
IF (@loopcount = 1) -- the first loop does not get the OR in front
SELECT @result = CONCAT(@result, ' ', @colIdentifier, ' LIKE ''', line, '''') FROM @lichenTable WHERE id = @loopCount
ELSE -- but all subsequent loops do
SELECT @result = CONCAT(@result, ' OR ', @colIdentifier, ' LIKE ''', line, '''') FROM @lichenTable WHERE id = @loopCount
SET @loopcount = @loopCount + 1 -- increment loop
END -- end loop through @lichenTable
-- set final parens after lichenTable loop
SET @result = CONCAT(@result, ' )')
END -- end 'like in' branch having found a delimiter
ELSE -- no delimiter was provided
BEGIN -- begin "no delimiter found" branch
IF @leadingAnd = 1
SET @result = CONCAT(' AND ', @colIdentifier, ' LIKE ''' + @argString + '''')
ELSE
SET @result = CONCAT(' ', @colIdentifier, ' LIKE ''' + @argString + '''')
END -- end "no delimiter found" branch
-- Return the result of the function
RETURN @result
END -- end lichen function
GO
सीमांकक का पता लगाने की योजना बनाई गई है, लेकिन अब यह अर्धविराम के लिए डिफॉल्ट करता है इसलिए आप बस default
वहां डाल सकते हैं । इसमें शायद कीड़े हैं। @leadingAnd
पैरामीटर तय करें कि आप ब्लॉक के सामने एक अग्रणी "और" पुट चाहते तो यह अन्य कहां खंड परिवर्धन के साथ अच्छी तरह से फिट बैठता है में बस थोड़ा मूल्य है।
उदाहरण उपयोग (argString में सीमांकक के साथ)
SELECT [dbo].[Lichen] (
default -- @leadingAND, bit, default: 1
,default -- @delimiter, nchar(1), default: ';'
,'foo.bar' -- @colIdentifier, nvarchar(64), this is the column identifier
,'01%;02%;%03%' -- @argString, nvarchar(256), this is the input string to parse "LIKE IN" from
)
GO
एक nvarchar (512) से युक्त होगा:
AND ( foo.bar LIKE '01%' OR foo.bar LIKE '02%' OR foo.bar LIKE '%03%' )
यदि इनपुट में सीमांकक नहीं है तो यह ब्लॉक को भी छोड़ देगा:
उदाहरण का उपयोग (बिना सीमांकक के)
SELECT [dbo].[Lichen] (
default -- @leadingAND, bit, default: 1
,default -- @delimiter, nchar(1), default: ';'
,'foo.bar' -- @colIdentifier, nvarchar(64), this is the column identifier
,'01%' -- @argString, nvarchar(256), this is the input string to parse "LIKE IN" from
)
GO
एक nvarchar (512) से युक्त होगा:
AND foo.bar LIKE '01%'
मैं इस पर काम जारी रखने जा रहा हूं, इसलिए यदि मैंने कुछ (स्पष्ट रूप से स्पष्ट या अन्यथा) अनदेखी की है, तो कृपया टिप्पणी करने या पहुंचने के लिए स्वतंत्र महसूस करें।