473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Insert after running Select query in Stored Procedure

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
2 9020
azimmer
200 Expert 100+
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
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

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

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
1
by: Harald | last post by:
Hi, is it possible to create an "INSERT INTO ..... "Select from stored procedure" Query? I want to create an temporary table. In this table I want to enter the data, which I can get from an...
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
0
by: ImraneA | last post by:
Hi there I had pleasure of upsizing Access v97 db to Access v2K/SQL 2K. Wish to provide some knowledge gained back to community - hopefully help others. 1.Question how do you test stored...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
8
by: John Smith | last post by:
Hello all: Is there a way to determine the auto increment id from a newly inserted record in the database? Thanks, John
6
by: Lelle | last post by:
Hello ! how can i insert text containg code examples from a textbox into a database using SQL insert statment. i have no problem to just add text that dont contains code and script examples...
6
by: Peter Neumaier | last post by:
Hi, I am trying to select some data through a stored procedure and would like to store the result in a local access table. Is that possible? Can somebody provide an example? Thanks&regards!...
0
by: charlesg | last post by:
I have a query that select rows from the employees,salary_head and salary_group tables this is the query SELECT TOP 100 PERCENT dbo.salary_head.salary_group_id, dbo.salary_group.salary_group,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.