473,761 Members | 8,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert after running Select query in Stored Procedure

2 New Member
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.UserI d, 7)

How do I do this?
Aug 9 '07 #1
2 9036
azimmer
200 Recognized Expert New Member
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.UserI d, 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
DotNetDanjo
2 New Member
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
9011
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 learning how to pass these varibables from ASP to the SP.
1
8347
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 stored procedure. But in the FROM-clause a stored procedure is not allowed?
4
41598
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 do my insert into 3 different table all using the same uniqueID. I can't use the @@identity function because my application uses a connection pool and it's not garanteed that a connection won't be used
0
4492
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 procedure from SQL Server vs MS Access point of view ?
2
3334
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 several stored procedures to Insert and update datas in our SQL database. Some stored procedures are smaller (insert datas in only one table) and some of them are quite big (insert datas in several
8
5446
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
2082
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 or the illegal chars for the insert command is it possible to encasulate the text/string so the server doesnt reads the string as a command?
6
9906
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! Peter
0
1681
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, dbo.salary_head.amount, dbo.grade_level., dbo.employees.employ_name, dbo.employees.work_id, dbo.employees.company_id, dbo.employees.designation, dbo.salary_head.level_id, dbo.employees.terminate,...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.