SQL Server动态化列名数据

2023-12-22 17:00:34

SQL Server动态化列名数据

DECLARE @ColumnName NVARCHAR(100)
SET @ColumnName = '总数' + CAST(DATEPART(HOUR, GETDATE()) AS NVARCHAR) + '点前派单'
DECLARE @Sql NVARCHAR(MAX)
SET @Sql = 'SELECT COUNT(1) AS ' + QUOTENAME(@ColumnName) + '
FROM MBHZJ_GD
WHERE "SN码" IS NULL AND CAST(boss归档时间 AS date) LIKE ''2023-12-21%'''
EXEC sp_executesql @Sql

DECLARE @ColumnName NVARCHAR(100)
SET @ColumnName = '总数' + CAST(DATEPART(HOUR, GETDATE()) AS NVARCHAR) + '点前派单'
DECLARE @DateFilter NVARCHAR(20)
SET @DateFilter = CONVERT(NVARCHAR(10), GETDATE()-1, 23) + '%'
DECLARE @Sql NVARCHAR(MAX)
SET @Sql = 'SELECT COUNT(1) AS ' + QUOTENAME(@ColumnName) + '
FROM MBHZJ_GD
WHERE "SN码" IS NULL AND CAST(boss归档时间 AS DATE) LIKE ' + QUOTENAME(@DateFilter, '''') 
EXEC sp_executesql @Sql

文章来源:https://blog.csdn.net/qq_43001132/article/details/135155550
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。