如何将参数传递给MSSQL查询?

时间:2017-08-07 10:55:56

标签: sql-server

我正在做一个MSSQL函数,它从给定的数据集中计算Cronbach的alpha。这是我目前的代码:

DROP PROCEDURE IF EXISTS alpha;
GO
CREATE PROCEDURE alpha
AS
BEGIN
    EXEC sp_execute_external_script
    @language = N'R'
    , @script = N'library(psych);
    a = tryCatch(alpha(Test), warning = function(w) {alpha(Test,check.keys=TRUE)})
    str(a)'
    , @input_data_1 = N'SELECT 
    CAST([ky1] as float) as ky1, 
    CAST([ky2] as float) as ky2,    
    CAST([ky3] as float) as ky3,    
    CAST([ky4] as float) as ky4,    
    CAST([ky5] as float) as ky5,    
    CAST([ky6] as float) as ky6,    
    CAST([ky7] as float) as ky7,    
    CAST([ky8] as float) as ky8,    
    CAST([ky9] as float) as ky9,    
    CAST([ky10] as float) as ky10,  
    CAST([ky11] as float) as ky11,  
    CAST([ky12] as float) as ky12,  
    CAST([ky13] as float) as ky13,  
    CAST([ky14] as float) as ky14,  
    CAST([ky15] as float) as ky15,  
    CAST([ky16] as float) as ky16,  
    CAST([ky17] as float) as ky17,  
    CAST([ky18] as float) as ky18,  
    CAST([ky19] as float) as ky19,  
    CAST([ky20] as float) as ky20 
    FROM Vuodet'
    , @input_data_1_name = N'Test'
END;
GO
EXEC alpha;

如何更改用户可以从他或她想要的字段而不是从给定字段ky1到ky20计算alpha的代码?

1 个答案:

答案 0 :(得分:0)

解决方案是添加

@input_data_1 = @input

到剧本。