将列名称声明为变量

时间:2018-12-07 17:15:37

标签: sql sybase

我在以下查询中遇到问题。

DECLARE @CC VARCHAR(100)
SET @CC = 'MKT_COV_CAR_LFR'
DECLARE @SQL VARCHAR(16384)
SET @SQL =      
'INSERT INTO PP_Industry
SELECT
    R.Year,
    ''01'' AS Carline,
    Sales_Locality,
    '+@CC+' AS Reg, 
    National_Reg,
    CAST('+@CC+' AS FLOAT) / CAST(National_Reg AS FLOAT) AS Reg_Share
FROM
    PP_Registrations AS R
    JOIN
        (
            SELECT
                Year,
                SUM('+@CC+') AS National_Reg
            FROM
                PP_Registrations
            GROUP BY
                Year
        ) AS SQ ON R.Year = SQ.Year'

EXEC @SQL

运行它时收到以下错误。请帮忙!

  

错误(2812)存储过程'INSERT INTO PP_Industry       选择           R.Year,           '01'AS Carline,           Sales_Locality,           MKT_COV_CAR_LFR AS Reg,           找不到Nationa。指定owner.objectname或使用sp_help检查对象是否存在(sp_help可能会产生大量输出)。

1 个答案:

答案 0 :(得分:0)

如果您运行自定义SQL语句,则应这样执行:

class SomeVariables{
    String s;
    int dontneed;
}
class MainClass{
    // this is ok
    SomeVariables vars= new SomeVariables();
    // not allowed here, must be on method, main for example
    vars.s = "why this doesnt work?. IDE says Uknown class 'vars.s'";
    // not allowed here, must be on method, main for example
    System.out.println(vars.s);        // Accesing it also doesnt work

    void ChangeValue(){
        // it works because is on scope and inside a method
        vars.s = "why does this work?";
    }

    public static void main(String[]args){
        // here sholud be your statements var.s = ... and System.out.println
    }
}