Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-2008, 10:20 PM
Erland Sommarskog
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

pbd22 (dushkin@gmail.com) writes:
> I am trying to insert a zero into the below table and I am being told
> that:
>
> Cannot insert the value NULL into column 'UsageToday', table
> DB.dbo.Pub_Count'; column does not allow nulls. UPDATE fails. The
> statement has been terminated.
>
> What I am trying to insert is a string that I convert into an int
> before the insert. It complains
> when I try to insert the converted zero.


Juding from the error message, you are trying to insert a NULL.

Since you don't show us any code or data, there is no way we can tell
what you are doing wrong.





--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #2 (permalink)  
Old 08-12-2008, 10:22 PM
pbd22
 
Posts: n/a
Trying to insert a zero, its telling me i cannot insert a null


Hi.

I am trying to insert a zero into the below table and I am being told
that:

Cannot insert the value NULL into column 'UsageToday', table
DB.dbo.Pub_Count'; column does not allow nulls. UPDATE fails. The
statement has been terminated.

What I am trying to insert is a string that I convert into an int
before the insert. It complains
when I try to insert the converted zero.

The table is here:

CREATE TABLE [dbo].[Pub_Count](
[CPID] [int] NOT NULL,
[UsageToday] [int] NOT NULL CONSTRAINT [DF__Pub_C__Usage__19DFD96B]
DEFAULT ((0)),
CONSTRAINT [PK_Pub_Count] PRIMARY KEY CLUSTERED
(
[CPID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Thanks in advance.
Reply With Quote
  #3 (permalink)  
Old 08-12-2008, 10:31 PM
Plamen Ratchev
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

What is the SQL statement that you use to insert? Try this:

INSERT INTO Pub_Count VALUES(1, '0');

INSERT INTO Pub_Count VALUES(2, CAST('0' AS INT));

You can use COALESCE to make sure the value is not NULL.


Plamen Ratchev
http://www.SQLStudio.com
Reply With Quote
  #4 (permalink)  
Old 08-12-2008, 10:42 PM
pbd22
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

Hi,

Thanks for the fast reply.

It is a stored procedure i am calling from the code-behind.
I have looked (again and again) at the values I am passing
and I never see a null value... the point it complains on I see
that I am trying to pass a zero.

Here is the bit of code where I construct the command:


List<List<string>> pubs =
null;

SqlCommand command =
conn.CreateCommand();
command.CommandText =
"vb_Update";
command.CommandType =
CommandType.StoredProcedure;

//declare 4 different (or
whatever number you need) params
SqlParameter pub_id = new
SqlParameter("@pub_id", SqlDbType.Int);
SqlParameter pub_bw = new
SqlParameter("@total_yesterday_hours", SqlDbType.Int);
SqlParameter pub_date = new
SqlParameter("@param_date", SqlDbType.SmallDateTime);

//add them to command

command.Parameters.Add(pub_id);

command.Parameters.Add(pub_bw);

command.Parameters.Add(pub_date);

pubs = new
Profile().Publisher();

for (int j = 0; j <
pubs.Count; j++)
{
int p = 0;

for (int k = 0; k <
pubs[j].Count; k++)
{

if (k != 2)
{

command.Parameters[p].Value = Convert.ToInt32(pubs[j][k]);

p++;
}
else
{


command.Parameters[p].Value = pubs[j][k];
}

}

Reply With Quote
  #5 (permalink)  
Old 08-12-2008, 10:59 PM
pbd22
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

here is the procedure:


ALTER PROCEDURE [dbo].[vb_Update]
(
@pub_id INT,
@total_yesterday_hours INT,
@param_date SMALLDATETIME
)
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

IF NOT EXISTS
(SELECT CountDate
FROM Pub_ViewCount
WHERE CPID = @pub_id
AND CountDate = @param_date )
BEGIN
INSERT INTO Pub_ViewCount (
CPID, CountDate, ViewCount,
EmbedCount, DirectCount,
UsageToday ) VALUES (
@pub_id, @param_date, 0,0,0,0)
END

-- get total for yesterday, add to UsageTodayBW
UPDATE Pub_ViewCount
SET Pub_ViewCount.DailyPSHours= @total_yesterday_hours
WHERE Pub_ViewCount.CountDate = @param_date
AND Pub_ViewCount.CPID = @pub_id;

UPDATE Pub_ViewCount
SET Pub_ViewCount.UsageToday = (@total_yesterday_hours - (SELECT
Pub_ViewCount.DailyPSHours
FROM Pub_ViewCount
WHERE Pub_ViewCount.CountDate = CONVERT(VARCHAR(50), DATEADD(day, -1,
@param_date), 101)
AND Pub_ViewCount.CPID = @pub_id))
WHERE Pub_ViewCount.CountDate = @param_date
AND Pub_ViewCount.CPID = @pub_id;

UPDATE Pub_Usage
SET TotalUsed = (SELECT SUM(p.UsageToday)
FROM Pub_ViewCount AS p
WHERE p.CPID = @pub_id)
WHERE PUPID = @pub_id;

END
Reply With Quote
  #6 (permalink)  
Old 08-12-2008, 11:12 PM
pbd22
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

I think the problem statement is here:

UPDATE Pub_ViewCount
SET Pub_ViewCount.UsageToday = (@total_yesterday_hours - (SELECT
Pub_ViewCount.DailyPSHours
FROM Pub_ViewCount
WHERE Pub_ViewCount.CountDate = CONVERT(VARCHAR(50), DATEADD(day, -1,
@param_date), 101)
AND Pub_ViewCount.CPID = @pub_id))
WHERE Pub_ViewCount.CountDate = @param_date
AND Pub_ViewCount.CPID = @pub_id;


Particularly:

(@total_yesterday_hours - (SELECT
Pub_ViewCount.DailyPSHours
FROM Pub_ViewCount
WHERE Pub_ViewCount.CountDate = CONVERT(VARCHAR(50), DATEADD(day, -1,
@param_date), 101)
AND Pub_ViewCount.CPID = @pub_id))


I have tried ISNULL around @total_yesterday_count but that didn't
work.

any ideas?
Reply With Quote
  #7 (permalink)  
Old 08-12-2008, 11:22 PM
Plamen Ratchev
 
Posts: n/a
Re: Trying to insert a zero, its telling me i cannot insert a null

The problem is with the following update:

UPDATE Pub_ViewCount
SET Pub_ViewCount.UsageToday = (@total_yesterday_hours - (SELECT
Pub_ViewCount.DailyPSHours
FROM Pub_ViewCount
WHERE Pub_ViewCount.CountDate = CONVERT(VARCHAR(50), DATEADD(day, -1,
@param_date), 101)
AND Pub_ViewCount.CPID = @pub_id))
WHERE Pub_ViewCount.CountDate = @param_date
AND Pub_ViewCount.CPID = @pub_id;


If the subquery does not return any rows then you are updating to
(@total_yesterday_hours - NULL) which evaluates to NULL. Add COALESCE:

UPDATE Pub_ViewCount
SET Pub_ViewCount.UsageToday = @total_yesterday_hours - COALESCE(<subquery>,
0)
....


Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:48 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Unblock Myspace | Moneygram | Bad Credit Loan | Repair Bad Credit | Cell Phones



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114