• SQL/T-SQL实例参考-2


    对多关联查询,查询多中的记录,但是返回一的结果集

    子查询语法

    --一对多关联查询,查询多中的记录,但是返回一的结果集
    SELECT C.*  FROM (
    SELECT A.BasicID
    FROM [tbiz_PuzzleBasic] A LEFT JOIN [tbiz_PuzzleWork] B ON B.BasicID = A.BasicID
    WHERE B.Position LIKE '%经理%'
    GROUP BY A.BasicID ) T LEFT JOIN [tbiz_PuzzleBasic] C ON T.BasicID = C.BasicID

    SqlBulkCopy

    //string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
    //SqlConnection conn = new SqlConnection(str);
    
    DataTable dtTdAnswerStorage = new DataTable();
    dtTdAnswerStorage.Columns.Add("QUESTIONID", typeof(int));
    dtTdAnswerStorage.Columns.Add("QUESTIONNAME", typeof(string));
    dtTdAnswerStorage.Columns.Add("SORTNUM", typeof(int));
    
    strQid = dt.Rows[0]["Qid"].ToString();
    foreach (OptionItem option in questionItem.OptionItems)
    {
        DataRow dr = dtTdAnswerStorage.NewRow();
        dr["QUESTIONID"] = strQid;
        dr["QUESTIONNAME"] = option.OptionTitle;
        dr["SORTNUM"] = option.SortNum;
        dtTdAnswerStorage.Rows.Add(dr);
    }
    
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
    {
        bulkCopy.BatchSize = 5000;
        bulkCopy.BulkCopyTimeout = 60;
        bulkCopy.DestinationTableName = "TD_ANSWER_STORAGE";
        bulkCopy.ColumnMappings.Add("QUESTIONID", "QUESTIONID");
        bulkCopy.ColumnMappings.Add("QUESTIONNAME", "QUESTIONNAME");
        bulkCopy.ColumnMappings.Add("SORTNUM", "SORTNUM");
        bulkCopy.WriteToServer(dtTdAnswerStorage);
    }

    INSERT FROM SELECT

    Insert Into TD_ANSWER(ANSWERID,QUESTIONID,SCALEID,QUESTIONNAME,SORTNUM) 
    Select ANSWERID,QUESTIONID,{0},QUESTIONNAME,SORTNUM From TD_ANSWER_STORAGE WHERE QUESTIONID ={1};

    UPDATE FROM SELECT

    例子1:子查询嵌套+自增序列

    UPDATE TD_QUESTION SET SORTNUM=M.I
    FROM
    (
    SELECT QUESTIONID,ROW_NUMBER() OVER(ORDER BY QUESTIONID ASC) As I FROM TD_QUESTION B
    WHERE SCALEID=21
    ) M
    WHERE SCALEID=21

    //所谓的链接子查询写法

    UPDATE TD_QUESTION SET SORTNUM=M.I
    FROM TD_QUESTION A INNER JOIN
    (
    SELECT QUESTIONID,ROW_NUMBER() OVER(ORDER BY QUESTIONID ASC) As I FROM TD_QUESTION B
    WHERE SCALEID=21
    ) M ON A.QUESTIONID= M.QUESTIONID
    WHERE SCALEID=21

     处理NULL相关函数

    isnull(check_expression , replacement_value )
    ISNULL( T.recommendCount, 0 ) AS recommendCount,
    CASE WHEN TBIZ_Contract.ContractID IS NULL

    Group By

    SELECT RoleID,ISNULL(Count(*),0) NUM FROM tbiz_UserInfo WHERE CompanyID=177 GROUP BY  RoleID HAVING RoleID=6

    SQL/T-SQL实例参考-1

    CASE

    SELECT [staff_no] 
    	  ,CASE WHEN ISNUMERIC([staff_no])=0 THEN 0 ELSE CONVERT(numeric(18,0),[staff_no]) END staff_no_num 
          ,[staff_name]
          ,[role]
          ,[dept_no]
          ,[dept_name]
          ,[team_no]
          ,[team_name]
      FROM [tStaff_temp] ORDER BY staff_no_num DESC 
    

      

  • 相关阅读:
    【转】jar参数运行应用时classpath的设置方法
    【转】命令行执行main方法
    【转】JAVA下的多线程程序造成系统时钟变快
    Spring MVC学习总结(6)——一些Spring MVC的使用技巧
    Spring MVC学习总结(6)——一些Spring MVC的使用技巧
    Maven学习总结(21)——Maven常用的几个核心概念
    专访李智慧:架构是最高层次的规划和难以改变的决定
    华为架构师8年经验谈:从单体架构到微服务的服务化演进之路
    华为架构师8年经验谈:从单体架构到微服务的服务化演进之路
    Spring学习总结(12)——Druid连接池及监控在spring配置
  • 原文地址:https://www.cnblogs.com/zhuji/p/8482024.html
Copyright © 2020-2023  润新知