Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert into existing table but for each group?

Author  Topic 

tech_1
Posting Yak Master

129 Posts

Posted - 2013-01-27 : 18:28:07
I have a large table with existing data.
The joining key/column is "UserID" (varchar)
I want to insert a record in this table but for each UserID found in the table

how can I do this?

There are multiple single userID's in there, each with a different column value which is linked to another table (think of it as a joining reference table for settings for each user)

any ideas?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-27 : 18:47:46
I did not quite follow what you meant by groups - if the data for a given userid already exists, don't you want to do an update rather than insert duplicate rows for the same userid? Can you post the schema for the two (or more tables) that are involved along with some sample data?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-28 : 00:03:52
Sounds like OP wants to replicate some data for each of existing users but add only once per user. Seems like currently table is having multiple records per user.

If thats the case, it should be something like

INSERT INTO YourTable (UserID,.. other columns)
SELECT UserID,
value1,value2,..
FROm (SELECT DISTINCT USerID FROM YourTable)t


if this was not your question then please post some sample data as James suggested as we cant make out much from your current explanation.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

srimami
Posting Yak Master

160 Posts

Posted - 2013-01-29 : 08:12:17
If I am not wrong, you wanted to lookup for userid's existing on other table in your join condition and for each userid it matched, you wanted to insert values. Let us know in detail.
Go to Top of Page
   

- Advertisement -