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


Please Help Members By Posting Answers For Below Questions

Is it possible to have clustered index on separate drive from original table location?

1047


How do I start sql server 2017?

1023


What are the source of constraints?

1015


What is acid mean in sql server?

1257


what is spatial nonclustered index

1066


What is subquery? Explain the properties of a subquery?

1070


How to remove duplicate rows from table except one?

1096


How can you append an identity column to a temporary table?

1076


what is the Ticketing tool used in Wipro technologies at Bangalore...???

8237


How to rebuild the master database?

1175


How to enter binary string literals in ms sql server?

1255


Can one drop a column from a table?

1098


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?

1051


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?

1221


Equi join and non equi join is possible with sql server?

1139