简体   繁体   English

如何根据SQL服务器中的一条记录插入一行

[英]How to insert a row based on a record in SQL Server

I have a Table where 'ID' is an auto number.我有一张表,其中“ID”是一个自动编号。 There are a few records present with the same OrderID.存在一些具有相同 OrderID 的记录。 I want to add another record to this table with the same OrderID.我想用相同的 OrderID 向该表中添加另一条记录。 I tried the following but it does not do anything.我尝试了以下但它没有做任何事情。

IF NOT EXISTS (SELECT 1 FROM Orders WHERE OrderID='2344567')
Insert into orders (Product_Id, Quantity, Description, Price)
Values ('" & Product_Id & "', '" & Quantity & "', '" & Description & "', '" & Price & "')

Can you help please?你能帮忙吗?

In SQL Server you can use在 SQL 服务器你可以使用

SET IDENTITY INSERT - Allows explicit values to be inserted into the identity column of a table. SET IDENTITY INSERT - 允许将显式值插入到表的标识列中。

In this case you turn on and off the possibility to insert into an identity column which is defined as a sequence在这种情况下,您可以打开和关闭插入到定义为序列的标识列的可能性

The following link will help to resolve the task: SQL Server set identity以下链接将有助于解决该任务: SQL 服务器设置标识

Use SQL Server Profiler to see what query will be run on your Db.使用SQL Server Profiler查看将在您的数据库上运行的查询。 I think you may have some problems on setting parameters on your application, then you can see what really happening in your Db.我认为您在设置应用程序参数时可能会遇到一些问题,然后您可以看到 Db 中到底发生了什么。

-- If (OrderID='2344567') record is EXISTS , your INSERT statement doesn't work -- 如果 (OrderID='2344567') 记录是EXISTS ,您的INSERT语句不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM