主頁(yè) > 知識(shí)庫(kù) > sql 多條件組合查詢,并根據(jù)指定類別找出所有最小子類別的SQL語(yǔ)句備忘

sql 多條件組合查詢,并根據(jù)指定類別找出所有最小子類別的SQL語(yǔ)句備忘

熱門(mén)標(biāo)簽:外呼系統(tǒng)改進(jìn) 知名電銷機(jī)器人價(jià)格 地圖標(biāo)注牌 廣東防封卡外呼系統(tǒng)原理是什么 電銷機(jī)器人公司 需要哪些牌照 分享百度地圖標(biāo)注多個(gè)位置 菏澤語(yǔ)音電銷機(jī)器人加盟公司 長(zhǎng)沙智能外呼系統(tǒng) 湖南電腦外呼系統(tǒng)平臺(tái)
復(fù)制代碼 代碼如下:

DECLARE @PAGESIZE INT
DECLARE @PAGEINDEX INT

DECLARE @PAGECOUNT INT
DECLARE @RECORDCOUNT INT

SELECT @PAGESIZE=5
SELECT @PAGEINDEX=1

DECLARE @FIELDNAME VARCHAR(50)
DECLARE @FIELDVALUE VARCHAR(50)
DECLARE @OPERATION VARCHAR(50)

--組合條件
DECLARE @WHERE NVARCHAR(1000)
SELECT @WHERE=' WHERE NOTDISPLAY=0 '

DECLARE ABC CURSOR FOR
SELECT FIELDNAME,FIELDVALUE,OPERATION FROM TBPARAMETERS
OPEN ABC
FETCH NEXT FROM ABC INTO @FIELDNAME,@FIELDVALUE,@OPERATION
WHILE @@FETCH_STATUS=0
BEGIN
    IF(@OPERATION = 'Like')
        SELECT @WHERE=@WHERE + ' AND ' + @FIELDNAME + ' Like ''%'+@FIELDVALUE+'%'''
    ELSE
    BEGIN
        IF(@FIELDNAME='CLASSID')
        BEGIN
            DECLARE @ROOTID INT
            SELECT @ROOTID=@FIELDVALUE
            --將指定類別的值的子類加入臨時(shí)表
            INSERT INTO TBTEMCLASS(ID) SELECT ID FROM TBSDINFOCLASS WHERE ROOTID=@ROOTID

            --使用游標(biāo)來(lái)將指定類別的最小類別提出放入臨時(shí)表
            DECLARE CLASSID CURSOR FOR
            SELECT ID FROM TBTEMCLASS
            OPEN CLASSID
            FETCH NEXT FROM CLASSID INTO @ROOTID
            WHILE @@FETCH_STATUS=0
            BEGIN
                --如果判斷有子類則將子類加入臨時(shí)表,并刪除該類別,以使游標(biāo)在臨時(shí)表中循環(huán)
                IF(EXISTS(SELECT ID FROM TBSDINFOCLASS WHERE ROOTID=@ROOTID))
                BEGIN
                    INSERT INTO TBTEMCLASS(ID) SELECT ID FROM TBSDINFOCLASS WHERE ROOTID=@ROOTID
                    DELETE FROM TBTEMCLASS WHERE ID=@ROOTID
                END
                FETCH NEXT FROM CLASSID INTO @ROOTID
            END
            CLOSE CLASSID
            DEALLOCATE CLASSID

            --將自身加入臨時(shí)表
            INSERT INTO TBTEMCLASS(ID) SELECT @FIELDVALUE

            SELECT @WHERE=@WHERE +' AND CLASSID IN(SELECT ID FROM TBTEMCLASS)'
        END
        ELSE
            SELECT @WHERE=@WHERE + ' AND ' + @FIELDNAME + @OPERATION+@FIELDVALUE
    END
    FETCH NEXT FROM ABC INTO @FIELDNAME,@FIELDVALUE,@OPERATION
END
CLOSE ABC
DEALLOCATE ABC

TRUNCATE TABLE TBPARAMETERS

-- --計(jì)數(shù)語(yǔ)句
DECLARE @COUNTSQL NVARCHAR(500)
SELECT @COUNTSQL=N'SELECT @RECORDCOUNT=COUNT(*) FROM TBSDINFO INNER JOIN TBUSER ON TBSDINFO.USERNAME=TBUSER.USERNAME '
SELECT @COUNTSQL=@COUNTSQL+@WHERE
--
-- --執(zhí)行統(tǒng)計(jì)
EXEC sp_executesql @COUNTSQL,
     N'@RECORDCOUNT INT OUT',
     @RECORDCOUNT OUT
--
-- --計(jì)算頁(yè)數(shù)
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
--
-- --查詢語(yǔ)句
DECLARE @SQL NVARCHAR(2000)
DECLARE @ORDERBY VARCHAR(100)
SELECT @ORDERBY=' ORDER BY TBSDINFO.IsCommon DESC,TBSDINFO.CommonTime DESC,TBSDINFO.CreateTime DESC'

IF(@PAGEINDEX=1)
BEGIN
    SELECT @SQL='INSERT INTO TBTEMINFO(ID,TITLE,REMARK,CREATETIME,ENDTIME,WEBDOMAIN,CLASSID,CLASSNAME,TYPEID,TYPENAME,PROVINCEID,PROVINCE,CITYID,CITY,COMPANYNAME,ADDRESS,USERTYPE) '
    SELECT @SQL=@SQL+'SELECT TOP '+CONVERT(VARCHAR(4),@PAGESIZE)+' TBSDINFO.ID,TITLE,Remark,TBSDINFO.Createtime,EndTime, WebDomain,TBSDINFO.CLASSID,(SELECT CLASSNAME FROM TBSDINFOCLASS WHERE TBSDINFOCLASS.ID=TBSDINFO.CLASSID) AS CLASSNAME,TYPEID,(SELECT TYPENAME FROM TBSDINFOTYPE WHERE TBSDINFO.TYPEID=TBSDINFOTYPE.ID) AS TYPENAME,ProvinceID,(SELECT PROVINCE FROM TBPROVINCE WHERE TBPROVINCE.ID=PROVINCEID) AS PROVINCE,CityID,(SELECT CITY FROM TBCITY WHERE TBCITY.ID=CITYID) AS CITY,CompanyName,TBSDINFO.Address,UserType FROM TBSDINFO INNER JOIN TBUSER ON TBUSER.USERNAME=TBSDINFO.USERNAME'
    SELECT @SQL=@SQL+@WHERE
    SELECT @SQL=@SQL+@ORDERBY
END
ELSE
BEGIN
    DECLARE @MINRECORD INT
    SELECT @MINRECORD=(@PAGEINDEX-1)*@PAGESIZE
    SELECT @SQL='INSERT INTO TBTEMINFO(ID,TITLE,REMARK,CREATETIME,ENDTIME,WEBDOMAIN,CLASSID,CLASSNAME,TYPEID,TYPENAME,PROVINCEID,PROVINCE,CITYID,CITY,COMPANYNAME,ADDRESS,USERTYPE) '
    SELECT @SQL=@SQL+'SELECT TOP '+CONVERT(VARCHAR(4),@PAGESIZE)+' TBSDINFO.ID, TITLE,Remark,TBSDINFO.Createtime,EndTime, WebDomain,TBSDINFO.CLASSID,(SELECT CLASSNAME FROM TBSDINFOCLASS WHERE TBSDINFOCLASS.ID=TBSDINFO.CLASSID) AS CLASSNAME,TYPEID,(SELECT TYPENAME FROM TBSDINFOTYPE WHERE TBSDINFO.TYPEID=TBSDINFOTYPE.ID) AS TYPENAME,ProvinceID,(SELECT PROVINCE FROM TBPROVINCE WHERE TBPROVINCE.ID=PROVINCEID) AS PROVINCE,CityID,(SELECT CITY FROM TBCITY WHERE TBCITY.ID=CITYID) AS CITY,CompanyName,TBSDINFO.Address,UserType FROM TBSDINFO INNER JOIN TBUSER ON TBUSER.USERNAME=TBSDINFO.USERNAME'
    IF(@WHERE>'')
        SELECT @SQL=@SQL+@WHERE+' AND '
    ELSE
        SELECT @SQL=@SQL+' WHERE '        
    SELECT @SQL=@SQL+' TBSDINFO.ID NOT IN(SELECT TOP '+CONVERT(VARCHAR(4),@MINRECORD)+' TBSDINFO.ID FROM TBSDINFO INNER JOIN TBUSER ON TBUSER.USERNAME=TBSDINFO.USERNAME '+@WHERE+@ORDERBY+')'

    SELECT @SQL=@SQL+@ORDERBY
END

--PRINT @SQL

--執(zhí)行查詢
--查詢的結(jié)果是將找到的記錄放入臨時(shí)表,再通過(guò)以下游標(biāo)查詢出相應(yīng)的父類與根類記錄
EXEC (@SQL)

DECLARE @CLASSID INT
DECLARE @ID INT

DECLARE TEM CURSOR FOR
SELECT ID,CLASSID FROM TBTEMINFO
OPEN TEM
FETCH NEXT FROM TEM INTO @ID,@CLASSID
WHILE @@FETCH_STATUS=0
BEGIN
    DECLARE @NS VARCHAR(500)
    DECLARE @DS VARCHAR(200)

    SELECT @NS=''
    SELECT @DS=''

    DECLARE @TEMROOTID INT

    DECLARE @TEMTS VARCHAR(50)
    SELECT @CLASSID=ID,@TEMTS=CLASSNAME,@TEMROOTID=ROOTID FROM TBSDINFOCLASS WHERE ID=@CLASSID
    SELECT @NS=@TEMTS+'#'+@NS
    SELECT @DS=CONVERT(VARCHAR(10),@CLASSID)+'#'+@DS

    WHILE(@TEMROOTID>0)
    BEGIN    
        SELECT @TEMROOTID=ROOTID,@CLASSID=ID,@TEMTS=CLASSNAME FROM TBSDINFOCLASS WHERE ID=@TEMROOTID
        SELECT @NS=@TEMTS+'#'+@NS
        SELECT @DS=CONVERT(VARCHAR(10),@CLASSID)+'#'+@DS
    END

    UPDATE TBTEMINFO SET NS=@NS,DS=@DS WHERE ID=@ID

    FETCH NEXT FROM TEM INTO @ID,@CLASSID
END
CLOSE TEM
DEALLOCATE TEM


SELECT * FROM TBTEMINFO

TRUNCATE TABLE TBTEMINFO
TRUNCATE TABLE TBTEMCLASS
您可能感興趣的文章:
  • SqlServer使用 case when 解決多條件模糊查詢問(wèn)題
  • SqlServer 巧妙解決多條件組合查詢
  • linq to sql 中,如何解決多條件查詢問(wèn)題,答案,用表達(dá)式樹(shù)! (下)
  • linq to sql中,如何解決多條件查詢問(wèn)題,答案,用表達(dá)式樹(shù)!
  • MySQL中使用case when 語(yǔ)句實(shí)現(xiàn)多條件查詢的方法
  • SQL 多條件查詢幾種實(shí)現(xiàn)方法詳細(xì)介紹

標(biāo)簽:商洛 福建 西寧 天水 泉州 美容院 呼和浩特 珠海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《sql 多條件組合查詢,并根據(jù)指定類別找出所有最小子類別的SQL語(yǔ)句備忘》,本文關(guān)鍵詞  sql,多,條件,組合,查詢,并,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sql 多條件組合查詢,并根據(jù)指定類別找出所有最小子類別的SQL語(yǔ)句備忘》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于sql 多條件組合查詢,并根據(jù)指定類別找出所有最小子類別的SQL語(yǔ)句備忘的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章