How to write stored procedure to update the data in 10
tables

Answers were Sorted based on User's Feedback



How to write stored procedure to update the data in 10 tables..

Answer / mohit johri

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 ?    15 Yes 3 No

How to write stored procedure to update the data in 10 tables..

Answer / mohit johri

Kindly ignore the last statement in my posted answer which
says 'Exec(@sql)' it was wrongly copied

Is This Answer Correct ?    9 Yes 2 No

How to write stored procedure to update the data in 10 tables..

Answer / vaishali

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 ?    10 Yes 11 No

How to write stored procedure to update the data in 10 tables..

Answer / jaipal

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 ?    6 Yes 7 No

Post New Answer

More SQL Server Interview Questions

hi friends please answer this question ASAP:- how to count the no. of employee in a each department or no. of employee in each location by using emp/dept table

7 Answers   Indecomm,


what are the different ways of moving data/databases between servers and databases in sql server? : Sql server database administration

0 Answers  


Where the sql logs gets stored?

0 Answers  


How do you check sql server is up and running?

0 Answers  


How to read 2nd highest sal from EMP table?

5 Answers   IBM, TCS,


Explain about temporary stored procedure?

0 Answers  


How do you start single user mode in clustered installations?

0 Answers  


Define left outer join in sql server joins?

0 Answers  


How to connect of datebase with sql express.?

0 Answers   MCN Solutions,


What is the maximum number of instances in 32 bit and 64 bit sql server 2012?

0 Answers  


What is sql server management studio? : sql server management studio

0 Answers  


What is auditing in sql server?

0 Answers  


Categories