sql 多条件组合查询并根据指定类别找出所有最小子类别的SQL语句备忘

2016-02-19 09:26 7 1 收藏

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享sql 多条件组合查询并根据指定类别找出所有最小子类别的SQL语句备忘教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - 编程语言 】

代码如下:

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
            --将指定类别的值的子类加入临时表
            INSERT INTO TBTEMCLASS(ID) SELECT ID FROM TBSDINFOCLASS WHERE ROOTID=@ROOTID

            --使用游标来将指定类别的最小类别提出放入临时表
            DECLARE CLASSID CURSOR FOR
            SELECT ID FROM TBTEMCLASS
            OPEN CLASSID
            FETCH NEXT FROM CLASSID INTO @ROOTID
            WHILE @@FETCH_STATUS=0
            BEGIN
                --如果判断有子类则将子类加入临时表,并删除该类别,以使游标在临时表中循环
                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

            --将自身加入临时表
            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

-- --计数语句
DECLARE @COUNTSQL NVARCHAR(500)
SELECT @COUNTSQL=N'SELECT @RECORDCOUNT=COUNT(*) FROM TBSDINFO INNER JOIN TBUSER ON TBSDINFO.USERNAME=TBUSER.USERNAME '
SELECT @COUNTSQL=@COUNTSQL+@WHERE
--
-- --执行统计
EXEC sp_executesql @COUNTSQL,
     N'@RECORDCOUNT INT OUT',
     @RECORDCOUNT OUT
--
-- --计算页数
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
--
-- --查询语句
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

--执行查询
--查询的结果是将找到的记录放入临时表,再通过以下游标查询出相应的父类与根类记录
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(@TEMROOTID0)
    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

来源:http://www.tulaoshi.com/n/20160219/1590797.html

延伸阅读
显示某个Sql Server某个数据库中所有表或视图的信息 sql server 2000 与 2005 不同 差别在于 红色字部分 以下语句为获取所有表信息,替换绿色黑体字"U"为"V"为获取所有视图信息。 Sql Server 2000 版本 SELECT Sysobjects.name AS TABLE_NAME, syscolumns.Id, syscolumns.name AS COLUMN_NAME, systypes.name AS DATA_TYPE, syscolumns.le...
SQL语句如下: 代码如下: WITH 表1 AS ( SELECT 编号字段名, ROW_NUMBER() OVER(ORDER BY 排序字段名 DESC) AS RowNum FROM 表名 ) Update 表1 SET 编号字段名=RowNum 应用场景: 通过这样的SQL语句根据小组人气值对小组进行排名: 代码如下: WITH groups AS ( SELECT RankNum, ROW_NUMBER() OVER(ORDER BY 人气值 DESC) A...
SQL的意思是结构化查询语言,其主要功能是同各种数据库建立联系,进行沟通.查询指的是对存储于SQL的数据的请求。查询要完成的任务是:将 Select 语句的结果集提供给用户。Select 语句从 SQL 中检索出数据,然后以一个或多个结果集的形式将其返回给用户。  ======================================================...
作用类别 本品为感冒类非处方药药品。 规格 每支装10毫升。 功能与主治 疏风解表,清热解毒。用于外感风热所致的感冒,症见发热、咳嗽、咽痛。 用法用量 口服。一次20毫升,一日3次;小儿酌减或遵医嘱。 ...
标签: ASP
有的时候取出全部数据库记录也许正好满足你的要求,不过,在大多数情况下我们通常只需得到部分记录。这样一来该如何设计查询呢?当然会更费点脑筋了,何况本文也存心不想让你去用那个什么劳什子的recordset。 举个例子,假如你只打算取出p_name 记录,而且这些记录的名字必须以字母w打头,那么你就要用到下面的WHERE 子句了: SQL ="SELECT p...

经验教程

67

收藏

92
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部