Home > Archive > MS SQL Server > August 2005 > specifying the column from a stored procedure parameter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author specifying the column from a stored procedure parameter
Loui Mercieca

2005-08-24, 11:23 am

How do i specify a column using a stored procedure parameter. Whenever i do
this, an error (137) is saying "Must declare variable [varname]"

CREATE PROCEDURE dbo.TestProc
@Test varchar(50),

AS

Select
*
From
@Test

GO



Alejandro Mesa

2005-08-24, 11:23 am

You have to use dynamic sql to accomplish this.

CREATE PROCEDURE dbo.TestProc
@Test varchar(50),
AS
set nocount on

declare @sql nvarchar(4000)

set @sql = N'select * from ' + quotename(@Test)

exec dbo.sp_executesql @sql

return @@error
go

The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html


AMB

"Loui Mercieca" wrote:

> How do i specify a column using a stored procedure parameter. Whenever i do
> this, an error (137) is saying "Must declare variable [varname]"
>
> CREATE PROCEDURE dbo.TestProc
> @Test varchar(50),
>
> AS
>
> Select
> *
> From
> @Test
>
> GO
>
>
>
>

Jens Süßmeyer

2005-08-24, 11:23 am

Thats not working for 2000, you have to use dynamic Sql (BTW. what you want
to do is to parameterize a table not a column)
--
HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---


"Loui Mercieca" wrote:

> How do i specify a column using a stored procedure parameter. Whenever i do
> this, an error (137) is saying "Must declare variable [varname]"
>
> CREATE PROCEDURE dbo.TestProc
> @Test varchar(50),
>
> AS
>
> Select
> *
> From
> @Test
>
> GO
>
>
>
>

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com