SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Mulitple Stored Procedures Declares
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Johnph
Yak Posting Veteran

95 Posts

Posted - 02/25/2013 :  16:50:39  Show Profile  Reply with Quote
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;

Edited by - Johnph on 02/25/2013 16:51:13

James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 02/25/2013 :  17:20:52  Show Profile  Reply with Quote
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
Yak Posting Veteran

95 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/26/2013 :  00:53:56  Show Profile  Reply with Quote
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
Yak Posting Veteran

95 Posts

Posted - 02/26/2013 :  16:37:28  Show Profile  Reply with Quote
thank you both!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/26/2013 :  22:34:10  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000