SQL 变量/命令?

Posted

技术标签:

【中文标题】SQL 变量/命令?【英文标题】:SQL variable/commands? 【发布时间】:2021-09-13 11:43:54 【问题描述】:

我正在寻找一种方法以更短的格式编写下面的代码。 “SET x...”部分是我想要一个快速命令(变量)来替换下面代码中的那个部分。

另外,如果有可能为不同的时间跨度编写带有更多变量/命令的整个代码,而不是每次都写出来。

SET x = DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE '中欧标准时间',4),1) as INT),TimeTrue)

SELECT
convert(varchar,TimeTrue,23) as Date,
TimeTrue as TimeTrueUTC,
DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as TimeTrue,
(CASE WHEN cast(TimeTrue as Date) BETWEEN '2021-02-08 00:00:00' AND '2021-07-11 00:00:00' THEN (CASE WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '06:00:00' AND '14:59:59' THEN 0 WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '15:00:00' AND '00:54:59' THEN 1 ELSE 2 END)
WHEN cast(TimeTrue as Date) BETWEEN '2021-07-12 00:00:00' AND '2021-08-29 00:00:00' THEN (CASE WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '06:00:00' AND '14:29:59' THEN 0 WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '14:30:00' AND '00:54:59' THEN 1 ELSE 2 END)
ELSE (CASE WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '06:00:00' AND '13:59:59' THEN 0 WHEN cast(DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) as time(0)) BETWEEN '14:00:00' AND '23:57:59' THEN 1 ELSE 2 END) END) AS ShiftNo


FROM PerformanceOpcArchive (NOLOCK)
WHERE convert(varchar,TimeTrue,23)>='2021-03-24'
AND convert(varchar,TimeTrue,23)<='2021-03-29'
ORDER BY TimeTrue ASC
;

我想写什么。

SET @x = DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue)


SELECT
convert(varchar,TimeTrue,23) as Date,
TimeTrue as TimeTrueUTC,
@x as TimeTrue,
(CASE WHEN cast(TimeTrue as Date) BETWEEN '2021-02-08 00:00:00' AND '2021-07-11 00:00:00' THEN (CASE WHEN cast(@x as time(0)) BETWEEN '06:00:00' AND '14:59:59' THEN 0 WHEN cast(@x as time(0)) BETWEEN '15:00:00' AND '00:54:59' THEN 1 ELSE 2 END)
WHEN cast(TimeTrue as Date) BETWEEN '2021-07-12 00:00:00' AND '2021-08-29 00:00:00' THEN (CASE WHEN cast(@x as time(0)) BETWEEN '06:00:00' AND '14:29:59' THEN 0 WHEN cast(@x as time(0)) BETWEEN '14:30:00' AND '00:54:59' THEN 1 ELSE 2 END)
ELSE (CASE WHEN cast(@x as time(0)) BETWEEN '06:00:00' AND '13:59:59' THEN 0 WHEN cast(@x as time(0)) BETWEEN '14:00:00' AND '23:57:59' THEN 1 ELSE 2 END) END) AS ShiftNo


FROM PerformanceOpcArchive (NOLOCK)
WHERE convert(varchar,TimeTrue,23)>='2021-03-24'
AND convert(varchar,TimeTrue,23)<='2021-03-29'
ORDER BY TimeTrue ASC
;

【问题讨论】:

您在寻找APPLY 运营商吗? 您可以编写一个函数,然后调用该函数来完成所有代码。它将在处理过程中添加一个步骤来调用该函数,然后执行您想要的所有操作。更新:如果您可以在 powerbi 领域中使用这种类型的东西(在我发布后注意到)。 我相信我可以使用它。我可以在 Power BI 中编写 SQL 查询。在查看 microsoft 上的功能帮助时,我不明白它是如何适应的以及如何编写它。 【参考方案1】:

您可以使用 datapart(tz) 以分钟为单位获取中欧标准的偏移量。(此处参见 variable_x2)

例如:

 select DATEADD(HOUR
                ,CAST(LEFT
                         (RIGHT(convert(datetime2(0), SYSUTCDATETIME(), 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),SYSUTCDATETIME()
                ) as variable_x
      ,dateadd(minute
               ,datepart(tz,SYSUTCDATETIME() AT TIME ZONE 'Central European Standard Time') 
               ,SYSUTCDATETIME()) as variable_x2


+-----------------------------+-----------------------------+
|         variable_x          |         variable_x2         |
+-----------------------------+-----------------------------+
| 2021-09-13 13:59:42.8387246 | 2021-09-13 13:59:42.8387246 |
+-----------------------------+-----------------------------+

【讨论】:

我相信这不是我想要的。我想调用 variable_x 而不是编写的整个代码。我不知道在 SQL 中调用了什么函数,但我研究了 DECLARE 函数,但似乎无法让它工作。 DECLARE @x = DATEADD(HOUR,CAST(LEFT(RIGHT(convert(datetime2(0), TimeTrue, 126) AT TIME ZONE 'Central European Standard Time',4),1) as INT),TimeTrue) 可能会破坏代码以及如何使用这些函数,但不熟悉这些,并试图找到一种方法将所有代码变成一个简单的语句,例如示例中的“@x”。 您将使用动态 sql 首先构造您的查询字符串并设置变量@x。之后,您将使用 EXEC (@qry_string) 来执行查询。然而,这在 T-SQL 中作为代码块使用 declare @x set@x=' set @qry_string=concat('select ',@x,' from table') exec (@qry_string)

以上是关于SQL 变量/命令?的主要内容,如果未能解决你的问题,请参考以下文章

使用 C# 和一个连接在 SQL 命令中持久化变量

SQL中啥时候要用declare声明变量

如何用SQL代码将动态查询结果赋值给变量?

SQL Server 中区分大小写的变量名? [关闭]

如何将数据库查询结果放入变量中

如何从 Databricks 中的 SQL 语句输出创建变量