小编SyD*_*SyD的帖子

SQL变量作为Where子句中的列名

我需要一些SQL逻辑方面的帮助,而且我已经工作(并且研究)了2天,但没有成功.

我的目标是尝试将ASP页面中的变量传递给存储过程,该过程将该变量用作where子句中列名的条件.

例如(我的查询的简化版本):

@strDept nvarchar(10), @strUser nvarchar(30)
-- The asp page will pass f18 to @strDept & Ted Lee to strUser
-- f18 is the column name in my database that I need in the where.

select x, y, z from table1 where @strDept in (@strUser)
-- and this is the select statement, notice the where clause.
Run Code Online (Sandbox Code Playgroud)

存储过程确实执行,但它不返回任何值,我知道它将@strDept视为文字nvarchar而不是列名.

所以我想我的问题是,如何让SQL Server 2005将@sqlDept变量视为列名?

sql t-sql sql-server

4
推荐指数
2
解决办法
5万
查看次数

标签 统计

sql ×1

sql-server ×1

t-sql ×1