消息156,级别15,状态1,第1行关键字“BY”附近的语法不正确[closed]

ds97pgxw  于 2022-10-22  发布在  其他
关注(0)|答案(1)|浏览(205)

**已关闭。**这个问题是not reproducible or was caused by typos。它目前不接受答案。

这个问题是由一个打字错误或一个无法复制的问题引起的。虽然类似的问题可能在这里出现,但这一问题的解决方式不太可能帮助未来的读者。
2天前关闭。
社区在两天前审查了是否重新讨论这个问题,并将其关闭:
未解决原始关闭原因
i1 j2 k1 l
我尝试运行动态sql查询,但它显示了一个错误。我在谷歌上搜索了这个错误(标题中),但我似乎找不到它来帮助我,我的动态sql查询:

DROP TABLE IF EXISTS TABLE_NEW_XY
CREATE TABLE TABLE_NEW_XY (email VARCHAR(MAX),Profile VARCHAR(MAX),Subscriber 
VARCHAR(MAX),origin VARCHAR(MAX),country VARCHAR(MAX),Date VARCHAR(MAX))
select * from TABLE_NEW_XY
DECLARE @Sql NVARCHAR(MAX) = ''
,@TableName VARCHAR(MAX)
,@Id INT

DECLARE Table_Cursor CURSOR FOR 
SELECT 
ROW_NUMBER() OVER (ORDER BY TABLE_NAME ASC) Id
,TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE' 
AND TABLE_NAME LIKE 'Total_%'

OPEN Table_Cursor  
FETCH NEXT FROM Table_Cursor INTO @Id,@TableName 

WHILE @@FETCH_STATUS = 0  
BEGIN  
 IF(@Id = 1)
    BEGIN

        SET @Sql = @Sql + 'SELECT count(distinct(email)) email, Profile,Subscriber,origin,country , ''' + RIGHT(@TableName,7) + ''' Date FROM '+@TableName+ 'GROUP BY Profile,Subscriber,origin,country'  ----Modify the columns based on your column names
        SELECT @SQL
    END
 ELSE
    BEGIN
        SET @Sql = @Sql + ' UNION ALL SELECT count(distinct(email)) email, Profile,Subscriber,origin,country,''' + RIGHT(@TableName,7) + ''' Date FROM '+@TableName + 'GROUP BY Profile,Subscriber,origin,country' --Modify the columns based on your column names
    END

 FETCH NEXT FROM Table_Cursor INTO @Id,@TableName
END 

CLOSE Table_Cursor  
DEALLOCATE Table_Cursor

INSERT INTO TABLE_NEW_XY
EXEC (@Sql)

有人能帮我吗?

tv6aics1

tv6aics11#

@TableName and“分组依据”之间需要空格。
您需要使用@TableName+ ' GROUP BY而不是@TableName+ 'GROUP BY

DROP TABLE IF EXISTS TABLE_NEW_XY
CREATE TABLE TABLE_NEW_XY (email VARCHAR(MAX),Profile VARCHAR(MAX),Subscriber 
VARCHAR(MAX),origin VARCHAR(MAX),country VARCHAR(MAX),Date VARCHAR(MAX))
select * from TABLE_NEW_XY
DECLARE @Sql NVARCHAR(MAX) = ''
,@TableName VARCHAR(MAX)
,@Id INT

DECLARE Table_Cursor CURSOR FOR 
SELECT 
ROW_NUMBER() OVER (ORDER BY TABLE_NAME ASC) Id
,TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE' 
AND TABLE_NAME LIKE 'Total_%'

OPEN Table_Cursor  
FETCH NEXT FROM Table_Cursor INTO @Id,@TableName 

WHILE @@FETCH_STATUS = 0  
BEGIN  
 IF(@Id = 1)
    BEGIN

        SET @Sql = @Sql + 'SELECT count(distinct(email)) email, Profile,Subscriber,origin,country , ''' + RIGHT(@TableName,7) + ''' Date FROM '+@TableName+ ' GROUP BY Profile,Subscriber,origin,country'  ----Modify the columns based on your column names
        SELECT @SQL
    END
 ELSE
    BEGIN
        SET @Sql = @Sql + ' UNION ALL SELECT count(distinct(email)) email, Profile,Subscriber,origin,country,''' + RIGHT(@TableName,7) + ''' Date FROM '+@TableName + ' GROUP BY Profile,Subscriber,origin,country' --Modify the columns based on your column names
    END

 FETCH NEXT FROM Table_Cursor INTO @Id,@TableName
END 

CLOSE Table_Cursor  
DEALLOCATE Table_Cursor

INSERT INTO TABLE_NEW_XY
EXEC (@Sql)

相关问题