[英]How to insert data in multiple tables using single query in SQL Server?
I'm trying to insert data into multiple tables if it doesn't already exist.如果数据尚不存在,我正在尝试将数据插入到多个表中。 I can't seem to figure this out at all.
我似乎根本无法弄清楚这一点。
Table 1:表格1:
CREATE TABLE [dbo].[search_results]
(
[company_id] [int] NULL,
[title] [text] NULL,
[link] [text] NULL,
[domain] [text] NULL,
[index] [int] NULL,
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL
)
Table 2:表 2:
CREATE TABLE [dbo].[statements]
(
[statement_link_id] [int] NULL,
[statement_page] [text] NULL,
[statement_text_location] [text] NULL,
[statement_description] [text] NULL,
[statement_description_html] [text] NULL,
[statement] [int] NULL,
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL
)
This is what I want to do:这就是我想要做的:
check to see if the company_id
and the link already exist in the table or not.检查表中是否已存在
company_id
和链接。
SELECT *
FROM search_results
WHERE company_id = 4 AND link = 'https://test.com';
If the data does not exist, insert it into two tables如果数据不存在,则将其插入到两个表中
INSERT INTO search_results (company_id, link, title, domain)
VALUES (4, 'https://test.com', 'title', 'test.com');
and also insert the search_result
last inserted id to the following table.并将
search_result
最后插入的 id 插入下表。 corporate_statement
value is always 1 corporate_statement
值始终为 1
INSERT INTO corporate_statements (statement_link_id, corporate_statement)
VALUES (743, 1);
I'm trying this based on what I found on SO我正在根据我在SO上找到的内容进行尝试
DECLARE @result AS TABLE (id int, company_id int, link text, title text, domain text);
WITH cte AS
(
SELECT *
FROM (VALUES (4, 'https://test.com', null, null)) AS t(company_id, link, title, domain)
)
INSERT INTO @result
SELECT *
FROM
(INSERT INTO dbo.search_results (company_id, link, title, domain)
OUTPUT inserted.*
SELECT * FROM cte
WHERE NOT EXISTS (SELECT * FROM dbo.[search_results]
WHERE company_id = cte.company_id
AND CAST(link AS varchar(250)) = CAST(cte.link AS varchar(50))
)) r
SELECT * FROM @result;
Even trying with a single insert statement, I get the following error:即使尝试使用单个插入语句,我也会收到以下错误:
Msg 213, Level 16, State 1, Line 8
消息 213,第 16 层,State 1,第 8 行
Column name or number of supplied values does not match table definition.列名或提供的值的数量与表定义不匹配。
As you can see, I also tried to cast it to varchar since it was throwing error when I hadn't.正如你所看到的,我也尝试将它转换为 varchar,因为它在我没有抛出错误的时候抛出了错误。 How can update this?
这个怎么更新?
To me - this seems a lot cleaner, and it also will be a lot simpler to understand (and maintain:) in the future:对我来说 - 这似乎更干净,而且将来也更容易理解(和维护:):
-- check to see if your data already exists
IF NOT EXISTS (SELECT *
FROM search_results
WHERE company_id = 4 AND link = 'https://test.com')
BEGIN TRY
BEGIN TRANSACTION
-- if not -> insert into the first table
INSERT INTO search_results (company_id, link, title, domain)
VALUES (4, 'https://test.com', 'title', 'test.com');
-- grab the last identity value from that previous INSERT
DECLARE @LastId INT;
SELECT @LastId = SCOPE_IDENTITY();
-- insert into the second table
INSERT INTO corporate_statements (statement_link_id, corporate_statement)
VALUES (@LastId, 1);
COMMIT;
END TRY
BEGIN CATCH
-- in case of an error rollback the full transaction
ROLLBACK;
END CATCH;
and you're done.你就完成了。 Or am I missing something?
还是我错过了什么? I think this would be doing what you're described in the intro of your post - not necessarily what you're showing in your code...
我认为这将执行您在帖子简介中描述的内容-不一定是您在代码中显示的内容...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.