How to write stored procedure to update the data in 10
tables
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
What's the maximum size of a row?
can i write function in stored procedure and stored procedure in function and nested procedure.Give one example for each question?
What the class forname () does?
List the types of recovery model available in sql server?
1.what is the difference between view and cursor? 2.If we do any change in view will it affect the database,similarly when we do changes in cursor will it affect the databse?with certain example?Thanks
How to generate create table script on an existing table in ms sql server?
How to use the inserted and deleted pseudo tables?
Can we delete data from a view?
Is it possible to have more then one foreign key in a single table? if possible, is this the good way to design the table?
What types of replication are supported in sql server?
How to get number of days in a given year?
how to find number of columns in a table in sql server 2000 and 2005 also