How to write stored procedure to update the data in 10
tables
Answer Posted / 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 |
Post New Answer View All Answers
How to insert data with null values?
How do I determine how many instances of sql server are installed on a computer?
List all the types of user-defined functions?
What do you understand by user-defined function in the sql server and explain the steps to create and execute a user-defined function in the sql server?
what authentication modes does sql server support? : Sql server database administration
explain what is a deadlock and what is a live lock? How will you go about resolving deadlocks? : Sql server database administration
What is a non clustered primary key?
What will be the value of @@fetch_status if a row that was a part of the cursor resultset has been deleted from the database after the time the stored procedure that opened the cursor was executed?
how you can deploy an ssrs report?
What are the aggregate and scalar functions?
What is a raid and what are different types of raid configurations?
Do you know the different ddl commands in sql?
What is the use of commit?
What is the difference between MVC and Teir Architecher? Plz explain with Layyered Programming example...? Thanks
How to generate create procedure script on an existing stored procedure?