Help with transaction; Related tables

I am creating a transaction.   I have done this before with unrelated tables.   However, now I have related table.  Say I have an "organization" table.   Then I also have a "member" table, which has a foreign key to the "organization" table.   If I add a new organization, I need the primary key of that record to then use on the INSERT for the "member" table.   If this was not within a transaction, I would use SELECT Scope_Identity() and .ExecuteScalar to obtain the Primary Key.  

How do I do this same thing within a transaction?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Yes. You can use it in a transaction. Why do you mention .ExecutScalar? Consider using a single T-SQL batch or even better a stored procedure to do this.
HLRosenbergerAuthor Commented:
I mention .ExecuteScalar because that's how I got back the Primary key; that the only way I know how get it.  

So, are you saying I can use SELECT Scope_Identity()  in a transaction?  How exactly?
ste5anSenior DeveloperCommented:

INSERT INTO tableName () VALUES ();


INSERT INTO otherTableName () VALUES ( @ID.. );

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Deepak ChauhanSQL Server DBACommented:
@I would use SELECT Scope_Identity()

Is primary key a Identity column in Organization table ?

if yes then you can take the advantage of Scop_identity() if not then you can use output clause in T-SQl script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HLRosenbergerAuthor Commented:
yes. it's an identity column.
HLRosenbergerAuthor Commented:
How do I do this:  DECLARE @ID INT = SCOPE_IDENTITY();

if it's not a stored proc?
HLRosenbergerAuthor Commented:
I see that I can still do rh.ID = command.ExecuteScalar() even within the transaction

I got it working.  Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.