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
Is a primary key unique?
Explain about analysis services?
Define tempdb database?
What is difference between primary key and foreign key?
Define self join in sql server joins?
Explain the steps to create and execute a user-defined function in the sql server?
What is bcnf normalization form?
What is nonclustered index on computed columns?
What is indexing in sql server with example?
What is the difference between stored procedure and functions?
What are data files?
What are examples of triggers?
How does using a separate hard drive for several database objects improves performance right away?
List the different normalization forms?
How to return the top 5 rows from a select query in ms sql server?