我编写了一个用于保存和更新数据的存储过程(列表)。存储过程更新(任何额外数据)重复数据和任何新数据被添加到数据库,即检查数据是否已存在于数据库中。
程序很简单,效果很好,但问题是我得到了上述错误:
无法添加Timeout已过期。超时时间过去之前 完成操作或服务器没有响应
我无法添加数据,但过了一段时间后,错误就会解决,数据会被保存和更新。 什么是确切的问题? 我是否需要在sql server管理中更改查询执行时间,或者必须优化查询?
我的程序如下:
ALTER PROCEDURE [dbo].[InsertManuallyUsingXmlExhibitorList]
@XML_TRANSACTIONDETAILS nText,
@exhibitor nvarchar(100)
AS
DECLARE @docHandle int
SET NOCOUNT ON;
DECLARE @exbitionID nvarchar(100)
EXEC sp_xml_preparedocument @docHandle OUTPUT, @XML_TRANSACTIONDETAILS
-- CREATE TEMP TABLE
CREATE TABLE #TempTable
(
maxid nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
companyname nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
email nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
website nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
country bigint NULL,
contactperson nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
telphone nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
mobile nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
fax nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
region bigint NULL,
status bigint NULL,
username bigint NULL,
date datetime NULL,
category bigint NULL,
urlorcatalog nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
address nvarchar(200) Collate SQL_Latin1_General_CP1_CI_AS NULL,
lastupdatedby bigint NULL
)
-- INSERT INTO TEMP TABLE DETAILS OF EMAIL
INSERT INTO #TempTable
(
maxid,companyname,email,website,country,contactperson,telphone,mobile,fax,region,status,username,date,category,urlorcatalog,address,lastupdatedby
)
SELECT
data.maxid,data.companyname,data.email,data.website,data.country,data.contactperson,data.telphone,data.mobile,data.fax,data.region,data.status,data.username,GETDATE(),data.category,data.urlorcatalog,data.address,data.username
FROM OPENXML(@docHandle,N'master/TRANSACTION',3)
WITH
(
companyname nvarchar(200),
website nvarchar(200),
country bigint ,
contactperson nvarchar(200),
telphone nvarchar(200),
mobile nvarchar(200),
fax nvarchar(200),
region bigint,
status bigint,
username bigint,
category bigint,
urlorcatalog nvarchar(200),
email nvarchar(200),
maxid nvarchar(200),
address nvarchar(200)
) as data
-- INSERT INTO TEMP TABLE DETAILS OF EMAIL1
INSERT INTO #TempTable
(
maxid,companyname,email,website,country,contactperson,telphone,mobile,fax,region,status,username,date,category,urlorcatalog,address,lastupdatedby
)
SELECT
data.maxid,data.companyname,data.email1,data.website,data.country,data.contactperson,data.telphone,data.mobile,data.fax,data.region,data.status,data.username,GETDATE(),data.category,data.urlorcatalog,data.address,data.username
FROM OPENXML(@docHandle,N'master/TRANSACTION',3)
WITH
(
companyname nvarchar(200),
website nvarchar(200),
country bigint ,
contactperson nvarchar(200),
telphone nvarchar(200),
mobile nvarchar(200),
fax nvarchar(200),
region bigint,
status bigint,
username bigint,
category bigint,
urlorcatalog nvarchar(200),
email1 nvarchar(200),
maxid nvarchar(200),
dataentrytype nvarchar(200),
lastupdatedby nvarchar(200),
address nvarchar(200)
) as data
WHERE data.email1 !=''
-- INSERT INTO TEMP TABLE DETAILS OF EMAIL2
INSERT INTO #TempTable
(
maxid,companyname,email,website,country,contactperson,telphone,mobile,fax,region,status,username,date,category,urlorcatalog,address,lastupdatedby
)
SELECT
data.maxid,data.companyname,data.email2,data.website,data.country,data.contactperson,data.telphone,data.mobile,data.fax,data.region,data.status,data.username,GETDATE(),data.category,data.urlorcatalog,data.address,data.username
FROM OPENXML(@docHandle,N'master/TRANSACTION',3)
WITH
(
companyname nvarchar(200),
website nvarchar(200),
country bigint ,
contactperson nvarchar(200),
telphone nvarchar(200),
mobile nvarchar(200),
fax nvarchar(200),
region bigint,
status bigint,
username bigint,
category bigint,
urlorcatalog nvarchar(200),
email2 nvarchar(200),
maxid nvarchar(200),
address nvarchar(200)
) as data
WHERE data.email2 !=''
--**********************UPDATE TEMP TABLE IF COUNTRY AND REGION IS NULL********************
UPDATE #TempTable
SET
country=1
WHERE country=0
UPDATE #TempTable
SET
region=2
WHERE region=0
-- CODE TO COUNT DUPLICATES EMAILS
SELECT
COUNT
( email)
FROM
#TempTable T
WHERE
T.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=T.email)
-- TRANSACTION BEGINS HERE
BEGIN TRANSACTION
---------------------- EXHIBITOR LIST ------------------------------------
IF EXISTS ( SELECT *
FROM exibitions
WHERE exhibitiorlist=@exhibitor )
BEGIN
SELECT @exbitionID = EID FROM exibitions WHERE exhibitiorlist=@exhibitor
end
ELSE
BEGIN
INSERT INTO exibitions VALUES (@exhibitor)
SET @exbitionID = (SELECT SCOPE_IDENTITY())
END
------------- UPADATE EXHIBITOR LIST -------------------------------------
--DECLARE @RtnStr VARCHAR(MAX)
UPDATE dbo.companyinfo
SET companyinfo.exblist=RESULT.m2
FROM
(
select TEMP1.DataID,COALESCE(COMPANY.exblist + ',', '') + @exbitionID as 'm2'
from
(
SELECT Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
LEFT OUTER JOIN emailinfo Email ON tmp.email =Email.email
WHERE
tmp.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
)AS TEMP1
LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID =COMPANY.dataId
WHERE
TEMP1.RowNumber =1
) AS RESULT
WHERE companyinfo.dataId =RESULT.DataID
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
------------- UPDATE DATE IF EMAIL ALREADY EXISTED ------------------------
UPDATE dbo.companyinfo
SET companyinfo.lastupdateddate=RESULT.date
FROM
(
select TEMP1.date,TEMP1.DataID
from
(
SELECT tmp.date,Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
LEFT OUTER JOIN emailinfo Email ON tmp.email =Email.email
WHERE
tmp.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
)AS TEMP1
LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID =COMPANY.dataId
WHERE
TEMP1.RowNumber =1
) AS RESULT
WHERE companyinfo.dataId =RESULT.DataID
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
-- ******************* UPDATE CATEGORY IF EMAIL ALREADY EXISTED ***********************
UPDATE dbo.companyinfo
SET companyinfo.category=case when companyinfo.status!=1 and companyinfo.status!=2 and companyinfo.status!=13 and companyinfo.status!=17 then RESULT.category else companyinfo.category end
FROM
(
select TEMP1.category,TEMP1.status,TEMP1.DataID
from
(
SELECT tmp.category,tmp.status,Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
LEFT OUTER JOIN emailinfo Email ON tmp.email =Email.email
WHERE
tmp.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
)AS TEMP1
LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID =COMPANY.dataId
WHERE
TEMP1.RowNumber =1
) AS RESULT
WHERE companyinfo.dataId =RESULT.DataID
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
-- ******************* UPDATE COMPANY IF EMAIL ALREADY EXISTED ***********************
UPDATE dbo.companyinfo SET companyinfo.companyname=case when companyinfo.companyname='' or companyinfo.companyname=null then RESULT.companyname else companyinfo.companyname end ,
companyinfo.website= case when companyinfo.website='' OR companyinfo.website IS NULL then RESULT.website else companyinfo.website end ,
companyinfo.contactperson= case when companyinfo.contactperson='' OR companyinfo.contactperson IS NULL then RESULT.contactperson else companyinfo.contactperson end,companyinfo.country = case when companyinfo.country=1 OR companyinfo.country IS NULL then RESULT.country else companyinfo.country end,
companyinfo.telphone=case when companyinfo.telphone='' OR companyinfo.telphone IS NULL then RESULT.telphone else companyinfo.telphone end,companyinfo.mobile= case when companyinfo.mobile='' OR companyinfo.mobile IS NULL then RESULT.mobile else companyinfo.mobile end ,
companyinfo.fax= case when companyinfo.fax='' OR companyinfo.fax IS NULL then RESULT.fax else companyinfo.fax end, companyinfo.region= case when companyinfo.region=2 OR companyinfo.region IS NULL then RESULT.region else companyinfo.region end,companyinfo.address= case when companyinfo.address='' OR companyinfo.address IS NULL then RESULT.address else companyinfo.address end,
companyinfo.lastupdatedby=RESULT.lastupdatedby
FROM
(
select TEMP1.companyname,TEMP1.website,TEMP1.contactperson,TEMP1.country, TEMP1.telphone , TEMP1.mobile, TEMP1.fax,TEMP1.region, TEMP1.urlorcatalog,TEMP1.address,TEMP1.lastupdatedby, TEMP1.DataID
from
(
SELECT tmp.companyname,tmp.website,tmp.contactperson,tmp.country,tmp.telphone,tmp.mobile,tmp.fax, tmp.region,tmp.urlorcatalog,tmp.address,tmp.lastupdatedby,Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
LEFT OUTER JOIN emailinfo Email ON tmp.email =Email.email
WHERE
tmp.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
)AS TEMP1
LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID =COMPANY.dataId
WHERE
TEMP1.RowNumber =1
) AS RESULT
WHERE companyinfo.dataId =RESULT.DataID
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
-- ******************** UPDATE STATUS IF EMAIL ALREADY EXISTED ******************
UPDATE dbo.companyinfo
SET companyinfo.status=RESULT.status
FROM
(
select TEMP1.status,TEMP1.DataID
from
(
SELECT tmp.email,tmp.status,Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
LEFT OUTER JOIN dbo.emailinfo Email ON tmp.email=Email.email
WHERE
tmp.email !=''
AND
EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
)AS TEMP1
LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID=COMPANY.dataID
where --cmp.status !=8 And
TEMP1.RowNumber=1 AND
COMPANY.status !=1 AND
COMPANY.status < 6 AND
TEMP1.status < COMPANY.status
) AS RESULT
WHERE companyinfo.dataID=RESULT.DataID
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
-- ************INSERT INTO PRIMARY TABLE(companyinfo)*************
INSERT INTO dbo.companyinfo
(
dataId,companyname,website,country,contactperson,telphone,mobile,fax,region,status,userid,date,category,urlorcatalog,dataentrytype,address,exblist
)
SELECT
T1.maxid,T1.companyname,T1.website,T1.country,T1.contactperson,T1.telphone,T1.mobile,T1.fax,T1.region,T1.status,T1.username,T1.date,T1.category,T1.urlorcatalog,'MANUAL',T1.address,@exbitionID
FROM
(
SELECT tmpdata.*, ROW_NUMBER() OVER (PARTITION BY maxid ORDER BY maxid ) AS 'RowNumber'
FROM
(
SELECT
*
FROM
(
SELECT tmp.*, ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber1'
FROM #TempTable tmp
-- LEFT OUTER JOIN emailinfo Email ON tmp.email=Email.email
)AS T
WHERE T.RowNumber1=1
AND T.email !=''
AND
NOT EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=T.email)
)tmpdata
)AS T1
WHERE
T1.email !='' AND
T1.RowNumber=1
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
-- ************INSERT INTO SECONDARY TABLE*************
INSERT INTO emailinfo
(
DataID,email
)
SELECT
T.maxid,T.email
FROM
(
SELECT tmp.maxid,tmp.email, ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
FROM #TempTable tmp
--LEFT OUTER JOIN emailinfo Email ON tmp.email=Email.email
)AS T
WHERE T.RowNumber=1
AND T.email !=''
AND
NOT EXISTS (SELECT emailinfo.email FROM dbo.emailinfo WHERE email=T.email)
IF @@ERROR<>0 BEGIN ROLLBACK TRANSACTION RETURN -101 END
COMMIT TRANSACTION
DROP TABLE #TempTable
EXEC sp_xml_removedocument @docHandle
即使是4-5次更新/插入的极少数条目,错误仍然存在。
答案 0 :(得分:0)
当您尝试使用Microsoft SQL Server Management Studio中的表设计器修改大型表时,您可能会收到与以下内容类似的错误消息:
无法修改表格。超时已过期。超时时间已过 在完成操作之前或服务器没有响应。
<强> RESOLUTION 强>:
要解决此问题,请使用以下方法之一:
参考文献:
答案 1 :(得分:0)