TSQL程序问题和建议

时间:2016-07-11 16:42:25

标签: sql-server tsql procedural

我正在寻找有关实现以下目标的最佳方法的建议

我在SQL Server中有一个表,用于保存来自外部系统的下载数据。我需要用它来更新另一个数据库。一些记录将是插入,其他记录将是更新。有一个注释表和一个要插入/更新的主表。注释通过在注释表中创建的ID链接,并存储在主表记录的列中。 (一对一的关系)

因此插入注释表并获取scope_identity返回值,然后将其用作主表的insert语句的一部分。

更新从主表中的记录中获取注释ID,然后在必要时更新注释表,并在必要时更新主表

EG表有5条记录

Get first record
If exists in database
get commentID column from comment table and update comment and main table
If not exists
insert into comment table and return comment ID and insert the record into the main table with that comment ID
get the next record

我正在努力弄清楚如何在SQL Server中做到最好。找不到正确的游标组合,while循环,存储过程等。在SQL Server中通过程序工作没有做太多。

非常感谢任何建议/帮助

由于

哈博。我很感激反馈。我很难写出一个简明扼要的问题。链接页面提供了很好的建议。希望下面这个脚本有助于澄清。

再次感谢。

USE TEMPDB

--TABLE TO HOLD JOB RECORDS
create table tbl_jobs(
jobnumber varchar(16) primary key clustered,
jobdesc varchar(50),
commentID int
)
GO

INSERT INTO tbl_jobs VALUES ('Job1','Desc1', '1')
INSERT INTO tbl_jobs VALUES ('Job2','Desc2', '2') 
INSERT INTO tbl_jobs VALUES ('Job3','Desc3', '3') 

--TABLE TO HOLD JOB RECORD COMMENTS
create table tbl_jobComments(
commentID INT IDENTITY(1,1) NOT NULL,
comment text
)
GO
Insert into tbl_jobComments VALUES ('Comment1')
Insert into tbl_jobComments VALUES ('Comment2')
Insert into tbl_jobComments VALUES ('Comment3')


--TABLE TO HOLD RECORDS DOWNLOADED FROM EXTERNAL SYSTEM
create table tbl_updates(
jobnumber varchar(16) primary key clustered,
jobdesc varchar(50),
comment text
)
GO

INSERT INTO tbl_updates VALUES ('Job1','Desc1Modified', 'Comment1')
INSERT INTO tbl_updates VALUES ('Job2','Desc2', 'Comment2') 
INSERT INTO tbl_updates VALUES ('Job3','Desc3Modified', 'Comment3')  
INSERT INTO tbl_updates VALUES ('Job4','Desc4', 'Comment4') 
GO

--OUTPUT FROM tbl_Jobs
+-----------+---------+-----------+
| jobnumber | jobdesc | commentID |
+-----------+---------+-----------+
| Job1      | Desc1   |         1 |
| Job2      | Desc2   |         2 |
| Job3      | Desc3   |         3 |
+-----------+---------+-----------+

--OUTPUT FROM tbl_JobComments
+-----------+----------+
| commentID | comment  |
+-----------+----------+
|         1 | Comment1 |
|         2 | Comment2 |
|         3 | Comment3 |
+-----------+----------+

--OUTPUT FROM tbl_updates
+-----------+---------------+-----------+
| jobnumber |    jobdesc    | comment   |
+-----------+---------------+-----------+
| Job1      | Desc1Modified | Comment1  |
| Job2      | Desc2         | Comment2a |
| Job3      | Desc3Modified | Comment3  |
| Job4      | Desc4         | Comment4  |
+-----------+---------------+-----------+


--DESIRED RESULTS tbl_jobs
+-----------+-----------------+-----------+
| jobnumber | jobdesc         | commentID |
+-----------+-----------------+-----------+
| Job1      | Desc1Modified   |         1 |
| Job2      | Desc2           |         2 |
| Job3      | Desc3Modified   |         3 |
| Job4      | Desc4           |         4 |
+-----------+---------+-------------------+

--DESIRED RESULTS tbl_jobs_comments
+-----------+-----------+
| commentID | comment   |
+-----------+-----------+
|         1 | Comment1  |
|         2 | Comment2a |
|         3 | Comment3  |
|         4 | Comment4  |
+-----------+-----------+

1 个答案:

答案 0 :(得分:0)

You can break this into 2 statements, an update and an insert query

(This assumes there is only 1 comment per ID)

UPDATE maintable
SET Comment=upd.comment
FROM maintable mt
JOIN updatestable upd
ON mt.id=upd.id

then insert what is missing:

INSERT INTO maintable (id,comment)
SELECT id, comment
FROM updatestable
WHERE id NOT IN (SELECT id FROM maintable)