MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old November 14th, 2003, 12:35 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
Please help creating this query...

Hello,
I need some help with this query, I have 2 tables...
I need them have a common int field,...
on the first table it is an auto incrementing field called ID, on the second it is called CID, and is suppose to act as a foreign key.
I am using Enterprise manager and MS SQL 2000 database to manage this thing, but to be honest my database skills dont go far from basic inserts, updates, and deletes.

To make this happen I have set up a relationship to my first table like so FK_table2_table1... primary on first table = id and foregn key on second table = CID. The second table could have multiple records with same CID...

What needs to be achieved here is when I do an insert it should update second table and enter CID as the auto gen number from the first table. What do I need to do?

Lito
__________________
Sometimes just a few hours of trial and error debugging can save minutes of reading the manual.

Reply With Quote
  #2  
Old November 14th, 2003, 12:50 PM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
So what I am understanding that you want to do is this:

You have the following tables structure:

table1
-----------
id (autogenerated PK)
other fields


table2
-----------------
table2id(autogenerated PK)
CID (FK to table1)
other fields

You want to update table2 CID when you do an insert into table1? If you insert into table1, which means you would get a new id, wouldn't you be doing an insert into table2 as well, since table1.id = table2.CID?

Why don't you just get the identity (id) field back and do another insert?

Reply With Quote
  #3  
Old November 14th, 2003, 12:57 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
Quote:
Originally posted by jstrohofer
Why don't you just get the identity (id) field back and do another insert? [/B]


you are understanding me correctly, but I can't do what you suggest! This is a web application, which means there is a posibility of more then a single user at any given second updating the same tables, and if I get the (id) field back of a wrong insert, my app is useless. Thanks.

Lito

Reply With Quote
  #4  
Old November 14th, 2003, 01:02 PM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
How would you get the wrong id field back?
When you do the insert do something like this:

strSQL = "SET NOCOUNT ON; INSERT INTO table1(field1, field2) VALUES(" & value1 & ", " & value2 & "); SELECT @@IDENTITY; SET NOCOUNT OFF;"

set objRS = objConn.Execute(strSQL)
dim intID
intID = objRS(0)

and then your only recordset row is that of the identity field...

so now you can say

strSQL2 = "INSERT INTO table2(CID, otherfield) VALUES ( " & intID & ", " & othervalue & "); "

Reply With Quote
  #5  
Old November 14th, 2003, 01:21 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
Thanks it works the way I need it to, I didn't know you could execute multiple statements in same sql string! And what does "SET NOCOUNT ON
/OFF" do?

Thanks alot,
Lito

Reply With Quote
  #6  
Old November 14th, 2003, 05:15 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
Oh I have realized something just now...
and I dont need to get the Identety field but another field and use it as a counter Dont ask why

Im trying this

INSERT INTO tbl1 (myID, 'other', 'values',...) VALUES (MAX(myID)+1, 'other', 'values', ...)

and it doesn't want to work!!!!

I can swear I have done this before.

Reply With Quote
  #7  
Old November 15th, 2003, 10:05 AM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
Ok, so do the insert and pull back the identity, and the do a select of the counter where id = the identity field you pulled back in the insert statement.

Reply With Quote
  #8  
Old November 17th, 2003, 12:46 AM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
Hi, thanks again
I did almost exaclty as you suggested...
SELECT TOP 1 myID FROM myTable ORDER BY myID DESC
and then just add to it.

T y,
Lito

Reply With Quote
  #9  
Old November 17th, 2003, 07:30 AM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
That's really not a good way to do it...

What if someone else adds a record a second later? If you are POSITIVE that won't happen, I guess you could use your method. But the only way of getting back the correct record for sure is using the @@IDENTITY.

J

Reply With Quote
  #10  
Old November 17th, 2003, 01:31 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 6
You are absolutly right!
I am doing the exact thing i wanted to avoid. Becase the DB structure is funky here and I am not authorized to make any significant changes this is my solution...
PHP Code:
 SET NOCOUNT ON;
DECLARE @
newID INT;
SELECT @newID = (MAX(elementID)+1FROM tblCattle
INSERT INTO table1 (...values..., myID, ... values
VALUES (...values..., @newID, ...values);
INSERT INTO table2 (...values..., myID, ... values)
VALUES (...diffvalues..., @newID, ...diff values);
SET NOCOUNT OFF

You have helped more then you know, thanks again.

Lito

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Please help creating this query...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway