Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我需要在存储过程的select语句中使用变量传递列名,但我不能使用动态查询_Sql_Sql Server_Database_Sql Server 2008 - Fatal编程技术网

Sql 我需要在存储过程的select语句中使用变量传递列名,但我不能使用动态查询

Sql 我需要在存储过程的select语句中使用变量传递列名,但我不能使用动态查询,sql,sql-server,database,sql-server-2008,Sql,Sql Server,Database,Sql Server 2008,下面是我的SQL查询。我想从作为变量给出的列名中选择值。除了使用动态查询之外,有没有其他合适的方法来实现这一点 SELECT EPV.EmployeeCode, @RateOfEmployee, @RateOfEmployer FROM [HR_EmployeeProvisions] EPV 不使用动态sql的一种方法是使用CASE语句 但这很难看 SELECT EPV.EmployeeCode, case @RateOfEmployee when 'RateOfEmployee' then

下面是我的SQL查询。我想从作为变量给出的列名中选择值。除了使用动态查询之外,有没有其他合适的方法来实现这一点

SELECT EPV.EmployeeCode, @RateOfEmployee, @RateOfEmployer
FROM [HR_EmployeeProvisions] EPV

不使用动态sql的一种方法是使用CASE语句

但这很难看

SELECT EPV.EmployeeCode, case @RateOfEmployee  when 'RateOfEmployee' then RateOfEmployee
when 'X' then X 
..
end , case @RateOfEmployer  when 'RateOfEmployer' then RateOfEmployer
when 'Y' then Y
..
end 
FROM [HR_EmployeeProvisions] EPV
您必须检查
CASE
语句中的所有列。

您不能在Sql server中进行参数化,我怀疑它在任何其他关系数据库中都是可能的

最好的选择是使用动态Sql

请注意,动态sql通常是一种安全隐患,您必须保护代码免受攻击

我可能会这样做:

Declare @Sql nvarchar(500)
Declare numberOfColumns int;

select @numberOfColumns = count(1)
from information_schema.columns
where table_name = 'HR_EmployeeProvisions'
and column_name IN(@RateOfEmployee, @RateOfEmployer)

if @numberOfColumns = 2 begin

Select @Sql = 'SELECT EmployeeCode, '+ QUOTENAME(@RateOfEmployee) +' ,'+ QUOTENAME(@RateOfEmployer) +
'FROM HR_EmployeeProvisions'

exec(@Sql)
end
通过这种方式,您可以确保列名实际存在于表中,并将其用作另一个安全层

注意:在表示层中,您应该处理这样一个选项,即由于列名无效,将不会执行select。

查看一下子句-我不确定它是否适用于您的情况,但在某些情况下,它可以用于在不使用动态SQL的情况下按列名查询值:

create table t1 (
  a int,
  b int,
  c int
);

insert into t1 values
(1, 11, 111),
(2, 22, 222),
(3, 33, 333);

select a, col_name, col_value from t1
unpivot (col_value for col_name in (b, c)) as dt;
结果:

| a | col_name | col_value |
|---|----------|-----------|
| 1 |        b |        11 |
| 1 |        c |       111 |
| 2 |        b |        22 |
| 2 |        c |       222 |
| 3 |        b |        33 |
| 3 |        c |       333 |
()


如果您仅需要
a
中的值,这取决于
b
c
上的某些条件(动态),则可以在此基础上构建条件。如果需要列
b
c
中的值,可以添加
。。。其中col_name=?
。如果您需要更多的列,您可能需要过滤未透视表上的列值,而不是再次透视,以便将值返回到列中。

mysql或sql server?感谢您的回答,但遗憾的是,我不能使用此选项。@Prdp实际上,我认为您的回答在列不多的情况下同样好。如果表格有很多列,那就太麻烦了。@MuhammadBilal为什么不?@MuhammadBilal-开始解释你的问题,而不是说我不能使用/它没有使用work@Prdp我已经探索够了。我的场景不允许我使用动态查询,可能与PIVOT相关的内容会有所帮助。我很感激你的回答,但不幸的是,这不是我想要的。我已经试过了,但它对我不起作用。此外,它还通过为每列添加case语句使查询变得相当冗长。谢谢你的回答。:)我已经尝试过了,但是我在其他几个语句中使用了UNIONALL,所以我不能在我的场景中添加它。我已在查询中使用此CASE语句。:)你能举个例子说明unpivot在这种情况下能起到什么作用吗?我现在明白你的意思了。美好的