Unix Technical Forum

sql insert problem

This is a discussion on sql insert problem within the SQL Server forums, part of the Microsoft SQL Server category; --> INSERT INTO Comments_tbl (FullName_tsf, Email_tsf, NTuser_tsf, name_tsf, type_tsf, comments_tsf) VALUES ('Test Tester' , 'tester@test.com', 'ttest', 'test', 'help', 'need better ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:13 AM
kieran
 
Posts: n/a
Default sql insert problem

INSERT INTO Comments_tbl (FullName_tsf, Email_tsf, NTuser_tsf,
name_tsf, type_tsf, comments_tsf) VALUES ('Test Tester' ,
'tester@test.com', 'ttest', 'test', 'help', 'need better help')

I am trying to use the following sql to insert from a script but there
is a primary key column - 'com_no' - on the table.

How do I write the sql insert so that the 'com_no' value gets a value
inserted into and increases each time.

Thanks for all help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:13 AM
Simon Hayes
 
Posts: n/a
Default Re: sql insert problem


"kieran" <kieran5405@hotmail.com> wrote in message
news:b39796b3.0406141026.5d6e7da0@posting.google.c om...
> INSERT INTO Comments_tbl (FullName_tsf, Email_tsf, NTuser_tsf,
> name_tsf, type_tsf, comments_tsf) VALUES ('Test Tester' ,
> 'tester@test.com', 'ttest', 'test', 'help', 'need better help')
>
> I am trying to use the following sql to insert from a script but there
> is a primary key column - 'com_no' - on the table.
>
> How do I write the sql insert so that the 'com_no' value gets a value
> inserted into and increases each time.
>
> Thanks for all help.


I'm not completely sure from your description, but I guess you're looking
for an IDENTITY column, which will automatically generate a new value for
each INSERT? Unfortunately, you can't add the IDENTITY property to an
existing table, so you would need to create the table again:

create table dbo.Comments (
CommentID int identity(1,1) not null,
FullName ...,
...,
constraint PK_Comments primary key nonclustered (CommentID)
)

After the INSERT, you can use the function SCOPE_IDENTITY() to get the value
that was just inserted.

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:14 AM
kieran
 
Posts: n/a
Default Re: sql insert problem

Thanks Simon.

That clears up everything for me.





"Simon Hayes" <sql@hayes.ch> wrote in message news:<40cdf3be$1_1@news.bluewin.ch>...
> "kieran" <kieran5405@hotmail.com> wrote in message
> news:b39796b3.0406141026.5d6e7da0@posting.google.c om...
> > INSERT INTO Comments_tbl (FullName_tsf, Email_tsf, NTuser_tsf,
> > name_tsf, type_tsf, comments_tsf) VALUES ('Test Tester' ,
> > 'tester@test.com', 'ttest', 'test', 'help', 'need better help')
> >
> > I am trying to use the following sql to insert from a script but there
> > is a primary key column - 'com_no' - on the table.
> >
> > How do I write the sql insert so that the 'com_no' value gets a value
> > inserted into and increases each time.
> >
> > Thanks for all help.

>
> I'm not completely sure from your description, but I guess you're looking
> for an IDENTITY column, which will automatically generate a new value for
> each INSERT? Unfortunately, you can't add the IDENTITY property to an
> existing table, so you would need to create the table again:
>
> create table dbo.Comments (
> CommentID int identity(1,1) not null,
> FullName ...,
> ...,
> constraint PK_Comments primary key nonclustered (CommentID)
> )
>
> After the INSERT, you can use the function SCOPE_IDENTITY() to get the value
> that was just inserted.
>
> Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:14 AM
sa
 
Posts: n/a
Default Re: sql insert problem

Hello Simon,

Suppose, unlike Kieran's case, I have a table with one column set as
primary key, which is not supposed to be duplicated. This key has a
corresponding textbox on the form which the user enters manually.

I use .ExecuteNonQuery() after setting up the parameters of a
SQLCommand object to insert a row. The SP does a simple insert
routine.

Using one single button for updating the table (new row/existing row),
is there a simple / automatic method to modify an existing row in the
table (using Triggers, etc.) ?

Or, do I have to write the code in the button that will -

first, fire an sp that returns back a boolean indicating whether a row
with the user-entered primary key already exists, then

second, conditionally set the .commandtext value to either of the two
SPs (INSERTSP / UPDATESP) depending on whether the boolean was
row-exists/does-not-exist.

finally, fire the .executenonquery() ?

Any help will be highly appreciated.

Regards,

Sanjay.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:14 AM
Zach Wells
 
Posts: n/a
Default Re: sql insert problem

sa wrote:
> Hello Simon,
>
> Suppose, unlike Kieran's case, I have a table with one column set as
> primary key, which is not supposed to be duplicated. This key has a
> corresponding textbox on the form which the user enters manually.


Then you'd have a bad design. You really shouldn't allow primary keys
to be changed, especially by the user. If you find yourself changing
your primary keys so often that you need a data entry field, then
rethink your primary key. That said...

What you can do is test for the row's existance in the stored procedure.
Basically, your stored procedure can do one or the other depending on
the case, no need for multiple procedures:

IF EXISTS(SELECT statement to check for the row)
UPDATE Statement
ELSE
INSERT statement

Zach
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 04:14 AM
Carl H
 
Posts: n/a
Default Re: sql insert problem

try this...

declare @com_no int,
@cnt int,
@rowcnt int


select @com_no =max(com_no) from Comments_tbl
select @cnt=0,
@rowcnt=count(*) from <table with rows to insert, or hard code a
number>

while @cnt < @rowcnt
begin
INSERT INTO Comments_tbl (com_no, FullName_tsf, Email_tsf,
NTuser_tsf,
name_tsf, type_tsf, comments_tsf) VALUES (@com_no, 'Test Tester' ,
'tester@test.com', 'ttest', 'test', 'help', 'need better help')
select @com_no-@comno+1

if @cnt>@rowcnt break else continue
end


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:45 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com