Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how do i overcome error: Must declare the table variable "xxx" in stored proc.

Posted on 2009-04-30
6
Medium Priority
?
399 Views
Last Modified: 2012-08-13
I am tryinhg to create a stored procedure, i need to pass 2 variables (one of whicih is a table name)
and return a result set.
when i try to create the stored procedure i get the following error message: Must declare the table variable "@Plan".

Thanks,
CREATE PROCEDURE [dbo].[georouteplan] 
(
    @Plan as varchar(25),
    @acc as varchar(6)
)
AS
BEGIN
SELECT d.Name, r.Destination 
FROM georoute.dbo.routing r, @Plan d, georoute.dbo.customer c
WHERE r.routingid = d.id 
AND c.id = r.customerid 
AND c.customerfk = @acc
group by d.Name, r.Destination;
RETURN
END
GO

Open in new window

0
Comment
Question by:roblaw84
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24271427
Hello roblaw84,

CREATE PROCEDURE [dbo].[georouteplan]
(
    @Plan as varchar(25),
    @acc as varchar(6)
)
AS
BEGIN
      EXEC ( 'SELECT d.Name, r.Destination
                  FROM georoute.dbo.routing r, ['+@Plan+'] d, georoute.dbo.customer c
                  WHERE r.routingid = d.id
                  AND c.id = r.customerid
                  AND c.customerfk = @acc
                  group by d.Name, r.Destination;' )
END
GO

Regards,

Aneesh
0
 
LVL 12

Expert Comment

by:udaya kumar laligondla
ID: 24271441
u are trying to use a variable as a table. you need to correct line 9
0
 

Author Comment

by:roblaw84
ID: 24271494
Thanks,
i have ammened the tabel varible reference on line 9 however when i try to execute the stored procedure i get: Must declare the scalar variable "@acc".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24271646
you will need sp_executesql

CREATE PROCEDURE [dbo].[georouteplan] 
(
    @Plan as varchar(25),
    @acc as varchar(6)
)
AS
BEGIN
declare @sql nvarchar(4000) 
set @sql = '
SELECT d.Name, r.Destination 
FROM georoute.dbo.routing r, [' + @Plan + '] d, georoute.dbo.customer c
WHERE r.routingid = d.id 
AND c.id = r.customerid 
AND c.customerfk = @acc
group by d.Name, r.Destination' 

exec sp_executesql @sql, N'@acc varchar(6)', @acc
END
GO

Open in new window

0
 
LVL 12

Expert Comment

by:udaya kumar laligondla
ID: 24271650
can u post the query
0
 

Author Closing Comment

by:roblaw84
ID: 31576493
Excellent thanks for your help! you've saved me from pullingmy hair out.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question