The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MS SQL Development
|
Please help creating this query...
Discuss Please help creating this query... in the MS SQL Development forum on Dev Shed. Please help creating this query... MS SQL Development forum discussing administration, MS SQL queries, and other MS SQL-related topics. SQL Server is Microsoft's enterprise database engine.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 14th, 2003, 12:35 PM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
|
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.
|

November 14th, 2003, 12:50 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111
Time spent in forums: 1 m 3 sec
Reputation Power: 10
|
|
|
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?
|

November 14th, 2003, 12:57 PM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
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
|

November 14th, 2003, 01:02 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111
Time spent in forums: 1 m 3 sec
Reputation Power: 10
|
|
|
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 & "); "
|

November 14th, 2003, 01:21 PM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
|
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
|

November 14th, 2003, 05:15 PM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
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.
|

November 15th, 2003, 10:05 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111
Time spent in forums: 1 m 3 sec
Reputation Power: 10
|
|
|
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.
|

November 17th, 2003, 12:46 AM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
|
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
|

November 17th, 2003, 07:30 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111
Time spent in forums: 1 m 3 sec
Reputation Power: 10
|
|
|
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
|

November 17th, 2003, 01:31 PM
|
 |
MACTEP /\OMACTEP
|
|
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150

Time spent in forums: 6 h 47 m 12 sec
Reputation Power: 11
|
|
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)+1) FROM tblCattle;
INSERT INTO table1 (...values..., myID, ... values)
VALUES (...values..., @newID, ...values);
INSERT INTO table2 (...values..., myID, ... values)
VALUES (...diff. values..., @newID, ...diff values);
SET NOCOUNT OFF;
You have helped more then you know, thanks again.
Lito
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|