在表上运行插入查询时从表中选择

时间:2019-07-14 16:04:54

标签: sql sql-server tsql sql-insert

我正在尝试运行一个存储过程,该过程从某些表中获取数据,并在运行另一个查询时向用户显示该数据,该查询将数据插入到我从中获取数据的同一表中。

最后,我从服务器获取数据的查询如下:

SELECT *
FROM
    (SELECT DISTINCT
         ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS row, 
         ManualJEListTempTable.* 
     FROM
         (SELECT 
              dbo.ManualJEHeader.JENumber,
              dbo.ManualJEHeader.JEType,
              dbo.JEType.Description as JETypeDescription,
              --dbo.ManualJELines.JELineNo,
              dbo.ManualJEHeader.CompanyId,
              dbo.Companies.CompanyName,
              dbo.ManualJEHeader.OrginalCurrency,
              --dbo.ManualJELines.DebitAmount,
              --dbo.ManualJELines.CreditAmount,
              (SELECT SUM(ISNULL(dbo.ManualJELines.DebitAmount, 0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalDebitAmount,
                  (select  sum(isnull(dbo.ManualJELines.CreditAmount,0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalCreditAmount,
                  (select  sum(isnull(dbo.ManualJELines.ConsolidationDebitAmount,0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalConDebitAmount,
                  (select  sum(isnull(dbo.ManualJELines.ConsolidationCreditAmount,0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalConCreditAmount,
                  (select  sum(isnull(dbo.ManualJELines.TransactionDebitAmount,0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalTranDebitAmount,
                  (select  sum(isnull(dbo.ManualJELines.TransactionCreditAmount,0)) from ManualJELines where (dbo.ManualJELines.statuscode not like 'CA' or  dbo.ManualJELines.statuscode is null) and companyId = dbo.ManualJEHeader.CompanyId and dbo.ManualJEHeader.JENumber = JENumber) as totalTranCreditAmount,
                 -- dbo.ManualJELines.ItemCode,
                  --dbo.ConsolidationItems.ItemName,
                  --dbo.ManualJELines.AccountNo,
                  ---ISNULL(dbo.Accounts.AccountName,'') as accountName,
                 -- dbo.ManualJELines.ContraItemCode,
                 -- ISNULL(contraItem.ItemName,'') as ContraItemName,
                --  dbo.ManualJELines.ContraAccountNo,
                 -- ISNULL(ContraAcountName.AccountName,'') as ContraAccountName,
                --  dbo.ManualJELines.ContraCompanyId,
                --  ISNULL(compName.CompanyName,'') as ContraCompanyName,
                    dbo.ManualJEHeader.JEReference,
                    dbo.ManualJEHeader.Details,
                  dbo.ManualJEHeader.BalanceDate,
                  dbo.ManualJEHeader.StatusCode,
                  dbo.JEStatus.Description as statusDescription,
                  dbo.ManualJEHeader.StornoDate
            from dbo.ManualJEHeader with(nowait)
            --inner join dbo.ManualJELines
            --on dbo.ManualJEHeader.CompanyId = dbo.ManualJELines.CompanyId and dbo.ManualJEHeader.JENumber = dbo.ManualJELines.JENumber
            inner join CompaniesGroupDetails with(nowait) on dbo.CompaniesGroupDetails.CompanyId = ManualJEHeader.CompanyId
            inner join CompaniesGroupHeader with(nowait) on dbo.CompaniesGroupHeader.GroupId = CompaniesGroupDetails.GroupId
            left join Companies with(nowait) on ManualJEHeader.CompanyId = Companies.CompanyId
            --left outer join Companies with(nowait) compName on ManualJELines.ContraCompanyId = compName.CompanyId
            --left join ConsolidationItems with(nowait) on dbo.ManualJELines.ItemCode = ConsolidationItems.ItemCode
            --left outer join ConsolidationItems with(nowait) contraItem on dbo.ManualJELines.ContraItemCode = contraItem.ItemCode
            left join JEStatus with(nowait) on ManualJEHeader.StatusCode=JEStatus.StatusCode
            --left join Accounts with(nowait) on ManualJELines.AccountNo=Accounts.AccountId and ManualJELines.CompanyId = Accounts.CompanyId
            --left outer join Accounts with(nowait) ContraAcountName on ManualJELines.ContraAccountNo = ContraAcountName.AccountId and ManualJELines.CompanyId = ContraAcountName.CompanyId
            left join JEType with(nowait) on ManualJEHeader.JEType=JEType.TypeCode where CompaniesGroupHeader.GroupId = '00' and (dbo.ManualJEHeader.StatusCode <> 'CA' or dbo.ManualJEHeader.StatusCode is null)) as ManualJEListTempTable ) as tempTable where Row between 0 and 300

当我运行此查询时,出现此错误

  

第22行,州51,第3行,1222消息
  超过了锁定请求超时期限。

有人知道为什么我在使用with(nowait)时遇到该错误吗?

3 个答案:

答案 0 :(得分:0)

查询运行多长时间?如果您不等待,则可能必须使用 查询开头的'SET LOCK_TIMEOUT timeout_period'选项。

SET LOCK_TIMEOUT 1400 (which is in 1400 milliseconds) 

您也可以看到这一点(此博客对如何使用set lock_timeout理解nowait进行了很好的解释)https://blog.sqlauthority.com/2013/01/28/sql-server-basic-explanation-of-set-lock_timeout-how-to-not-wait-on-locked-query/

答案 1 :(得分:-1)

添加

With(nolock) 

在每个联接或表之后,您就完成了。

例如

SELECT * FROM Person.Contact WITH (NOLOCK) WHERE ContactID < 20 

使用此工具时要小心。 read this on Microsoft

如果必须处理解决方案,还必须处理可能的问题,例如:

丢失的更新 当两个或多个事务选择同一行,然后根据最初选择的值更新该行时,将丢失更新。每笔交易都不知道其他交易。最后一次更新将覆盖其他事务所做的更新,从而导致数据丢失。

未提交的依赖关系(脏读) 当第二个事务选择另一个事务正在更新的行时,发生未提交的依赖关系。第二个事务正在读取尚未提交的数据,并且可以通过更新行来更改该数据。

分析不一致(不可重复读取) 当第二个事务多次访问同一行并每次读取不同的数据时,就会发生不一致的分析。不一致的分析类似于未提交的依赖性,因为另一个事务正在更改第二个事务正在读取的数据。

幻像阅读 对属于事务正在读取的行范围内的行执行插入或删除操作时,将发生幻像读取。

行更新导致的缺失和重复读取

答案 2 :(得分:-2)

我用with(nolock)重写查询并得到我想要的内容,

这是我的最终查询

SELECT *
FROM (SELECT DISTINCT
             ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS row,
             ManualJEListTempTable.*
      FROM (SELECT dbo.ManualJEHeader.JENumber,
                   dbo.ManualJEHeader.JEType,
                   dbo.JEType.Description AS JETypeDescription,
                   dbo.ManualJEHeader.CompanyId,
                   dbo.Companies.CompanyName,
                   dbo.ManualJEHeader.OrginalCurrency,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.DebitAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalDebitAmount,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.CreditAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalCreditAmount,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.ConsolidationDebitAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalConDebitAmount,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.ConsolidationCreditAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalConCreditAmount,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.TransactionDebitAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalTranDebitAmount,
                   (SELECT SUM(ISNULL(dbo.ManualJELines.TransactionCreditAmount, 0))
                    FROM ManualJELines WITH (NOLOCK)
                    WHERE (dbo.ManualJELines.statuscode NOT LIKE 'CA'
                        OR dbo.ManualJELines.statuscode IS NULL)
                      AND companyId = dbo.ManualJEHeader.CompanyId
                      AND dbo.ManualJEHeader.JENumber = JENumber) AS totalTranCreditAmount,
                   dbo.ManualJEHeader.JEReference,
                   dbo.ManualJEHeader.Details,
                   dbo.ManualJEHeader.BalanceDate,
                   dbo.ManualJEHeader.StatusCode,
                   dbo.JEStatus.DESCRIPTION AS statusDescription,
                   dbo.ManualJEHeader.StornoDate
            FROM dbo.ManualJEHeader WITH (NOLOCK)
                 INNER JOIN CompaniesGroupDetails WITH (NOLOCK) ON dbo.CompaniesGroupDetails.CompanyId = ManualJEHeader.CompanyId
                 INNER JOIN CompaniesGroupHeader WITH (NOLOCK) ON dbo.CompaniesGroupHeader.GroupId = CompaniesGroupDetails.GroupId
                 LEFT JOIN Companies WITH (NOLOCK) ON ManualJEHeader.CompanyId = Companies.CompanyId
                 LEFT JOIN JEStatus WITH (NOLOCK) ON ManualJEHeader.StatusCode = JEStatus.StatusCode
                 LEFT JOIN JEType WITH (NOLOCK) ON ManualJEHeader.JEType = JEType.TypeCode
            WHERE CompaniesGroupHeader.GroupId = '00'
              AND (dbo.ManualJEHeader.StatusCode <> 'CA'
                OR dbo.ManualJEHeader.StatusCode IS NULL)) ManualJEListTempTable ) tempTable
WHERE ROW BETWEEN 0 AND 300;

请注意,我在每个表之后都写了with(nolock)(当我没有这样做时,查询仍然处于锁定状态)