Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 insert query fails!!

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-25 : 21:00:12

ProductTable:

-- alias b
prd_id
ord_id(int null)
quantity(int null)

QuantityTable:

-- alias a
ProductID
orderID(int null)
Qty(int null)

SQL Query


insert ProductTable (b.quantity)
select a.Qty
from QuantityTable a
inner join ProductTable b on b.ord_id = a. orderID
where a.orderID is not null

Basically I need to check the orderId in both table and based on that enter the quantity(whether it is null or not) from QuantityTable to ProductTable. Please note that orderId can be null and quantity can be null. But still the ProductTable needs to be updated

Error Message


Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'prd_id', table 'dbo. ProductTable '; column does not allow nulls. INSERT fails.



SA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-25 : 22:10:41
Is prd_id set to the identity in your product table?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

GeoffreyP
Starting Member

9 Posts

Posted - 2009-11-26 : 00:55:53
Should you not be using update as to insert you need to insert all fields in the row.
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-11-26 : 01:05:03
Check whether prd_id set to the NOT NULL in your product table?

Msg 515, Level 16, State 2

is the error message displayed for inserting a null value to a column which is set to NOT NULL constraint.. Kindly refer..

http://www.sommarskog.se/error-handling-I.html


Balaji.K
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 13:14:40
The issue is as addressed by tkizer - that prd_id is set to identity. What can I chnage in my query to be able to insert.



SA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-11-30 : 13:18:00
quote:
Originally posted by agarwasa2008

The issue is as addressed by tkizer - that prd_id is set to identity. What can I chnage in my query to be able to insert.



SA


if its an identity field it should auto generate values. else you need to insert an explicit value for prd_id
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 13:23:23
Show us the actual DDL for the table so that we can verify it is setup correctly. The DDL is the CREATE TABLE statement and can be generated for you in SSMS.

I have a strong feeling that you think it is identity, however the table is not setup that way.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 13:34:10
CREATE TABLE [dbo].[ProductTable](
[prd_id] [int] IDENTITY(1,1) NOT NULL,
[emp_id] [char] (8) NOT NULL,
[ord_id] [int] NULL,
[quantity] [int] NULL,
CONSTRAINT [PK_ ProductTable_1] PRIMARY KEY CLUSTERED
(
[prd_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 13:40:06
Let's do a test then:

INSERT INTO ProductTable (quantity)
VALUES (1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 13:45:51
Please note that I have added [emp_id] [char] (8) NOT NULL, to the DDL I posted earlier. And this did not work. Got the following error message: Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'emp_id', table 'dbo. ProductTable'; column does not allow nulls. INSERT fails.
The statement has been terminated.

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 13:47:03
INSERT INTO ProductTable (quantity, emp_id)
VALUES (1, <SomeValue>)

I put <SomeValue> in there because I don't know if you've got a FK constraint that prevent test data, so please modify it as needed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 13:56:33
Thanks tkizer. It worked. But what I need to do is just update that 1 column - quantity. I just added that column and I need to update the values in that column only. The rest of the table values - epm_id is correctly populated in the ProductTable.

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 13:57:35
Then you need an UPDATE statement and not an INSERT statement.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 14:08:43
Vow!! I completely missed that. Thanks!! How do I update with a select statement. Please help!!

update ProductTable (b.quantity)
select a.Qty
from QuantityTable a
inner join ProductTable b on b.ord_id = a. orderID
where a.orderID is not null


SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 14:10:08
update b
set quantity = a.Qty
from ProductTable b
inner join QuantityTable a
on b.ord_id = a. orderID
where a.orderID is not null

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-30 : 14:15:19
Appreciate it so very much .... it worked like a charm.

SA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-30 : 14:27:58
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -