SQL Server 查询中的过程

Posted

技术标签:

【中文标题】SQL Server 查询中的过程【英文标题】:Procedure in SQL Server query 【发布时间】:2016-08-06 05:44:57 【问题描述】:
INSERT INTO #tempbe_budget 
SELECT budgetno, 
       Isnull(finyear, '')                            AS Finyear, 
       Isnull(bud.deptid, 0)                          AS Deptid, 
       Isnull(bud.costcode, '')                       AS Costcode, 
       Isnull(secname, '')                            AS Secname, 
       Isnull(oldno, '')                              AS OldNo, 
       Isnull(newno, '')                              AS Newno, 
       Isnull(iaeacode, '')                           AS IAEA, 
       Isnull(bud.description, '')                    AS Description, 
       Isnull(supplier, 0)                            AS Supplier, 
       Isnull(bud.category, '')                       AS Category, 
       Isnull(subcategory, '')                        AS SubCategory, 
       Isnull(status, '')                             AS Status, 
       Isnull(bud.phyprg, 0)                          AS Phyprogress, 
       Isnull(phytarget, 0)                           AS PhyTarget, 
       Isnull(emp.empname, '')                        AS Engrname, 
       Isnull(bud.empid, 0)                           AS Empid, 
       Isnull(refno, '')                              AS RefNo, 
       Isnull(CONVERT(DATETIME, issueddate, 103), '') AS Issueddate, 
       Isnull(cost, 0)                                AS Cost, 
       Isnull(CONVERT(DATETIME, deldate, 103), '')    AS Deldate, 
       Isnull(budgettype, '')                         AS BudgetType, 
       Isnull(reapr, 0)                               AS Reapr, 
       Isnull(remay, 0)                               AS Remay, 
       Isnull(rejun, 0)                               AS Rejun, 
       Isnull(rejul, 0)                               AS Rejul, 
       Isnull(reaug, 0)                               AS Reaug, 
       Isnull(resep, 0)                               AS Resep, 
       Isnull(reoct, 0)                               AS Reoct, 
       Isnull(renov, 0)                               AS Renov, 
       Isnull(redec, 0)                               AS Redec, 
       Isnull(rejan, 0)                               AS Rejan, 
       Isnull(refeb, 0)                               AS Refeb, 
       Isnull(remar, 0)                               AS Remar, 
       Isnull(recurfy, 0)                             AS Recurfy, 
       Isnull(committedcost, 0)                       AS Committedcost, 
       Isnull(orgdept, '')                            AS OrgDept, 
       Isnull(bud.remarks, '')                        AS Remarks, 
       Isnull(dept.deptname, '')                      AS Department, 
       bepobcno, 
       bepopono, 
       bepopodate, 
       bepopaiduptomarch04, 
       bepocurfinyrexp, 
       bepopovalue, 
       bewobcno, 
       bewowono, 
       bewowodate, 
       bewopaiduptomarch04, 
       bewocurfinyrexp, 
       bewowovalue, 
       bewofinalrevisionvalue, 
       bewowoenddate, 
       tpo.beposname, 
       two.bewosname, 
       Isnull(two.bewoactexp0102, 0)                  BEWOActExp0102, 
       Isnull(two.bewoactexp0203, 0)                  BEWOActExp0203, 
       Isnull(two.bewoactexp0304, 0)                  BEWOActExp0304, 
       Isnull(two.bewoactexp0405, 0)                  BEWOActExp0405, 
       Isnull(two.bewoactexp0506, 0)                  BEWOActExp0506, 
       Isnull(two.bewoactexp0607, 0)                  BEWOActExp0607, 
       Isnull(two.bewoactexp0708, 0)                  BEWOActExp0708, 
       Isnull(two.bewoactexp0809, 0)                  BEWOActExp0809, 
       Isnull(two.bewoactexp0910, 0)                  BEWOActExp0910, 
       Isnull(two.bewoactexp1011, 0)                  BEWOActExp1011, 
       Isnull(two.bewoactexp1112, 0)                  BEWOActExp1112, 
       Isnull(two.bewoactexp1213, 0)                  BEWOActExp1213, 
       Isnull(two.bewoactexp1314, 0)                  BEWOActExp1314, 
       Isnull(two.bewoactexp1415, 0)                  BEWOActExp1415 
   FROM   
       budget bud 
   LEFT OUTER JOIN 
       womaster wo ON wo.bcno1516 = bud.newno 
   LEFT OUTER JOIN 
       department dept ON dept.deptid = bud.deptid 
   LEFT OUTER JOIN 
       #tempbewo two ON two.bewobcno = bud.newno 
   LEFT OUTER JOIN 
       #tempbepo tpo ON tpo .bepobcno = bud.newno 
   LEFT OUTER JOIN 
       employee emp ON emp.empid = bud.empid 
   WHERE  
       budgettype = 'BE' 
       AND finyear = '2016-17' 
   ORDER BY 
       dept.dept_group; 

运行此查询会导致错误:

消息 468,第 16 级,状态 9,第 3 行 无法解决等于操作中“SQL_Latin1_General_CP1_CI_AS”和“Latin1_General_CI_AI”之间的排序规则冲突。

消息 213,第 16 级,状态 1,第 3 行 列名或提供的值的数量与表定义不匹配。

【问题讨论】:

sql server 2014 不工作 嗯,是的。 SQL Server 2014 工作正常。每天都有成千上万(如果不是数百万人)的人使用它,包括我办公室的人。 确保连接条件中使用的columns 具有相同的排序规则。例如wo.bcno1516 = bud.newno 这里wo.bcno1516bud.newno 应该有相似的排序规则。如果wo.bcno1516 属于SQL_Latin1_General_CP1_CI_AS 排序规则,而bud.newno 属于Latin1_General_CI_AI,那么您将收到此错误。 第二个错误不言自明 【参考方案1】:

    您最好在表 #tempbe_budget 之后指定所有列,如下所示:

    插入 #tempbe_budget( 预算没有, 财政年度, 迪普蒂德, ...... )

    为您的 JOINS 添加排序规则调整:

    来自预算芽 左外连接womaster wo ON wo.bcno1516 COLLATE SQL_Latin1_General_CP1_CI_AS = bud.newno COLLATE SQL_Latin1_General_CP1_CI_AS LEFT OUTER JOIN部门 ON dept.deptid COLLATE SQL_Latin1_General_CP1_CI_AS = bud.deptid COLLATE SQL_Latin1_General_CP1_CI_AS 左外连接#tempbewo 2 ON two.bewobcno COLLATE SQL_Latin1_General_CP1_CI_AS = bud.newno COLLATE SQL_Latin1_General_CP1_CI_AS 左外连接#tempbepo tpo ON tpo .bepobcno COLLATE SQL_Latin1_General_CP1_CI_AS = bud.newno COLLATE SQL_Latin1_General_CP1_CI_AS LEFT OUTER JOIN 员工 emp ON emp.empid COLLATE SQL_Latin1_General_CP1_CI_AS = bud.empid COLLATE SQL_Latin1_General_CP1_CI_AS

我不确定是哪一个导致了错误,你可以尝试一下,但我猜你的数据库排序规则与服务器排序规则不同,并且只影响到临时表的连接。

    根据您的列名,我看到它们可以是 INT 类型,而不是 CHAR/VARCHAR。强烈建议使用适当的数据类型。

【讨论】:

以上是关于SQL Server 查询中的过程的主要内容,如果未能解决你的问题,请参考以下文章

了解 SQL Server 2008 R2 中的索引视图更新和查询过程

如何从 SQL Server Express 中的动态查询创建 RDLC 报告

WHERE 子句中的 OR 会降低 sql 查询性能(sql server)

sql server更新查询中的if条件

SQL Server查询优化与事务处理

Sql Server 活动监视器中的这个过程是啥?