Stored Procedure

D

David Way

How do I write a stored procedure that selects and orders
n records from a table and then updates each of the
selected records 1 through n in the order selected
 
M

MGFoster

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Perhaps this? Not too sure of the UPDATE statement. Read the Books on
Line (BOL) for more info on T-SQL syntax for UPDATE w/ SELECT.

CREATE PROCEDURE usp_UpdateNRecords
@nRecords INTEGER,
@update_value VARCHAR -- change data type to whatever you need.
AS
-- Set up a dynamic SQL string that will be EXECuted.
DECLARE @strSQL VARCHAR(500)
SET @strSQL =
'UPDATE table_name
SET column_name = ' + @update_value + '
SELECT TOP ' + @nRecords + ' *
FROM table_name
WHERE -- set up <criteria> as needed
ORDER BY column_name'

EXEC (@strSQL)

The table_name refers to the same table in both the Update & Select
statements.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQTzxqoechKqOuFEgEQI5rgCghcuQFBeDp12Lmj+VoBALzWeU0FwAoP3G
JN3i5d/xv+bgvWk4xcq+fVW5
=UFFV
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top