开发者

c# How do i use a SqlParameters as table or column names?

开发者 https://www.devze.com 2023-01-22 09:20 出处:网络
How 开发者_开发技巧can i use a SQLParameter as as the table name and column name? SELECT * FROM @TableName Where @ColumName=@Value

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.


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号