By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Insert after running Select query in Stored Procedure

P: 2
I want a stored procedure to run a select query on it's own like this:
Select *, user_id As UserId FROM CC_host.dbo.usr_table Where(email LIKE N'%@%usa.com')

But from these results I want the same stored procedure to INSERT any new records (user_id as primary key) into the next table..

If Not Exists (Select [user_id] As UserId2 From Usr_Type_Data WHERE UserId2 = UserId)
INSERT INTO Usr_Type_Data (user_id, CustomerTypeId) VALUES(t1.UserId, 7)

How do I do this?
Aug 9 '07 #1
Share this Question
Share on Google+
2 Replies


azimmer
Expert 100+
P: 200
I want a stored procedure to run a select query on it's own like this:
Select *, user_id As UserId FROM CC_host.dbo.usr_table Where(email LIKE N'%@%usa.com')

But from these results I want the same stored procedure to INSERT any new records (user_id as primary key) into the next table..

If Not Exists (Select [user_id] As UserId2 From Usr_Type_Data WHERE UserId2 = UserId)
INSERT INTO Usr_Type_Data (user_id, CustomerTypeId) VALUES(t1.UserId, 7)

How do I do this?
There are two ways I can think of: you either create a cursor and iterate it, or - better yet - do it in one go:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Usr_Type_Data
  2. SELECT user_id, 7
  3. FROM CC_host.dbo.usr_table usr
  4. WHERE (usr.email LIKE N'%@%usa.com')
  5. AND usr.user_id not in (SELECT user_id from Usr_Type_Data)
  6.  
Aug 9 '07 #2

P: 2
There are two ways I can think of: you either create a cursor and iterate it, or - better yet - do it in one go:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Usr_Type_Data
  2. SELECT user_id, 7
  3. FROM CC_host.dbo.usr_table usr
  4. WHERE (usr.email LIKE N'%@%usa.com')
  5. AND usr.user_id not in (SELECT user_id from Usr_Type_Data)
  6.  

WOW!!! Awesome skills dude!! :) Let me give it a try. :)
Aug 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.