SQLTeam.com | Weblogs | Forums

INSERT and working with 2 tables

Hello
I have some problems

I have 2 tables
Table 1 - groups
Table 2 - users

I need to INSERT a new RAW for every USER from table USERS
but
I will need to grab value ID from table user and put it in OWNERID in table 1
also every single ID in table 1 will be the same

I am not sure how to run the script, I tried many different ways

this may be clear to you, but that's it. Can you provide DDL and Sample data and we can help? Create table Table1..... Create table table 2... insert into ....

1 Like

And expected results would be needed - it isn't clear what you are expecting. We have no idea what RAW means.

The normal approach for inserting new rows into a table based on data already in that (or other tables) is to build a SELECT statement that returns the desired results. Once you have that it is simply:

INSERT INTO dbo.MyTable (list of columns)
SELECT {column list}
  FROM dbo.MyTable
 WHERE {conditions};

If this is a situation where you need to insert into one table - and the insert process generates a new ID value (identity) - you can use the OUTPUT clause on the first insert to return that information for use in the second insert statement.

1 Like

Could you please post what you tried already?
We have no data samples to help you with. So either give us full access to your server (joke) or provide us sample data