ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
How to write stored procedure to update the data in 10 
tables
 Question Submitted By :: Tester
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to write stored procedure to update the data in 10 tables
Answer
# 1
create procedure proc_name 
{
@para1 varchar(20),
@para2 varchar(20),
}
AS
Declare @sql varchar(8000)
Set @sql="update table1 set col1='value' where 
col2='"+@para1+"'\n"
Set @sql=@sql+ "update table2 set col1='value' where 
col2='"+@para2+"'\n"

Exec(@sql)
 
Is This Answer Correct ?    3 Yes 3 No
Vaishali
 
  Re: How to write stored procedure to update the data in 10 tables
Answer
# 2
Hey Vaishaili,
The query you wrote first of all does not require a dynamic 
string to be constructed and then executed. The problem 
with this is you are defeating the purpose of a stored 
procedure. 

A stored procedure is basically used to make the execution 
faster as the code is kept in pre-complied mode. 

Here what you are doing is you are giving an 'EXEC' 
statement to execute the query which means that 
the 'update'statements are not kept in pre-compiled mode. 
They will first compile and then execute just like a normal 
SQL statement.
 
Hence you should directly write the 'UPDATE' statements so 
that it should only be executed and not compiled as they 
are already kept in a pre-compiled mode.

The procedure can be best written as follows:

CREATE PROCEDURE <<procName>>
(
   @param1 varchar(20),
   @param2 varchar(20)
)
AS
UPDATE <<tableName1>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2

UPDATE <<tableName1>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2

..
..
..
..

UPDATE <<tableNameN>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2



Exec(@sql)
 
Is This Answer Correct ?    5 Yes 0 No
Mohit Johri
 
 
 
  Re: How to write stored procedure to update the data in 10 tables
Answer
# 3
Kindly ignore the last statement in my posted answer which 
says 'Exec(@sql)' it was wrongly copied
 
Is This Answer Correct ?    3 Yes 2 No
Mohit Johri
 
  Re: How to write stored procedure to update the data in 10 tables
Answer
# 4
CREATE PROCEDURE <<procName>>
(
   @param1 varchar(20),
   @param2 varchar(20)
)
AS
UPDATE <<tableName1>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2

UPDATE <<tableName1>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2

..
..
..
..

UPDATE <<tableNameN>> SET <<colName1>> = @param1 WHERE 
<<colName2>> = @param2
 
Is This Answer Correct ?    1 Yes 1 No
Jaipal
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
What is the use of DBCC commands?  1
What is the use of CASCADE CONSTRAINTS?  2
What is the purpose of using COLLATE in a query?  1
what is the out put of below queries? a. select * from Emp where null = null; b. select * from Emp where 1=1; Patni9
Can we create a clustered index on composite primary key.  2
Advantages and Disadvantages of Cursor? Zenith10
What is bit datatype and what's the information that can be stored inside a bit column?  1
What is WITH CHECK OPTION Karur-Vysya-Bank-KVB2
From where can you change the default port?  3
when inserting to a table how many rows will be effected using triggers  1
How m-m relationships are implemented?  1
What is the difference between 2-Tier architecture and 3-Tier architecture Oracle10
How do SQL server 2000 and XML linked?  2
What's the maximum size of a row?  4
Which stored procedure will you be running to add a linked server?  1
write the query for taking database restore in sql?  2
Let us say master db itself has no backup. Now you have to rebuild the db so what kind of action do you take?  1
what is the maximum size of a row in sql server 2000 and 2005  2
Say if we have a table that contains only a single column , say OrderID, which has IDENTITY attribute defined on it. So how can we insert data in this table. I am reframing my question, that how can we make the table to increment the column "OrderID" value several times???  3
What is MSDE?  2
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com