当前位置:  数据库>sqlserver

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

    来源: 互联网  发布时间:2014-09-05

    本文导语:  代码如下: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...

代码如下:

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(@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

    
 
 

您可能感兴趣的文章:

  • SQL的小常识, 备忘之用, 慢慢补充.
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • mysql中查询当前正在运行的SQL语句并找出mysql中运行慢的sql语句
  • oracle导出sql语句的结果集和保存执行的sql语句(深入分析)
  • Mysql增加主键或者更改表的列为主键的sql语句
  • sql语句实例 取得日志条目的sql语句
  • Mysql下在某一列后即表的某一位置添加新列的sql语句
  • SQL Server统计SQL语句执行时间的脚本
  • sql语句实例 统计页面链接的sql语句
  • 如何实现连接一次数据库,提交多个sql语句。(sql的批处理)
  • 一条SQL语句搞定Sql2000 分页
  • C#中验证sql语句是否正确(不执行语句)
  • SQL Server数据库的修复SQL语句
  • SQL 语句拦截 P6SPY
  • C#使用带like的sql语句时防sql注入的方法
  • t-sql/mssql用命令行导入数据脚本的SQL语句示例
  • 在SQL Server的try...catch语句中获取错误消息代码的的语句
  • sql2005创建远程登录帐户的sql语句分享
  • SQL SERVER 查询正在实行的SQL语句
  • SQL语句中含有乘号报错的处理办法
  • sql无效字符引起的执行sql语句报错的解决方法
  • C#代码验证sql语句是否正确(只验证不执行sql)的方法
  • SQL Server中选出指定范围行的SQL语句代码
  • java命名空间java.sql接口statement的类成员方法: executeupdate定义及介绍
  • 请问,这是什么错误!java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][Named Pipes]??????? SQL Server?虽然分少,但一定给,只要您是前5名回复者中最好的以为!
  • java命名空间java.sql接口connection的类成员方法: nativesql定义及介绍
  • SQL查询分析工具 SQL Workbench/J
  • java命名空间java.sql接口preparedstatement的类成员方法: executeupdate定义及介绍
  • SQL客户端软件 PKLite SQL Client
  • java命名空间java.sql接口rowid的类成员方法: getbytes定义及介绍
  • SQL语句实现SQL Server 2000及Sql Server 2005日志收缩(批量)
  • java命名空间java.sql接口ref的类成员方法: getbasetypename定义及介绍
  • SQL客户端管理工具 SQuirreL SQL Client


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3