adspace
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 it possible to have clustered index on separate drive from original table location?
How do I start sql server 2017?
What are the source of constraints?
What is acid mean in sql server?
what is spatial nonclustered index
What is subquery? Explain the properties of a subquery?
How to remove duplicate rows from table except one?
How can you append an identity column to a temporary table?
what is the Ticketing tool used in Wipro technologies at Bangalore...???
How to rebuild the master database?
How to enter binary string literals in ms sql server?
Can one drop a column from a table?
If you're given a raw data table, how would perform etl (extract, transform, load) with sql to obtain the data in a desired format?
You have a stored procedure, which execute a lengthy batch job. This stored procedure is called from a trigger you do not want to slow the data entry process you do not want trigger to wait for this batch job to finish before it completes itself what you can do to speed up the process?
Equi join and non equi join is possible with sql server?