tags:

views:

30

answers:

2

How can i use a SQLParameter as as the table name and column name?

SELECT * FROM @TableName Where @ColumName=@Value

I get errors when i try to do something like that.

A: 

you'd have to use dynamic sql, which is generally not recommended.

Basically, you'd have to treat your sql like a string, append the variable values you want to include in the sql, then use exec sql to execute it.

Beth
Won't work unless concatenating strings - dynamic SQL will enclose the names in single quotes
OMG Ponies
maybe I should have said 'concatenate' instead of 'append'. I meant by using the + operator.
Beth
A: 

Dynamic SQL -

declare @TableName as varchar(20)
declare @ColumnName as varchar(20)
declare @Value as varchar(20)
declare @dynsql as varchar(200)
--Set the values before
SET @dynsql='Select * from '+@TableName+'Where '+@ColumnName+'='+@Value
execute(@dynsql)
Misnomer