Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Mulitple Stored Procedures Declares

Author  Topic 

Johnph
Posting Yak Master

103 Posts

Posted - 2013-02-25 : 16:50:39
I have a stored procedure that needs to run multiple times. I am confused on what to actually do.

I am getting this error:
The variable name '@Test' has already been declared. Variable names must be unique within a query batch or stored procedure.


My code looks like this... I dont want to run this 100 times, and instead want to run it on one script.

DECLARE @TEST as VARCHAR(30)
SET @TEST = '234'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;


--------------------------------------------
DECLARE @TEST as VARCHAR(30)
SET @TEST = '123'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-25 : 17:20:52
You can do one or the other of the following
------------ OPTION 1 ----------------
DECLARE @TEST as VARCHAR(30)
SET @TEST = '234'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;

GO

--------------------------------------------
DECLARE @TEST as VARCHAR(30)
SET @TEST = '123'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;


------------ OPTION 2 ----------------

DECLARE @TEST as VARCHAR(30)
SET @TEST = '234'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;


--------------------------------------------
--DECLARE @TEST as VARCHAR(30)
SET @TEST = '123'

INSERT INTO TABLE1(A, B)
SELECT TOP 1 @TEST, max(CAT) FROM TABLE2 where 1=1;
Go to Top of Page

Johnph
Posting Yak Master

103 Posts

Posted - 2013-02-25 : 23:58:26
Hey James, I am giving this a try tomorrow. THanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-26 : 00:53:56
the TOP 1 is redundant in selects as you're already applying max.

it should be

DECLARE @TEST as VARCHAR(30)
SET @TEST = '234'

INSERT INTO TABLE1(A, B)
SELECT @TEST, max(CAT) FROM TABLE2 where 1=1;


--------------------------------------------

SET @TEST = '123'

INSERT INTO TABLE1(A, B)
SELECT @TEST, max(CAT) FROM TABLE2 where 1=1;


you can also dispense with trivial where condition 1=1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Johnph
Posting Yak Master

103 Posts

Posted - 2013-02-26 : 16:37:28
thank you both!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-26 : 22:34:10
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -