SQL Server:IF ELSE条件不起作用

时间:2018-03-23 15:56:41

标签: sql sql-server

declare @FIRST_MONTH_TABLE varchar(50) 
declare @CURRENT_MONTH_TABLE varchar(50)
declare @CURRENT_YEAR varchar(50)

set @FIRST_MONTH_TABLE = 'jan' 
set @CURRENT_MONTH_TABLE = 'jan'
set @CURRENT_YEAR = '2017'      

if ('+ @FIRST_MONTH_TABLE +' = '+ @CURRENT_MONTH_TABLE +')
    if object_id (''cumulative_revenue'+ @CURRENT_YEAR +''', ''U'') is not null drop table cumulative_revenue'+ @CURRENT_YEAR +';
    (select *
    into cumulative_revenue'+ @CURRENT_YEAR +'
    from '+ @CURRENT_MONTH_TABLE +''+ @CURRENT_YEAR +'_final )
else 
    if object_id (''cumulative_revenue'+ @CURRENT_YEAR +'_final'', ''U'') is not null drop table cumulative_revenue'+ @CURRENT_YEAR +'_final;
    (select * 
    into cumulative_revenue'+ @CURRENT_YEAR +'_final
    from (select *
            from cumulative_revenue'+ @CURRENT_YEAR +'
            union all
            select *
            from '+ @CURRENT_MONTH_TABLE +''+ @CURRENT_YEAR +'_final) as cumulative_rev)`enter code here`

我上面的代码会抛出错误

  

关键字'其他'

附近的语法不正确

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

你缺少BEGIN / END块。

你需要使用动态SQL。

我不会完全修复你的代码,但这里的内容可能更接近你想要做的事情:

declare @FIRST_MONTH_TABLE nvarchar(50) 
declare @CURRENT_MONTH_TABLE nvarchar(50)
declare @CURRENT_YEAR nvarchar(50)

set @FIRST_MONTH_TABLE = 'jan' 
set @CURRENT_MONTH_TABLE = 'jan'
set @CURRENT_YEAR = '2017'     

DECLARE @tableName sysname;
DECLARE @sourceTable sysname;
DECLARE @sql nvarchar(max);

SELECT @tableName =  N'cumulative_revenue' + @CURRENT_YEAR; 
SELECT @sourceTable = @CURRENT_MONTH_TABLE + @CURRENT_YEAR + N'_final';

if (@FIRST_MONTH_TABLE = @CURRENT_MONTH_TABLE)
begin
    if object_id (@tableName) is not null 
    begin 
        SELECT @sql = N'drop table ' + QUOTENAME(@tableName);
        EXEC (@sql);
        SELECT @sql = N'select * into ' + QUOTENAME(@tableName) + N' from ' + QUOTENAME(@sourceTable);
        EXEC (@sql);
   end
end
else 
begin
... etc
end

这应该会给你一个模板来弄清楚你做错了什么以及如何做对。我真的不清楚你要做什么,所以你需要自己调整自己的代码,基于这个例子。

答案 1 :(得分:0)

老实说,我不知道这是否正确,因为SQL很乱,但是,我认为这可能是你所追求的:

DECLARE @FIRST_MONTH_TABLE varchar(50) 
DECLARE @CURRENT_MONTH_TABLE varchar(50)
DECLARE @CURRENT_YEAR varchar(50)

DECLARE @SQL nvarchar(MAX);

SET @FIRST_MONTH_TABLE = 'jan' 
SET @CURRENT_MONTH_TABLE = 'jan'
SET @CURRENT_YEAR = '2017'      


IF (@FIRST_MONTH_TABLE = @CURRENT_MONTH_TABLE) BEGIN
    IF OBJECT_ID('cumulative_revenue'+ @CURRENT_YEAR, 'U') IS NOT NULL BEGIN
        SET @SQL = N'DROP TABLE ' + QUOTENAME('cumulative_revenue' + @CURRENT_YEAR) + N';';
        EXEC sp_executesql @SQL;
        SET @SQL = N'SELECT *' + NCHAR(10) +
                   N'INTO ' + QUOTENAME('cumulative_revenue'+ @CURRENT_YEAR) + NCHAR(10) + 
                   N'FROM ' + QUOTENAME(@CURRENT_MONTH_TABLE + @CURRENT_YEAR +N'_final') + N';';
        PRINT @SQL;
        EXEC sp_executesql @SQL; --Commented out as I can't run it
    END
END ELSE BEGIN
    IF OBJECT_ID('cumulative_revenue' + @CURRENT_YEAR + '_final', 'U') IS NOT NULL BEGIN
        SET @SQL = N'drop table ' + QUOTENAME('cumulative_revenue'+ @CURRENT_YEAR +N'_final') + N';';
        EXEC sp_executesql @SQL;
        SET @SQL = N'SELECT *' + NCHAR(10) + 
                   N'INTO ' + QUOTENAME('cumulative_revenue'+ @CURRENT_YEAR +N'_final') + NCHAR(10) + 
                   N'FROM (SELECT *' + NCHAR(10) + 
                         N'FROM ' + QUOTENAME('cumulative_revenue'+ @CURRENT_YEAR) + NCHAR(10) + 
                         N'UNION ALL' + NCHAR(10) + 
                         N'SELECT *' + NCHAR(10) + 
                         N'from '+ QUOTENAME(@CURRENT_MONTH_TABLE + @CURRENT_YEAR + N'_final') + N') as cumulative_rev);'
        PRINT @SQL;
        EXEC sp_executesql @SQL; --Commented out as I can't run it
    END
END