Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2004, 11:03 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default complex insert statement?

im not that experienced at sql..
anyways this is the problem im facing ..

Im indicating the primary keys in each table along with the table with a '-'

ive 1 Main Table(Invoices - InvoiceGID) and 3 Other Tables
Companys - CompanyGID;
Branches - BranchGID;
Orders - OrderGID

The 3 Three Tables are related to the main Invoice table using these 3 relationship tables
CompanyInvoice - CompanyFID,InvoiceFID;
BranchInvoice - BranchFID,InvoiceFID;
OrderInvoice - OrderFID,InvoiceGID


CompanyGID is linked to CompanyFID & InvoiceGID is linked to InvoiceFID in the relationship table
CompanyInvoice

Now consider im entering a row into the Invoice Table (The Primary Autogenerated key is InvoiceGID)
for a BranchGID = 345, I can insert the data into Invoice Table fine.. but how do i relate the
BranchFID with the new Invoice Table Row in the Relationship table BranchInvoice.

I mean how do i put the value of BranchFID(345) & InvoiceFID(now autogenerated whose value i dont know)
into the relationship table while inserting the new row into Invoice table itself

how do i do this .. or is there some other way or is there a problem with how i understood the concept

if im not clear at explaining the problem pls do tell ...

 
Old November 1st, 2004, 01:07 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If I've understood then you need to use @@IDENTITY which gives you the autogenerated ID:
Code:
DECLARE @NewId INT
-- First insert
INSERT INTO tblInvoice data1, data2...
SET @NewId = @@IDENTITY
-- Second insert
INSERT INTO tblCompanyInvoice @NewId, data1, data2...
-- Other inserts...
--

Joe
 
Old November 2nd, 2004, 01:02 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx joe, i'll try this out..

one more thing .. ive explained my problem and asked for a solution which i thought was the best way to use the relationships factor in tables

now is there a better way to do this or is my understanding of the relationship table wrong?
 
Old November 2nd, 2004, 04:12 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

joe one more thing ...

consider if im using an asp.net page as front end with more than 25~30 people using the function at the same time. it wont be a problem of using this sql statement(im not familiar with locking and its areas) ie. i wont get the InvoiceGID of some other person who added this at the same instant right?

mebe the qs is irrelevant to this point or mebe i dint get the working of the sql identity statement .. but do give an idea..






Similar Threads
Thread Thread Starter Forum Replies Last Post
Complicated Insert Statement jackie25 SQL Server ASP 1 May 31st, 2006 09:08 AM
Insert Statement Iashia06 Access 0 April 7th, 2006 12:45 PM
What is wrong with this insert statement? method SQL Server 2000 13 April 27th, 2005 11:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.