Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > An SQL question

Reply
Thread Tools

An SQL question

 
 
Shelly
Guest
Posts: n/a
 
      09-21-2007
This is more of an SQL question, but I would like to know how to do it in
SQL Server.

In a given table I have two columns of importance. The first is account
number (and it is a foreign key). The other is agent_id. The combination
must be unique. What I would like to do is to autoincrement the agent_id
for a given account number. Example:

Account Number Agent ID
1000 1
1000 2
1100 1
1200 1
1200 2
1200 3

When I add a new agent to account number 1200, I would like it to come up
automatically with 2 for the agent_id.. I know I could do a select on
account number and return MAX of agent_id. I could then increment that
value and use that pair for new agent creation. However, I wonder if thee
is a way to do that automatically in SQL?

Shelly


 
Reply With Quote
 
 
 
 
Satish Itty
Guest
Posts: n/a
 
      09-21-2007
Make the Agent ID in the table as IDENTITY column and it will
autoincrement itself.


CREATE TABLE [dbo].[YourTable](
[AGENT_ID] [int] IDENTITY(1,1) NOT NULL,
[Account Number] [varchar](25) NOT NULL,
....
)

Shelly wrote:
> This is more of an SQL question, but I would like to know how to do it in
> SQL Server.
>
> In a given table I have two columns of importance. The first is account
> number (and it is a foreign key). The other is agent_id. The combination
> must be unique. What I would like to do is to autoincrement the agent_id
> for a given account number. Example:
>
> Account Number Agent ID
> 1000 1
> 1000 2
> 1100 1
> 1200 1
> 1200 2
> 1200 3
>
> When I add a new agent to account number 1200, I would like it to come up
> automatically with 2 for the agent_id.. I know I could do a select on
> account number and return MAX of agent_id. I could then increment that
> value and use that pair for new agent creation. However, I wonder if thee
> is a way to do that automatically in SQL?
>
> Shelly
>
>

 
Reply With Quote
 
 
 
 
Just Me
Guest
Posts: n/a
 
      09-21-2007
Well, to get the lastAgentID number you could

Select max(AgentID) From Agent





"Shelly" <> wrote in message
news:...
> This is more of an SQL question, but I would like to know how to do it in
> SQL Server.
>
> In a given table I have two columns of importance. The first is account
> number (and it is a foreign key). The other is agent_id. The combination
> must be unique. What I would like to do is to autoincrement the agent_id
> for a given account number. Example:
>
> Account Number Agent ID
> 1000 1
> 1000 2
> 1100 1
> 1200 1
> 1200 2
> 1200 3
>
> When I add a new agent to account number 1200, I would like it to come up
> automatically with 2 for the agent_id.. I know I could do a select on
> account number and return MAX of agent_id. I could then increment that
> value and use that pair for new agent creation. However, I wonder if thee
> is a way to do that automatically in SQL?
>
> Shelly
>



 
Reply With Quote
 
Just Me
Guest
Posts: n/a
 
      09-21-2007
Erm, perhaps Im missing the point there but If you do as you suggest you
would also need a relation in the agent table. This kinda seems ass about
face to me. Just because you add an account, why do you need a new agent
automatically as this implies a one to one relationship which is different
to that which is in the table shown below.

In the table shown below the OP seems to have suggested that there is an
incremental increase in the AgentID for each group of numbers, IE
1000,1100,1200 ranges each have their own incremental range , but the text
below is suggesting that the OP could take a MAX( AgentID) to get the
number.

This all seems wrong to me !


"Satish Itty" <> wrote in message
news:u3lQ6wF$...
> Make the Agent ID in the table as IDENTITY column and it will
> autoincrement itself.
>
>
> CREATE TABLE [dbo].[YourTable](
> [AGENT_ID] [int] IDENTITY(1,1) NOT NULL,
> [Account Number] [varchar](25) NOT NULL,
> ....
> )
>
> Shelly wrote:
>> This is more of an SQL question, but I would like to know how to do it in
>> SQL Server.
>>
>> In a given table I have two columns of importance. The first is account
>> number (and it is a foreign key). The other is agent_id. The
>> combination must be unique. What I would like to do is to autoincrement
>> the agent_id for a given account number. Example:
>>
>> Account Number Agent ID
>> 1000 1
>> 1000 2
>> 1100 1
>> 1200 1
>> 1200 2
>> 1200 3
>>
>> When I add a new agent to account number 1200, I would like it to come up
>> automatically with 2 for the agent_id.. I know I could do a select on
>> account number and return MAX of agent_id. I could then increment that
>> value and use that pair for new agent creation. However, I wonder if
>> thee is a way to do that automatically in SQL?
>>
>> Shelly



 
Reply With Quote
 
Shelly
Guest
Posts: n/a
 
      09-21-2007

"Satish Itty" <> wrote in message
news:u3lQ6wF$...
> Make the Agent ID in the table as IDENTITY column and it will
> autoincrement itself.


No, I only want it to autoincrement as a subset of account numbers. It does
not have to be unique. Only the combination of the two must be unique.

>
>
> CREATE TABLE [dbo].[YourTable](
> [AGENT_ID] [int] IDENTITY(1,1) NOT NULL,
> [Account Number] [varchar](25) NOT NULL,
> ....
> )
>
> Shelly wrote:
>> This is more of an SQL question, but I would like to know how to do it in
>> SQL Server.
>>
>> In a given table I have two columns of importance. The first is account
>> number (and it is a foreign key). The other is agent_id. The
>> combination must be unique. What I would like to do is to autoincrement
>> the agent_id for a given account number. Example:
>>
>> Account Number Agent ID
>> 1000 1
>> 1000 2
>> 1100 1
>> 1200 1
>> 1200 2
>> 1200 3
>>
>> When I add a new agent to account number 1200, I would like it to come up
>> automatically with 2 for the agent_id.. I know I could do a select on
>> account number and return MAX of agent_id. I could then increment that
>> value and use that pair for new agent creation. However, I wonder if
>> thee is a way to do that automatically in SQL?
>>
>> Shelly



 
Reply With Quote
 
Shelly
Guest
Posts: n/a
 
      09-21-2007

"Just Me" <news.microsoft.com> wrote in message
news:u8iF53F$...
> Erm, perhaps Im missing the point there but If you do as you suggest you
> would also need a relation in the agent table. This kinda seems ass about
> face to me. Just because you add an account, why do you need a new agent
> automatically as this implies a one to one relationship which is different
> to that which is in the table shown below.


It is not when I add an account. It is when I add an agent in the agent
table to an already existing account in the accounts table. The account to
agent is one to many, but each of the agent IDs for that account must be
unique. In the agent table, the account number is a foreign key. It is a
primary key in the account table.

>
> In the table shown below the OP seems to have suggested that there is an
> incremental increase in the AgentID for each group of numbers, IE
> 1000,1100,1200 ranges each have their own incremental range , but the text
> below is suggesting that the OP could take a MAX( AgentID) to get the
> number.
>
> This all seems wrong to me !


I can take a MAX(AgentID) when I select on accountNumber in the Agent table.
IOW, where I have a WHERE clause for accountNumber=the_account_number.

>
>
> "Satish Itty" <> wrote in message
> news:u3lQ6wF$...
>> Make the Agent ID in the table as IDENTITY column and it will
>> autoincrement itself.
>>
>>
>> CREATE TABLE [dbo].[YourTable](
>> [AGENT_ID] [int] IDENTITY(1,1) NOT NULL,
>> [Account Number] [varchar](25) NOT NULL,
>> ....
>> )
>>
>> Shelly wrote:
>>> This is more of an SQL question, but I would like to know how to do it
>>> in SQL Server.
>>>
>>> In a given table I have two columns of importance. The first is account
>>> number (and it is a foreign key). The other is agent_id. The
>>> combination must be unique. What I would like to do is to autoincrement
>>> the agent_id for a given account number. Example:
>>>
>>> Account Number Agent ID
>>> 1000 1
>>> 1000 2
>>> 1100 1
>>> 1200 1
>>> 1200 2
>>> 1200 3
>>>
>>> When I add a new agent to account number 1200, I would like it to come
>>> up automatically with 2 for the agent_id.. I know I could do a select
>>> on account number and return MAX of agent_id. I could then increment
>>> that value and use that pair for new agent creation. However, I wonder
>>> if thee is a way to do that automatically in SQL?
>>>
>>> Shelly

>
>



 
Reply With Quote
 
Shelly
Guest
Posts: n/a
 
      09-21-2007

"Just Me" <news.microsoft.com> wrote in message
news:Ogr6xxF$...
> Well, to get the lastAgentID number you could
>
> Select max(AgentID) From Agent


Like I said, I know I can do this (adding, of course, the clause WHERE
accountNumber=theaccountNumber) , but my question is can SQL do this
automatically on the insert statement into the Agent table? If it can, then
I can do an ExecuteScalar and return that value from the insert query
without having to do two queries in succession with the possiblity of a race
condition where someone else is adding an agent to the same account at the
same time.

Shelly

>
>
>
>
>
> "Shelly" <> wrote in message
> news:...
>> This is more of an SQL question, but I would like to know how to do it in
>> SQL Server.
>>
>> In a given table I have two columns of importance. The first is account
>> number (and it is a foreign key). The other is agent_id. The
>> combination must be unique. What I would like to do is to autoincrement
>> the agent_id for a given account number. Example:
>>
>> Account Number Agent ID
>> 1000 1
>> 1000 2
>> 1100 1
>> 1200 1
>> 1200 2
>> 1200 3
>>
>> When I add a new agent to account number 1200, I would like it to come up
>> automatically with 2 for the agent_id.. I know I could do a select on
>> account number and return MAX of agent_id. I could then increment that
>> value and use that pair for new agent creation. However, I wonder if
>> thee is a way to do that automatically in SQL?
>>
>> Shelly
>>

>
>



 
Reply With Quote
 
Just Me
Guest
Posts: n/a
 
      09-22-2007
OK, now I understand what you are trying to acheive. I think the answer is
no there is not.




"Shelly" <> wrote in message
news:...
>
> "Just Me" <news.microsoft.com> wrote in message
> news:Ogr6xxF$...
>> Well, to get the lastAgentID number you could
>>
>> Select max(AgentID) From Agent

>
> Like I said, I know I can do this (adding, of course, the clause WHERE
> accountNumber=theaccountNumber) , but my question is can SQL do this
> automatically on the insert statement into the Agent table? If it can,
> then I can do an ExecuteScalar and return that value from the insert query
> without having to do two queries in succession with the possiblity of a
> race condition where someone else is adding an agent to the same account
> at the same time.
>
> Shelly
>
>>
>>
>>
>>
>>
>> "Shelly" <> wrote in message
>> news:...
>>> This is more of an SQL question, but I would like to know how to do it
>>> in SQL Server.
>>>
>>> In a given table I have two columns of importance. The first is account
>>> number (and it is a foreign key). The other is agent_id. The
>>> combination must be unique. What I would like to do is to autoincrement
>>> the agent_id for a given account number. Example:
>>>
>>> Account Number Agent ID
>>> 1000 1
>>> 1000 2
>>> 1100 1
>>> 1200 1
>>> 1200 2
>>> 1200 3
>>>
>>> When I add a new agent to account number 1200, I would like it to come
>>> up automatically with 2 for the agent_id.. I know I could do a select
>>> on account number and return MAX of agent_id. I could then increment
>>> that value and use that pair for new agent creation. However, I wonder
>>> if thee is a way to do that automatically in SQL?
>>>
>>> Shelly
>>>

>>
>>

>
>



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      09-22-2007
"Shelly" <> wrote in message
news:...
>
> "Just Me" <news.microsoft.com> wrote in message
> news:Ogr6xxF$...
>> Well, to get the lastAgentID number you could
>>
>> Select max(AgentID) From Agent

>
> Like I said, I know I can do this (adding, of course, the clause WHERE
> accountNumber=theaccountNumber) , but my question is can SQL do this
> automatically on the insert statement into the Agent table? If it can,
> then I can do an ExecuteScalar and return that value from the insert query
> without having to do two queries in succession with the possiblity of a
> race condition where someone else is adding an agent to the same account
> at the same time.
>
> Shelly
>
>>
>>
>>
>>
>>
>> "Shelly" <> wrote in message
>> news:...
>>> This is more of an SQL question, but I would like to know how to do it
>>> in SQL Server.
>>>
>>> In a given table I have two columns of importance. The first is account
>>> number (and it is a foreign key). The other is agent_id. The
>>> combination must be unique. What I would like to do is to autoincrement
>>> the agent_id for a given account number. Example:
>>>
>>> Account Number Agent ID
>>> 1000 1
>>> 1000 2
>>> 1100 1
>>> 1200 1
>>> 1200 2
>>> 1200 3
>>>
>>> When I add a new agent to account number 1200, I would like it to come
>>> up automatically with 2 for the agent_id.. I know I could do a select
>>> on account number and return MAX of agent_id. I could then increment
>>> that value and use that pair for new agent creation. However, I wonder
>>> if thee is a way to do that automatically in SQL?
>>>
>>> Shelly
>>>

>>
>>

>
>




--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      09-22-2007
"Shelly" <> wrote in message
news:...

> Like I said, I know I can do this (adding, of course, the clause WHERE
> accountNumber=theaccountNumber) , but my question is can SQL do this
> automatically on the insert statement into the Agent table? If it can,
> then I can do an ExecuteScalar and return that value from the insert query
> without having to do two queries in succession with the possiblity of a
> race condition where someone else is adding an agent to the same account
> at the same time.


Do you mean something like this...?

<insert the record into the first table>
INSERT Agent (ID, field2, field3,...) VALUES (@@IDENTITY, value2,
value3,...)


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Reference, SQL Queries, SQL help ecoolone ASP .Net 0 01-03-2008 10:58 AM
Do the Self-Paced Training Kits: Microsoft SQL Server 2000 include Eval copy of SQL Server? Brian Whiting Microsoft Certification 2 12-29-2005 04:24 AM
DBI SQL column datatype not jiving with SQL statement requirement dna Perl 1 01-18-2004 04:15 PM
Re: SQL statement working in SQL Server but not in .aspx.cs page William \(Bill\) Vaughn ASP .Net 0 08-21-2003 10:41 PM
Newbie SQL question: Break SQL into different SELECTs or keep as one Jules ASP General 6 07-15-2003 12:47 PM



Advertisments