在sql查询中需要帮助以插入记录 [英] want help in sql query for insert the record

查看:84
本文介绍了在sql查询中需要帮助以插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用where子句在表中插入一些记录吗?请给我任何例子.

can we use where clause for insert some records in a table? pls give me any example.

推荐答案

在表中创建新记录而不是更新表时,在Insert语句中不需要Where子句.现有记录.因此,所有字段都没有值,从而使Where多余.

希望这对您有帮助
There is no need for a Where clause in an Insert statement, as you are creating a new record in the table, not updating an existing record. Therefore, none of the fields would have values, making the Where redundant.

Hope this helps


否.
SQL INSERT语句没有用于WHERE子句的工具: W3Schools INSERT [
No.
SQL INSERT statement does not have any facility for WHERE clauses: W3Schools INSERT[^]

Think about it: What would you use it for?


如果您将记录从另一个表插入到一个表中,则可以使用
if you r inserting records into a table from another table you can use
INSERT INTO SELECT


当在数据库中较早地创建表并且要从另一个表将数据插入该表时,将使用此方法.如果insert子句和select子句中列出的列相同,则不需要列出它们.我总是出于可读性和可伸缩性的目的列出它们.以下是演示数据库的示例,AdventureWorks是Sql Server2005附带的.


This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are are not required to list them. I always list them for readability and scalability purpose.Here is eaxmple of demo database AdventureWorks comes with Sql Server2005.

USE AdventureWorks
GO
----Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO


这篇关于在sql查询中需要帮助以插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆