SQL Server - two queries(Insert and update) in one stored procedure

Asked By http://taanu51.blogspot.com/ Dotnet Toolkit
11-Jan-11 10:07 PM
HI...
I am using C# and sqlserver 2005 i would like to write two queries(Insert and update) in one stored procedure..i have a registration page in this when i entered a new record(ex: 11th record) and click the button then the new record will be successfully insert at the same time i have to update the email address which i entered in the new record this email address field will be update in the last 10 records simantanesly...pls help me...
  Jithin AK replied to http://taanu51.blogspot.com/ Dotnet Toolkit
11-Jan-11 10:38 PM
If this needs to be done for each entry in the table, you can use a  trigger in this scenario after the insert, 
  Sundarasegaram Veerakuddy replied to http://taanu51.blogspot.com/ Dotnet Toolkit
11-Jan-11 10:39 PM
Hi,

Not sure why last 10 rows... Anyway,

Do you have any auto-incremented column or auto-generated system date column in your table?

If so your Procedure would do some thing like,

INSERT INTO <table> ...

and then,

UPDATE <table> SET <email column> = <email value> WHERE <auto-incremented column> IN (SELECT TOP 10 <auto-incremented column> FROM <table> ORDER BY 1 DESC)

Is this making any sense?

  http://taanu51.blogspot.com/ Dotnet Toolkit replied to Sundarasegaram Veerakuddy
11-Jan-11 11:18 PM
Thanks for your reply..not for 10 rows i have to update company email id in the previously entered records...
  Rohan Dave replied to http://taanu51.blogspot.com/ Dotnet Toolkit
11-Jan-11 11:28 PM
you mean if new record insert into the database then you need to update the company email for previous record with the email you have entered.

Suppose you entered new record (ex 15th record) then which record company email id will be update ? 14th record's company email id ?
  Vivek Jagga replied to http://taanu51.blogspot.com/ Dotnet Toolkit
11-Jan-11 11:58 PM
Tanisha,

A stored procedure is called and the data needs to be updated if it already exists and inserted if it does not.  If we refer to the Books Online documentation, it gives examples that are similar to:

IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
    UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
    INSERT INTO Table1 VALUES (...)

This approach does work, however it might not always be the best approach.  This will do a table/index scan for both the SELECT statement and the UPDATE statement.  In most standard approaches, the following statement will likely provide better performance.  It will only perform one table/index scan instead of the two that are performed in the previous approach.

UPDATE Table1 SET (...) WHERE Column1='SomeValue'
IF @@ROWCOUNT=0
    INSERT INTO Table1 VALUES (...)

The saved table/index scan can increase performance quite a bit as the number of rows in the targeted table grows.

Just remember, the examples in the MSDN documentation are usually the easiest way to implement something, not necessarily the best way.  Also (as I re-learned recently), with any database operation, it is good to performance test the different approaches that you take.  Sometimes the method that you think would be the worst might actually outperform the way that you think would be the better way

  http://taanu51.blogspot.com/ Dotnet Toolkit replied to Rohan Dave
12-Jan-11 12:00 AM
when i entered the company email id in the 15th(new record)..then i have to update all the previously entered company email id in the old records with this newly entered email id...
  Rohan Dave replied to http://taanu51.blogspot.com/ Dotnet Toolkit
12-Jan-11 12:18 AM
you can do this by first store/retrieve the value of comapny email id of newly entered row. Then you can execute Update query to update all the company email id of previously entered record by not setting any Where condition in Update query.

see the below sample code :

I assume you have some ID field in your table with set Identity/Autoincrement. So your ID will be generated automatically. Consider you have 14 record in your table and you are inserting 15th new record.

Insert Into yourtable(companyname, companyemail) Values ('abc Company', 'abc@gmail.com')

SET @CompanyEmail = (Select Top(1) companyemail From yourtable Order By ID Desc)

Update yourtable SET companyemail = @CompanyEmail

So it will update all the previously entered CompanyEmail id with new email id..

  Sundarasegaram Veerakuddy replied to http://taanu51.blogspot.com/ Dotnet Toolkit
12-Jan-11 05:19 AM
Hi,

So it is simple. As you will carry Email value to the Procedure for Inserting purpose, it will do the following:

(a) INSERT INTO <table> ...
(b) UPDATE <table> SET <email column> = <email value>

Above Update statement will update all rows including your new row added.
Create New Account
help
How can I decrypt a SQL Server stored-procedure? SQL Server hi Please help on this How can I decrypt a SQL Server stored-procedure? thx guru SQL Server Programming Discussions SQL Server (1) Stored procedure (1) Stu (1
Additonal thoughts, you should be able to connect to SQL Server via SQL Server SQL Server Additonal thoughts, you should be able to connect to SQL Server via SQL Server Management Studio. This will provide you with whether or not SQL Server
Can't login to SQL Server SQL Server Hello, I'm trying to migrate one SQL Server 2000 database to SQL Server Express, and I'm having the following issue. In the SQL Server 2000 database I
Alias for Linked SQL Server? SQL Server Hi, I'm trying to connect to a linked SQL Server 2008 from my local SQL = Server 2008. Everything seems to work fine so far but I'm having the following = problem
Accessing data on a server? SQL Server I have SQL Server developer edition installed on one computer and SQL Server Express Edition install on another computer. When I try to "Attach" a database located on my file server, SQL Server does not "see" the network drive. Is there anyway to get a local