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 2000 Forums
 SQL Server Development (2000)
 Why won't it let me do this?

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2003-03-20 : 08:35:48
I'm writing a stored proc that will fill a table in the database with it's results.

The relevant part of the stored proc is this:

quote:

Declare @email varchar(50)

Declare cursid cursor for
select distinct(right(email, (len(email)-charindex('@', email)))) from smc_new_products.dbo.usr_smc


Open cursid
Fetch next from cursid into @email
while (@@fetch_status<>-1)
Begin

insert into company_activity
select @email, (firstName + ' ' + lastname) as [Full Name], company_name as [Company], configname, requestdatetime
from
smc_new_products.dbo.etechmodelrequests et
join
smc_new_products.dbo.usr_smc us on et.username=convert(varchar(50), us.[user_id])
where
username in
(select convert(varchar(50), [user_id]) from smc_new_products.dbo.usr_smc where right(email, (len(email)-charindex('@', email)))=@email)
and
et.interfacename like '%download%'
order by et.requestdatetime

Fetch next from cursid into @email
End



The error I'm getting when I try to do this comes at the point where I'm trying to insert my data into the table in my database.

The error is this:

quote:
Error 8101: An explicit value for the indentity column in the table 'company_activity' can only be specified when a column list is used and IDENTITY_INSERT is ON


The table I'm trying to insert into is:

quote:

GO

CREATE TABLE [dbo].[company_activity] (
[Customer_ID] [int] IDENTITY (1, 1) NOT NULL ,
[email] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[full_name] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[company] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[configname] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[download_date] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[company_activity] WITH NOCHECK ADD
CONSTRAINT [PK_company_activity] PRIMARY KEY CLUSTERED
(
[Customer_ID]
) ON [PRIMARY]
GO



I know I've inserted records, in bulk, like this before in database tables so I have no idea why I'm getting this error now. Any ideas ?

Thanks in advance.

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-03-20 : 08:47:01
Hi!

Since the column Customer_ID is an identity column and hence generated automatically, you cannot insert values into that column. You'll have to use a syntax like this instead:

insert into company_activity (email, full_name, ... (etc)
select ... (etc)



Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2003-03-20 : 08:48:19
Hi
You havent defined a column list

Quote
"insert into company_activity
select @email, (firstName + ' ' + lastname) as [Full Name], company_name as [Company], configname, requestdatetime"

Should be
INSERT company_activity(email, fullname, company etc)
SELECT @email, .................

SQL Server is trying to insert @Email into your customerid column which is an identity

Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2003-03-20 : 08:58:21
quote:

Hi
You havent defined a column list

Quote
"insert into company_activity
select @email, (firstName + ' ' + lastname) as [Full Name], company_name as [Company], configname, requestdatetime"

Should be
INSERT company_activity(email, fullname, company etc)
SELECT @email, .................




Thanks to both of you.....that worked. I'm a little suprised it tried to insert the email into the Identity field though....I would have guessed it'd be smarter than that and try and start the insert at the first open column after the indentity.

Thanks again.



Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2003-03-20 : 09:26:34
Quote from BOL
"If the values in the VALUES list are not in the same order as the columns in the table or do not have a value for each column in the table, column_list must be used to explicitly specify the column that stores each incoming value"

I always use column lists especially on re-useable "queries/sp" - you never know when some "idiot" will come along and amend the table by sticking a new column in the middle of it rather than at the end!

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-03-20 : 10:54:21
Huh?

Why do you need a cursor? Your cursor is already part of your select.

Your cursor is based on usr_smc, right? Isn't the "IN" component of your Predicate Identical to the Cursor? What am I missing?



INSERT INTO company_activity
SELECT @email, (firstName + ' ' + lastname) as [Full Name], company_name as [Company], configname, requestdatetime
FROM etechmodelrequests et
INNER JOIN usr_smc us
ON et.username=convert(varchar(50), us.[user_id])
WHERE username in (
SELECT convert(varchar(50), [user_id])
FROM usr_smc
WHERE right(email, (len(email)-charindex('@', email)))=@email)
AND et.interfacename like '%download%'

--Why an Order by on an INSERT?
--order by et.requestdatetime


Brett

8-)
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-03-20 : 21:41:29
quote:

I'm a little suprised it tried to insert the email into the Identity field though....I would have guessed it'd be smarter than that and try and start the insert at the first open column after the indentity.



Thankfully, SQL Server is one of the Microsoft products that does not try to be too smart.
It does what it is told, and the developer is expected to be smart.

I like that much better.


Damian
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-03-20 : 22:10:31
My favorite complaint:

If the computer is so smart, why didn't it understand that I MEANT something else other than what I told it to do!



- Jeff
Go to Top of Page
   

- Advertisement -