简体   繁体   中英

How to insert a row based on a record in SQL Server

I have a Table where 'ID' is an auto number. There are a few records present with the same OrderID. I want to add another record to this table with the same 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

SET IDENTITY INSERT - Allows explicit values to be inserted into the identity column of a table.

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

Use SQL Server Profiler to see what query will be run on your Db. I think you may have some problems on setting parameters on your application, then you can see what really happening in your Db.

-- If (OrderID='2344567') record is EXISTS , your INSERT statement doesn't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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