简体   繁体   中英

How can we use IN SQL Statement into SQL SERVER Function

How can we use IN Statement into sql server function.

Here is my code:

ALTER FUNCTION [dbo].[getMentalTable]
( 
@Code  VARCHAR(50),
@Statu VARCHAR(10),
@donem1 VARCHAR(10),
@donem2 VARCHAR(10),
@principal VARCHAR(10)
)
RETURNS TABLE
AS
RETURN
(
SELECT
                  m.Code,
                  m.CodeType,
                  k.TANIM,
                    kart.DonemAdi,
                    COUNT(DISTINCT m.DRGPatientId) AS VakaSayısı
                FROM
                    TIGTest.dbo.MentalBozuklukVerileriOzet m,
                    TIGTest.dbo.KART_DONEM kart,
                    TIGTest.dbo.Hospitals h,
                  KODLAR k
                WHERE m.Code IN(@Code)
                AND kart.DonemKodu = m.DonemKodu
                AND h.HospitalCode = m.HospitalCode
                AND h.Statu='D'
                AND m.DonemKodu BETWEEN @donem1 AND @donem2
                AND k.KOD2=m.Code
                AND m.IsPrincipal=@principal
                GROUP BY
                    kart.DonemAdi,
                    m.DonemKodu,
                  m.Code,
                  m.CodeType,
                  k.TANIM,
                    kart.DonemAdi
)

I want to set multiple parameters in @Code variable.So this using cant work.How can we use IN statement in sql server functions.

Thanks .

Try this(considering that @code will have CSV)..U need to convert your CSV into rows to compare with IN operator

....
WHERE m.Code IN (SELECT Split.a.value('.', 'VARCHAR(100)')
FROM   (SELECT Cast ('<M>' + Replace(@Code, ',', '</M><M>') + '</M>' AS XML) AS code) AS A
       CROSS APPLY code.nodes ('/M') AS Split(a))
AND...

One method is to use dynamic SQL. That is, embed the list of codes in a string.

Another is to use a CTE to split the list into a table and use a join or in .

A third method that is simpler to write, but cannot take advantage of an index on m.Code , is:

 WHERE ',' + @Code + ',' like '%,' + m.Code + ',%'

This uses like for this purpose.

Have you considered a table-valued parameter? Passing a list in a single parameter (delimited list or XML) will necessitate parsing on the server side and is less efficient than a TVP. Below is a TVP example.

CREATE TYPE CodesType AS TABLE(
    Code varchar(50) NOT NULL PRIMARY KEY
    );
GO

CREATE FUNCTION [dbo].[getMentalTable]
( 
@Codes dbo.CodesType READONLY,
@Statu VARCHAR(10),
@donem1 VARCHAR(10),
@donem2 VARCHAR(10),
@principal VARCHAR(10)
)
RETURNS TABLE
AS
RETURN
(
SELECT
                  m.Code,
                  m.CodeType,
                  k.TANIM,
                    kart.DonemAdi,
                    COUNT(DISTINCT m.DRGPatientId) AS VakaSayısı
                FROM
                    TIGTest.dbo.MentalBozuklukVerileriOzet m,
                    TIGTest.dbo.KART_DONEM kart,
                    TIGTest.dbo.Hospitals h,
                  KODLAR k
                WHERE m.Code IN(SELECT Code FROM @Codes)
                AND kart.DonemKodu = m.DonemKodu
                AND h.HospitalCode = m.HospitalCode
                AND h.Statu='D'
                AND m.DonemKodu BETWEEN @donem1 AND @donem2
                AND k.KOD2=m.Code
                AND m.IsPrincipal=@principal
                GROUP BY
                    kart.DonemAdi,
                    m.DonemKodu,
                  m.Code,
                  m.CodeType,
                  k.TANIM,
                    kart.DonemAdi
);
GO

DECLARE @Codes dbo.CodesType;
INSERT INTO @Codes VALUES('1'), ('2');
SELECT * 
FROM [dbo].[getMentalTable](@Codes,'a','b','c','d');
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM