简体   繁体   English

如何在 SQL 服务器中使用单个查询在多个表中插入数据?

[英]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.

相关问题 使用单个sql查询如何在未连接的三个表中插入数据。 或者我们可以将数据插入未连接的多个表中 - using single sql query how to insert data in three tables which are not joined . or can we insert data into multiple tables which are not joined 在单个查询中将数据插入多个表 - Insert data to multiple tables in single query 使用一个SQL查询将数据插入多个表 - Insert data into multiple tables using one SQL query 如何使用单个查询查询多个表? - How to query multiple tables using a single query? 在SQL Server的一个查询中使用外键同时将新数据插入到多个表中 - Insert New data into multiple tables with Foreign Key simultaneously in One query in sql server 在JAVA中使用JDBC在单个查询中将多行插入到两个表中 - Insert multiple rows into two tables in a single query using JDBC in JAVA 如何使用引用多个表的SQL查询访问数据? - How to access data using a SQL query that references multiple tables? 如何使用SQL查询从多个表中获取数据 - How to get data from multiple tables using SQL query 单个查询从SQL Server中的多个表中删除 - single query to delete from multiple tables in sql server 如何使用单个查询更新多个表 - How to update multiple tables using single query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM