Friday, March 9, 2012

Declare a variable in an SP

Hi.
I have this sql query, which works:
INSERT INTO tblac
(type, t_id, startdate)
SELECT
tblac.type, ***182*** AS Expr1, tblac.startdate
FROM
tblac
WHERE
tblac.t_id = @.t_id
This works, but I want to put it into an SP. I can't figure out from
books online, how to replace the integer, 182, with a variable passed to
the SP.
I've tried:
CREATE PROCEDURE copyDates AS
(
DECLARE @.new_t_id int,
@.t_id int
)
INSERT INTO tblac
(type, t_id, startdate)
SELECT
tblac.type, @.new_t_id AS Expr1, tblac.startdate
FROM
tblac
WHERE
tblac.t_id = @.t_id
GO
..
but that doesn't work.
thanks for any help,
Mark
*** Sent via Developersdex http://www.examnotes.net ***Mark wrote:
You need to declare the variables you want to pass (the parameters)
BEFORE the AS.
CREATE PROCEDURE copyDates (@.new_t_id int, @.t_id int) AS
INSERT INTO tblac
(type, t_id, startdate)
SELECT
tblac.type, @.new_t_id AS Expr1, tblac.startdate
FROM
tblac
WHERE
tblac.t_id = @.t_id
GO
HTH,
Stijn Verrept.

No comments:

Post a Comment